简体   繁体   中英

how do I create a .jar to run command line application

For ease I'll use hello world...

public class HelloWorld{

    public static void main (String [] args){
        System.out.println("Hello World");
    }
}

Compiles and runs fine as you'd imagine. Then made a file Manifest.txt containing:

Main-Class: HelloWorld

then ran:

jar -cfm Hello.jar Manifest.txt *.class

However when I double click the resulting Hello.jar, nothing happens. I'd expect the console to pop up quickly at least... (I in fact have a more elaborate program which waits for input)

Any ideas why this is not working?

It is starting a process in the task manager (windows) but no cmd openning

make sure the Main-Class line in the manifest file is followed by an empty line!
Hard to believe, but the specification mandates that each line of the manifest file, including the last one, be terminated by a newline character.

EDIT:
If the problem is just with double clicking, try (assuming Windows):

assoc .jar=jarfile
ftype jarfile="<JRE directory>\bin\javaw.exe" -jar "%1" %*

(use these without argument to check the actual settings)

Your clients will need the JRE to run the Java program - the above settings normally are done by the installation of the JRE.

EDIT2:
Use java.exe instead of javaw.exe to see the standard console (if your program does not have a GUI)

Try executing from command line

java -jar /path/to/YourJar.jar

Make sure you have Main-Class entry


See Also

The manifest needs to be named MANIFEST.MF and it needs to reside in a top level directory called META-INF. Then it should work.

You should give JSmooth a try. It takes your jar file and wraps it into a Windows executable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM