Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes jar files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "jar files" Watch "jar files" New topic
Author

jar files

Brian R Wolf
Greenhorn

Joined: Jan 25, 2010
Posts: 18

Hi,

I have a library in a jar file that has this:

10230 Mon Dec 11 00:23:48 MST 2006 com/sun/syndication/io/XmlReader.class
1692 Mon Dec 11 00:23:48 MST 2006 com/sun/syndication/io/XmlReaderException.class

I compile a program, that has this line in it:

XmlReader reader = null;

It compiles into a class with no complaints

javac -cp ./rome-0.9.jar:./jdom.jar Reader.java

However, when I run it:

java -cp . Reader

Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/syndication/io/XmlReader

so is it expecting access to the jar file at run time? (I thought jar files get compiled into the class)

Thanks
Brian









Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18520
    
  40

Brian R Wolf wrote:
Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/syndication/io/XmlReader

so is it expecting access to the jar file at run time? (I thought jar files get compiled into the class)


Yes, it is expecting the jar file. (and no, it doesn't get compiled into the class).

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Brian R Wolf
Greenhorn

Joined: Jan 25, 2010
Posts: 18
I'm not sure how to do that:

java -jar ./rome-0.9.jar -cp ./Reader
Failed to load Main-Class manifest attribute from
./rome-0.9.jar

Thanks
Brian
Brian R Wolf
Greenhorn

Joined: Jan 25, 2010
Posts: 18
I also try this

java -cp ./rome-0.9.jar ./Reader
Exception in thread "main" java.lang.NoClassDefFoundError: //Reader
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18520
    
  40

Brian R Wolf wrote:I'm not sure how to do that:

java -jar ./rome-0.9.jar -cp ./Reader
Failed to load Main-Class manifest attribute from
./rome-0.9.jar



The -jar option is to run specially configured jar files -- that specifies a main. To run a class, that needs a jar just include the jar in the class path. In much the same way as you did while compiling....

java -cp ./rome-0.9.jar:. Reader


Henry
Brian R Wolf
Greenhorn

Joined: Jan 25, 2010
Posts: 18
OK, that works great , thanks
one more thing bugging me about jar files,

I have this one

jar xvf twitter4j-core-2.1.0.jar

....

and clearly contains
inflated: twitter4j/http/HttpClient.class

however when I try to use this class,

I declare it in the program:
....
import twitter4j.internal.http.HttpClient;
....

then when I try to compile:

javac -cp ./twitter4j-core-2.1.0.jar FeedMonitor.java

FeedMonitor.java:133: cannot find symbol
symbol : class HttpClient
location: class FeedMonitor
private HttpClient http = new HttpClient();




Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18520
    
  40

That's because those two classes are two different classes -- as a classname is fully qualified with the package too. The class you are trying to import is not the one in the Jar file.

Also, unless you are sure, I don't recommending changing the import -- just because two APIs have the similar class names doesn't mean that they work the same.

Henry
 
wood burning stoves
 
subject: jar files
 
Similar Threads
Updated servlets assignment page
Stale Connection Exception
WAR files with Tomcat 4.1
Need Servlets 4a Help
Quick refresher.....