File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Converting .class to .com Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Converting .class to .com" Watch "Converting .class to .com" New topic

Converting .class to .com

Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
How would I take an xyz.class file containing a proper main() method and create an file that could be called on the command line by simply typing "xyz" ?

Mike Gershman
SCJP 1.4, SCWCD in process
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
From what I know, .com files are rarely used any more except for specific system tasks. .exe files are much more common for several technical reasons, however this probably isn't the place to go into such a discussion.

There are some other tricks to make a single executable that will run your Java program. Although, for a single file with a single class, it probably isn't worth it. The trick I have begun to use lately is to create a JAR file (using the jar utility) that contains all the .class files that I compile from my source code. You need to make a manifest file with the "Main-Class" attribute set to the class that should be run from this jar file.

Once the jar is created, you can run it by typing

In Windows, you can also execute the jar file by double-clicking it in Windows Explorer.

There are also compilers available that compile your Java program into an executable, but I am not familiar with them. The main draw back is that the compiled program looses Java's cross-platform capabilities. On the other hand, the jar file I described above can usually run as-is on any machine with a JVM installed.



Java API Documentation
The Java Tutorial
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
p.s. You may also want to use the Search tool (see the link at the top of the page) to find previous messages discussing this topic. You can start with "stand alone executable" and see what you can find.
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
For creating an executable JAR, you might like to take a look at the "Creating an Executable JAR" thread.

For creating an exe, I recommend trying a quick search on this and the intermediate forums for "java exe".

If you go the exe route, please report back and let us know how things went. Of course, if you run into a problem, don't hesitate to post a question about it. I'd recommend the intermediate forum for such a conversation.
[ September 19, 2004: Message edited by: Dirk Schreckmann ]

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
I agree. Here's the link:
subject: Converting .class to .com
It's not a secret anymore!