This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes classpath problem, can't get jars to run Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "classpath problem, can Watch "classpath problem, can New topic
Author

classpath problem, can't get jars to run

Mark Brunson
Greenhorn

Joined: Apr 29, 2012
Posts: 14
So a while ago something happened that caused all of the jars I create using netbeans that use external libraries to give me the error 'Exception in thread "main" java.lang.noclassdeffounderror' when run. If I run them from the command line with the command 'java -cp "lib/library.jar;MyJar.jar" mypackage.MainClass' it will run, so I'm pretty sure somehow the lib folder is not being added to the classpath.

I also tried compiling it with a custom ant script, but that didn't fix anything.

How can I get my jars to run by just clicking on them?
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

to be able to run a jar file by clicking on it you require an executable jar . also the jar in question should have main method. that is but obvious. when you compile and build project in netbeans it automatically creates executable jar of your project in dist directory of your project folder. this is one way. you can also manually make executable jar file by adding manifest file with Main-class attribute. for that you need to refer some tutorial on the internet.
Mark Brunson
Greenhorn

Joined: Apr 29, 2012
Posts: 14
gurpeet singh wrote:to be able to run a jar file by clicking on it you require an executable jar . also the jar in question should have main method. that is but obvious. when you compile and build project in netbeans it automatically creates executable jar of your project in dist directory of your project folder. this is one way. you can also manually make executable jar file by adding manifest file with Main-class attribute. for that you need to refer some tutorial on the internet.


This is how I have been doing it for a while (netbeans method), and it worked pretty well. However for some reason this doesn't work anymore, and I get the error I mentioned (noclassdeffound) when I try to run it by just clicking. It definitely has all those other things because I can run it from the command line if I use a command that first sets the classpath manually, which it shouldn't if the jar wasn't executable or didn't have the main method set correctly.
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

just do one thing. in your project in netbeans, right click the project and then go to RUN and check whether the main-class is set or not. if ain't set then the resulting jar file will not be excutalble jar file. so check this setting out and set it to your proper main class
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37936
    
  22
Unzip the jar and read its manifest file. You can find out about manifest files here.
Mark Brunson
Greenhorn

Joined: Apr 29, 2012
Posts: 14
Campbell Ritchie wrote:Unzip the jar and read its manifest file. You can find out about manifest files here.


Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.4
Created-By: 1.7.0_02-b13 (Oracle Corporation)
Main-Class: mypackage.MainClass

Is there a simple way to get this to include classpath information?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37936
    
  22
Yes. Didn’t it explain it in the link I gave you yesterday? Go back to the link, one level up, and look for the section about adding classes to the manifest file. I think this is it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: classpath problem, can't get jars to run
 
Similar Threads
setting ANT class path
MessagingException with Ant mail task
Error with QueueElement when deploying EJB's
Classpath - usual problem I guess
Enviroment Variable Max Size