0

I have a simple Java program (lets call it MyProgram.java) that does some I/O, re-names some images, deletes a directory, etc. I've been browsing around S/O looking for a simple way to run a Java program's main method from command prompt. I've compiled the source code into a jar, and tried using Jar2EXE Wizard, however I kept getting an unexpected compilation error that I wasn't getting while running my code from the IDE.

Does any one have either a Jar -> EXE converter solution they've had success with or can walk me through how to run my program from a batch file?

3
  • Does your code have dependency on external libraries. Commented Nov 5, 2013 at 14:43
  • 1
    "I kept getting an unexpected compilation error that I wasn't getting while running my code from the IDE" - you need to fix those errors first. No wizard can help you here. Commented Nov 5, 2013 at 14:45
  • Yes, code uses the Apache commons FileUtils/IOUtils libraries Commented Nov 5, 2013 at 14:49

4 Answers 4

1

[..]or can walk me through how to run my program from a batch file?

The simplest way is to execute:

java -jar YOUR_JAR_FILE.jar 

in your batch file. However this requires a manifest file to be present in your jar file which specifies the Main class to use and jar files it depends on. If you do not want to work with a manifest file you can specify these things manually. If you do not depend on external jar files you can execute:

java -cp YOUR_JAR_FILE.jar some.package.Main 

This will execute the public static main(String[] args) method in class some.package.Main contained in YOUR_JAR_FILE.jar.

If there are other jar files you depend on (in your case that would be IOUtils/FileUtils), specify those jar files as well:

java -cp YOUR_JAR_FILE.jar:library1.jar:library2.jar some.package.Main 

(in your case library1 and library2 are IOUtils and FileUtils respectively).

You can specify any number of jar files and you can also use the wildcard *.jar to include all files in the current (or another) directory. Note however that you cannot write * or x*.jar or the like. Only *.jar (or some/directory/*.jar) is accepted.

In 90% of the times, the order of the jar files does not make any difference. However sometimes it does make a difference: If a resource is loaded from the classpath (could be a class or something as simple as a configuration file), the jar files are searched in the order you specified. If a resource exists in multiple jar files, only the first one found will be used.

Sign up to request clarification or add additional context in comments.

4 Comments

I know the question title is wrong but it seems OP wants to convert jar to exe
You can specify a classpath in the parameter list too.
The program compiles fine, and runs fine. However, I get a run time error that I don't get when running the program from the IDE. Do I have to specify my classpath in the batch file as well? Note: My program has dependencies on the apache commons IOUtils/FileUtils
@kidwit: Sry, I totally forgot about your question. Better late than never: I extended my answer to cover you dependencies.
0

You can consider using install4j.

Comments

0

If you want use batch file you can write this:

java -jar sources.jar 

If your code have more than 2 static void main(String[] args) you need explicitly hit the method:

java -jar sources.jar classes.package.Main 

Directory structure:

-\project\ -\project\run.bat -\project\sources.jar 

Comments

0

Take a look at JSmooth. It wraps your JARs as executables and provides options for detecting, and handling lack of, the JVM. I've used it on a simple app and it was painless.

Bonus: it is available as a portable app with no installation needed.

Comments