aspose file tools*
The moose likes Beginning Java and the fly likes JAR file class not found exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "JAR file class not found exception" Watch "JAR file class not found exception" New topic
Author

JAR file class not found exception

Brian Lang
Ranch Hand

Joined: Oct 21, 2008
Posts: 43
Hey all, first post. I'm relatively competent with Java, having tinkered with it a bit here and there over the years. I'm developing a simple GUI application to connect to and query our internal database (MySQL via JDBC). All this works fine. The problem I have is with the JAR I've created to distribute the application. It always fails with a 'class not found' exception.

Here's what I've done:

Created a directory for the application, e.g. C:\myapp
Used the package 'myapp' for all classes
Created a JAR with the command
jar cfe MyApp.jar MyAppEntry *.class

I've also added C:\myapp to my CLASSPATH.

I've never had a clear understanding of packages and the classpath anyway, I think the problem stems from this.

TIA!
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3446
    
  12
How are you running your program ? If you are using the java -jar option then you need to include the classpath and the main class in the jar's manifest file. See this for more info.

If you are not using the -jar option, then you need to include the jar file in your classpath and specify the main class on the command line
e.g.
java -cp myjar.jar MyMainClass
[ October 23, 2008: Message edited by: Joanne Neal ]

Joanne
Brian Lang
Ranch Hand

Joined: Oct 21, 2008
Posts: 43
Oh, yes I should have mentioned, I am attempting to run it with this command

java -jar MyApp.jar

Thanks for your reply, I'll look into that.
Brian Lang
Ranch Hand

Joined: Oct 21, 2008
Posts: 43
Henry- I did not notice that, thanks. I've made the necessary change in my profile.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

Check out the Class-Path entry for manifest files. You can include relative links to other JAR files you need in there. Joanne has given you a link for that.
[ October 23, 2008: Message edited by: Rob Prime ]

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Brian Lang
Ranch Hand

Joined: Oct 21, 2008
Posts: 43
Ok thanks for your replies; I eventually figured out the problem. I was packing the JAR file from within the directory, and I wasn't referring to the package name in referencing the class.

So now either one of these commands works:

jar cfm MyApp.jar Manifest.txt myapp\*.class
(where Manifest.txt contains the line "Main-Class: myapp.AppEntry")
-or-

jar cfe MyApp.jar myapp.AppEntry myapp\*.class

I can take the resulting JAR and either run it via command line using java -jar or double-clicking in Win explorer.
[ October 23, 2008: Message edited by: Brian Lang ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JAR file class not found exception