0

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.
Community
  • 1
  • 1
wrahool
  • 1,101
  • 4
  • 18
  • 42

2 Answers2

1

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.

SSaikia_JtheRocker
  • 5,053
  • 1
  • 22
  • 41
0

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
Dan Ciborowski - MSFT
  • 6,807
  • 10
  • 53
  • 88
  • I'm not sure that'll work, because the command expects a -info as arguments. – wrahool Aug 01 '13 at 10:18
  • This is how you run a jar and fix your error of not declaring a main class. Please try it and report back errors. You shouldn't have to moe anything. Everything you need should be found in your dist folder including your lib folder. – Dan Ciborowski - MSFT Aug 01 '13 at 10:46