REAL O G
REAL O G

Reputation: 685

Command Line Java + Jar Files

So far, I've only ran simple apps from the command line via javac filename.java. Now, I've created an application in NetBeans and I really want it to run from the command line. I am using an external .jar file in NetBeans that is a program dependency. How do I go about running this from command line? Furthermore, how would I go about creating a shortcut so that I all I have to do is double click and the program would start running?

EDIT: I've gotten it to compile, but it still won't run. It gives me this Error: Could not find or load main class JavaApplication1 even though the .java file, class file, and .jar file are all there.

EDIT2: I ended up solving this by going back to netbeans, doing a "build and clean" and using the .jar file that netbeans generated to run the project. Thanks for all your help!

Upvotes: 2

Views: 1966

Answers (3)

NRKirby
NRKirby

Reputation: 1634

To execute a jar from the command line you can run the following from the command line:

java -jar <jar-file-name>.jar

To have a shortcut execute the jar file make sure it runs the same command as above.

Upvotes: 1

Davide Lorenzo MARINO
Davide Lorenzo MARINO

Reputation: 26926

To start a java executable jar application:

 java -jar executable.jar

If you haven't an executable jar but only a single class or some class not packaged in a jar file you have to use the following command:

java MyClass

Where MyClass is the class with the main method.

Upvotes: 1

Tammert
Tammert

Reputation: 196

If you want to compile/run with a dependency on a jar, the -classpath modifier is used. For example:

javac -classpath <path-to-jar> filename.java

Upvotes: 2

Related Questions