Java file to .exe

Using the Java SDK with TextPad

Moderators: AmigoJack, bbadmin, helios, Bob Hansen, MudGuard

Post Reply
Joe

Java file to .exe

Post by Joe »

Hi, i was wondering if anyone knew how to change .java file into a .exe file. can it be done with textpad?
Garrett

Re: Java file to .exe

Post by Garrett »

Short answer:

With TextPad: No. Not remotely. With other apps: Yes, but it's more effort than it's worth.


Long answer:

No. There is no simple way to compile a .java file into a .exe file. A .java file gets compiled into .class files, (which can then be zipped into .jar files,) which is then executed by the JVM. Sun neither encourages nor supports compiling into a .exe file, since their big selling point on java is platform-agnosticism, something that is lost if you compile to a binary.

However, there ARE applications that let you compile .java files or .class files into .exe files. The ones I can think of right now are Microsoft Visual J++, (which is no longer supported,) and TowerJ, but there are others, I think. These applications, however, are intended for servers, to eke out maximum performance out of your application server. Since they only give you an additional 5% or so, and since they end up disabling some of the cooler functionalities of java, most notably dynamic class loading, they are inappropriate for client-side applications.

The real question is: Why bother? If you just want to have one _single_ file to execute, (the most common reason for the .exe request,) you can come close by zipping up all your class files into a single .jar file, and then making it executable. To make the .jar file executable, you need to modify the manifest.inf file located within the .jar file at META-INF/manifest.inf by adding the following line:

Main-Class: com.yourpackage.YourClassName

For any java.exe version 1.2.x or greater, you can call the following from the command line:

java myjarfile.jar

And it will then call the main() method within the class file defined by the manifest.inf file. At this point the only trick is executing the command, which can be done either with a .bat file or a shortcut. You only need to be careful that you set the proper working directory, either in the case of a .bat file with the line:

cd C:\mydir\

Or in the case of a shortcut, by setting the "Start In:" field to C:\mydir\. Now, obviously, this results in two files instead of one, but that's about is close as you can get.

If, on the other hand, you don't want to require your users to have the JRE installed, you're out of luck. The only solution is to compile. Even then, though, it's probably better just to package your application using installshield and installing the JRE. Microsoft Visual J++ is only available through an old version of MS Visual Studio, which costs upwards of $1200 and doesn't support java versions later than 1.2.2, while TowerJ supports version 1.4 and up, but it costs even more: $1500.
Onchik

Re: To Joe

Post by Onchik »

exeJ ( FREE )

http://www.bysoft.se/sureshot/products.html

exeJ is a command line tool that creates a "batch-like" executable ( .exe file )
for your java application.
The command line used to start your java application is wrapped in the generated executable and the user can simply start the java application with a double-click. The generated .exe is Windows 95/98/ME/NT/2000/XP compatible.

The target system must have JavaSoft JRE installed.

;-) ;-) ;-) ;-) ;-) ;-) ;-) ;-) ;-) ;-) ;-) ;-) ;-) ;-) ;-);-) ;-) ;-) ;-) ;-) ;-) ;-) ;-)
User avatar
jeffy
Posts: 323
Joined: Mon Mar 03, 2003 9:04 am
Location: Philadelphia

Re: Java file to .exe

Post by jeffy »

Garrett wrote:Short answer:

...

Long answer:

...
Just wanted to say how impressed I was with your reply. :' )
Post Reply