Totoro
Totoro

Reputation: 101

Java running external jar without IDE?

I've written a java program that reads an excel file with jxl.jar. It's currently working, but I have to use cmd to run the program. Double clicking the jar file does not appear to be working. These are the commands I use to compile and run the code:

javac -classpath C:/workspace/jxl.jar:. main.java GUi.java

jar cvfm run.jar manifest.txt Main.class GUI.class GUI$1.class GUI$2.class GUI$3.class Main$1MyCustomTableCellRenderer.class Main$1YourTableCellRenderer.class Main$MyCustomTableCellRenderer.class

java -cp run.jar Main

I'm not really sure why it's any different from double clicking it. I've compiled the jxl file into the run.jar file so I don't understand why it doesn't work?

Upvotes: 1

Views: 1460

Answers (2)

VijayaKumar Thangavel
VijayaKumar Thangavel

Reputation: 514

Try this in command prompt:

Run the below command if you bundled the jar with all libraries

java -jar MyJar.jar

If you need to add jars at the time of execution please add all the jars at command line, as follows;

java -cp /path/jxl.jar;myJar.jar Class-Name-Main

Upvotes: 0

Braj
Braj

Reputation: 46861

An Example directly from Java Tutorial on Adding Classes to the JAR File's Classpath as suggested by @MadProgrammer in his comment.

We want to load classes in MyUtils.jar into the class path for use in MyJar.jar. These two JAR files are in the same directory.

We first create a text file named Manifest.txt with the following contents:

Class-Path: MyUtils.jar

Warning: The text file must end with a new line or carriage return. The last line will not be parsed properly if it does not end with a new line or carriage return.


We then create a JAR file named MyJar.jar by entering the following command:

jar cfm MyJar.jar Manifest.txt MyPackage/*.class

This creates the JAR file with a manifest with the following contents:

Manifest-Version: 1.0
Class-Path: MyUtils.jar
Created-By: 1.7.0_06 (Oracle Corporation)

The classes in MyUtils.jar are now loaded into the class path when you run MyJar.jar.

Read more...

Upvotes: 1

Related Questions