Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

classpath problem, can't get jars to run

 
Mark Brunson
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48386
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unzip the jar and read its manifest file. You can find out about manifest files here.
 
Mark Brunson
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48386
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic