开发者

Why Java programs use .bat file as program launcher?

开发者 https://www.devze.com 2022-12-30 04:12 出处:网络
I found that a lot of Java programs in Windows use .bat batch file as p开发者_StackOverflow中文版rogram launcher. It looks weird if compared with other programs which use .exe executable file.

I found that a lot of Java programs in Windows use .bat batch file as p开发者_StackOverflow中文版rogram launcher. It looks weird if compared with other programs which use .exe executable file.

Why can't those Java programs use .exe file? Is that a limitation of Java program?

Thanks.


It's pretty easy to just create a .bat file and include the java -jar <archive-path> in it and you have it running. You can edit the .bat file easily and configure it more. All you need is notepad.

Also it looks pretty much like bash script files for Linux for example. This makes a .bat file for execution a program in a .jar easily transfered from Win to Linux.

While an .exe file is not easily modifiable(if at all) and it produces some meta-data, the resulting .exe is larger... EXE also is a Windows specific file extension...

Of course, if you want to pack everything up in a single file and have the user not worry about deleting the .jar file but still having the bat... that's another case.


batch file executes often "java -jar" and additionally adds JVM parameters - this can't be added to jar file.

its faster to write bat file than wrap java archive to exe file - those tools are often commercial or don't work well ...

also, there are not many native windows executables (.exe) done in java (however, it's still possible - vuze, limewire etc.)


Java doesn't compile to .exe. It compiles to a virtual machine, the JVM. This is the fundamental aspect of Java platform technology.

The .bat is essentially a shell script for Windows. It does execute an .exe, e.g. java.exe from the JRE which then run the bytecode compiled Java code.


Actually, quite a lot of Java applications do use native launchers instead of batch scripts: Eclipse, NetBeans, IntelliJ IDEA, JRuby and Vuze are only few of the examples.

The main reason why people don't do this more often, is that embedding a JVM in C is actually somewhat tricky and if you can get away with launching it from a batch script, then why bother?


Good question. I also wondered why Sun never provided some tool te generate an .exe.

An exe-file is much more user-friendly than a bat-file. It has a nice icon etcetara.

I once wrote something myself: http://www.bitstorm.org/jarx/


A batch file can load the JVM with the necessary classpath etc and tell it to run a jar or class or class in a jar. It's overkill and more work to produce an executable PE image just to issue one or two commands to launch the JVM and tell it to open a jar.


Well... you must use batch file if you want to execute .class file for example. You want to use batch because you must pass your java program URI to the java virtual machine (also a program) so it can execute it.

But you can build java program to be executable, same as EXE (just need to double click it to execute it). Its extension is .JAR, and sometimes you need to associate .JAR extension to Java Platform Binary (or similar) to achieve this. Then its up to Windows to pass your java application's URI to the java virtual machine, so it gets executed.

To do this in NetBeans, execute Build command, and in your project folder find folder dist, and there you will see a java executable.

Exe, or no exe, functionality is the same from user's perspective, so it doesn't meter...

A NOTE

Java programs are platform independent, so on different platforms process of execution can be different. If Java would be only for Windows, I'm sure that SUN would make it compile to Windows EXE.


Welcome to the world of DOS shell scripts. You can make a jar file executable but if you just have java class files then about the best you can do is type up a little script to run your java program.


Java programs were designed from the ground up to be run not by the OS but by a virtual machine. When you "run a java program" you're actually starting up a virtual machine and telling it to load your program.

That's just how it was designed. It's not that they were slacking off in the quality department (one answer suggested Sun only gets a product to 80% completion), it was a conscious decision. And because it's extremely trivial to create a batch file, shortcut, shell script or executable that will provide the one-click launcher, it wasn't necessary to add the ability to convert a java program to a single-click .exe (in the case of windows).


My two reasons are:

  1. Many installer builders do not allow creating a program menu shortcut containing for example java -jar whatever.jar unless you somehow configure the installer to accept java as a program existing on the target machine. This may be difficult or impossible, while creating a shortcut to a BAT file included in the distribution is a breeze.
  2. As stated by others: Need to add JVM startup parameters depending on host machine configuration.


You can, multi platform installation authoring tool could create one for you. For example, InstallAnywhere, it'll create a windows executable for you java application.

Sun was a company who follow the 80/20 rule, they get most important things to work but never make it work well. For example, compare java plug-in for browser detection and install to adobe flash. Flash was fast and easy many years ago, while java plugin often forward you to sun site, and that's it.

Back to the executable, the java model is write once, run many, when Sun decide to provide a launcher, they will have to provide it for all platform. For example, a windows JDK would required to provide launchers for windows x86, ia64, amd64, linux/unix can share a launcher since they all use share shell scripts. Then the question is, does sun show a packager for mac? It just doesn't fit in, you should definitely look into tools like installanywhere and install4j, they all include java launchers.


You can use autoit to very quickly create an exe with a custom icon that would look more professional than just a .bat file.

0

精彩评论

暂无评论...
验证码 换一张
取 消