wrahool
wrahool

Reputation: 1141

Exporting a jar using Eclipse and then running it

I'm using Eclipse 3.7 on Ubuntu 12.04.

I have five .java files in a certain Eclipse project in the src folder. The project folder has (apart from the bin, lib and src folders), the help file and the properties file along with certain input files.

I need to create a .jar file with the java files, and make it executable from the command line, along with the properties file as a parameter.

For example :

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

I used Eclipse to export the project as a .jar file, into the bin folder.

I copied all the input files, the properties file and the help file into the dist folder and ran the command.

I got an error saying

Failed to load Main-Class manifest attribute from jar

I then checked this answer and did the needful (created the manifest file with a line) and the ran

jar cfm <jar-file-name>.jar <manifest-file-name> ./bin/*.class

It didn't work, and threw a ClassNotFoundException.

Any help would be appreciated.

EDIT

I added the Main class by choosing 'Next' instead of 'Finish' while exporting the .jar. Now, while executing it, it threw a "ClassNotFoundException" for the mysql connector jar. Even though, it is included in the lib folder which had been added while making the jar.

Exception in thread "main" java.lang.RuntimeException: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

Second EDIT

Relevant outputs.

java -cp ./lib/* -jar <non-runnable-jar>.jar -info info.properties

where ./lib/ has all the dependent jars.

It gave this error :

Exception in thread "main" java.lang.RuntimeException: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    at polygonprobability.Model.readTestingData(Model.java:178)
    at polygonprobability.Model.<init>(Model.java:136)
    at polygonprobability.Info.getModel(Main.java:290)
    at polygonprobability.Main.loadInfo(Main.java:138)
    at polygonprobability.Main.operInfo(Main.java:61)
    at polygonprobability.Main.distribute(Main.java:170)
    at polygonprobability.Main.parse(Main.java:81)
    at polygonprobability.Main.main(Main.java:34)
Caused by: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:188)
    at polygonprobability.DatabaseConnect.authorizeSQL(DatabaseConnect.java:50)
    at polygonprobability.Model.readTestingData(Model.java:157)
    ... 7 more

For

java -cp /lib* -jar <non-runnable>.jar <packagename>.Main -info info.properties

It gave this error

Exception in thread "main" java.lang.NoClassDefFoundError: /lib64
Caused by: java.lang.ClassNotFoundException: .lib64
    at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: /lib64. Program will exit.

Upvotes: 0

Views: 1299

Answers (2)

Dan Ciborowski - MSFT
Dan Ciborowski - MSFT

Reputation: 7207

The command line to run a jar should be

java -jar myJar.jar your.package.ClasswithMainMethod

If this is provided errors please include them.

I believe you might specifically want

java -jar myJar.jar your.package.ClasswithMainMethod args

And the best way I find to run the program is from inside your project file one step above the dist folder so you would actually run

java -jar ./dist/myJar.jar your.package.ClasswithMainMethod args 

That way when you rebuild Eclipse doesn't complain that it can't delete the dist folder

Lastly if you for some reason have moved your lib path try the following

java -cp /path/to/lib/*:myJar.jar your.package.ClasswithMainMethod args

Upvotes: 0

SSaikia_JtheRocker
SSaikia_JtheRocker

Reputation: 5063

Try these steps -

  1. Export a non-runnable jar without the Main class included (skip the next part)
  2. Then do this java -cp /path/to/lib/*:yourjar.jar pkg.MainClass -info file.properties

/path/to/lib/* = path to all your dependent jars.

Upvotes: 1

Related Questions