views:

400

answers:

12

Hi,

I found that a lot of Java programs in Windows use .bat batch file as program 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.

+7  A: 

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.)

Xorty
For the "native" Java programs you refer to the PE executable does little more than start up the JVM (usually in a DLL) and launch a jar file – the same thing the batch file does, just a little bit more hidden. You can go a step further and embed the jar into the executable but I doubt that's done that often.
Joey
yes - but the question wasn't how to distribute java programs, but what batch files do ...
Xorty
+4  A: 

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.

polygenelubricants
.NET CIL is also not native code, yet resides in a normal PE file. It's not that Java couldn't do the same, it's just that the designers didn't do so.
Joey
A: 

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/

edwin
Nope, in terms of user friendlyness, the user never even sees what type of file it is, he just clicks on a shortcut.
Longpoke
Java can be built in JAR, which is same on JVM as EXE is on WIN.
Cipi
A: 

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.

Longpoke
A: 

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.

Cipi
A: 

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.

wsxedc
The standard way to distribute a desktop app is to use Java Web Start. And that claim about Sun not making things work well is very controversial. Java works well for me.
Pascal Thivent
Sun was most definitely *not* a company to follow the 80/20 rule. You don't seem to know much about the history of Sun.
Bryan Oakley
A: 

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.

Winter
A: 

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

+10  A: 

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.

Leni Kirilov
A: 

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).

Bryan Oakley
+1  A: 

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?

Jörg W Mittag
A: 

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.
fredarin