• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Using Jars and the confusion that ensues

 
Ranch Hand
Posts: 31
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

So I've spent the past couple hours scouring the internet for an answer to this but I guess it might be a bit too specific.

I'm trying to use an outside jar file (jtidy) and I've really no experience with it.
I also use the command line so no eclipse hand-holding.
As far as I know I have the classpaths all set up and working properly and if I keep things simple i.e.


and then run:
c:\myJava\Files>javac TidyTest.java
c:\myJava\Files>java TidyTest

this compiles and runs and prints 'Super Tidy!'
If I compile to an outside directory i.e.

c:\myJava\Files>javac -d <new place> TidyTest.java

it will compile, but!
If I try to run it from that directory i.e.

c:\myJava\Files>java -cp <new place> TidyTest

I get a NoClassDefFoundError: org/w3c/tidy/Tidy

I've read elsewhere that I have to be explicit when I run the class as to where to find the jar file, but why would it work when I compiled the class to the .java file's home directory and not to an outside directory?
The classpath pointing to the jar must be correct if it compiles in the first place...isn't it looking in the same place when it tries to run the TidyTest class?

I hope I've explained this clear enough.
Thanks for the help!

-Mark

 
Marshal
Posts: 80640
472
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to add the .jar to your CLASSPATH and the best way to do that is probably to use the -cp option both for the javac tool and the java tool, not by setting a system CLASSPATH. Start by writing down the location of the jtidy.jar file on paper. Go through the Java™ Tutorials and our Wiki. Be sure to add the .; which means, “look in the current directory,” to your CLASSPATH.
 
Mark Justison
Ranch Hand
Posts: 31
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so let's say I have my class compiled to C:\Users\directoryA and my .jar files are in C:\Users\lib.
my PATH and my CLASSPATH both point to C:\Users\lib and C:\Users\lib\myJar.jar

I try something like:

'C:>java -cp c:\Users\directoryA TidyTest'
and I get the NoClassDefFoundError

but if I navigate to directoryA and do this:
'C:\Users\directoryA>java TidyTest'
that works fine. I'm guessing somehow it knows the jar file exists but when I try to use the -cp to point to that directory from anywhere else on the command like (like C:\) suddenly
the jar file can't be found.
I'm not sure what I'm missing here. Am I typing in the command incorrectly?
 
Campbell Ritchie
Marshal
Posts: 80640
472
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mark Justison wrote:. . .
my PATH and my CLASSPATH both point to C:\Users\lib and C:\Users\lib\myJar.jar
. . . Am I typing in the command incorrectly?

That looks incorrect to me. Both look wrong. Your PATH should include the locations of the java.exe file, etc. Is that a system PATH or a user PATH? If it is a system PATH, I would be surprised you can get any programs to run at all. If it is a user PATH you should simply delete it. Run the following commands at the command line and tell us what the results are:-

echo %PATH%
echo %CLASSPATH%

You should usually set a system PATH to include your Java® installation folder towards its beginning, and there is usually no need to include the lib folders because they don't contain .exe files.
You should not set a permanent CLASSPATH, because it will vary from program to program. If you set a CLASSPATH and it doesn't contain the current directory which is what the .; means, you won't be able to access the files you can see with the dir command.
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
PATH plays no part in this, so anything set there can be ignored.

The priority Java uses for classpath definitions is as follows.

With the java command:
If -jar is used then the classpath defined in the jar file manifest is always used, no matter whether you supply a -cp or have a CLASSPATH defined.
If -cp is used then that classpath is used, and any CLASSPATH variable is ignored.
If a CLASSPATH is defined then that is used.
If none of the above then the default '.' (current directory) is used.

All bar the first one also apply to javac.
 
Mark Justison
Ranch Hand
Posts: 31
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
my PATH contents are:


My CLASSPATH contents:


All I was intending to do was keep any outside jars in a specific directory that I can use without having to explicitly call them when compiling and running in the command line. I figured this was an easy and well documented thing but I'm having a heck of a time trying to figure it out.

Thanks again for all your help!
 
Saloon Keeper
Posts: 28663
211
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
PATH is NEVER searched for java classes, whether in files or in JARs. The only java-related thing that should be in PATH is the location of the java bin directory.

CLASSPATH is only searched under the circumstances described by Dave Tolls.

Your CLASSPATH needed to be something like "C:\Users\mjust_000\Java\lib\jtidy-r938.jar;<new place>"

That is, if you use a classpath, it needs to point to each jar file (Java does not search directories of JARs) AND it needs to point to your application's classes. It will not mix and match class paths from multiple specifications.
 
Mark Justison
Ranch Hand
Posts: 31
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got it.
Dang it it was right in front of me the whole time.

I needed to include both the path that my .class file was in and the path that my jar file was in separated by a ;
c:\java -cp c:\myClassLocation;c:\myJarLocation\* myJavaClass

For whatever reason I was stuck on the idea that the jar file had to be included using the '-jar' command and not the '-cp' command. Thanks Dave for clearing that up the difference for me.

Thanks again everyone for you help.
 
Tim Holloway
Saloon Keeper
Posts: 28663
211
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yup. The "-jar" option is completely different. It causes the classpath to be set to the named JAR and only that jar. Nothing else, including jars within that jar are searched for classpath resources.

AND, the JAR is examined for a file named META-INF/manifest.mf containing a "Main-class" specification which defines the fully-qualified classname (that is, including the package name) that contains the Main() method that will be executed.

Because the meaning of the "-jar" option - loosely speaking - is "Execute this JAR".

It might seem limiting that the -jar option can only look for class resources within itself, but that's when custom classloaders get added to the jar code to permit things like looking in database driver jars and other external places and looking for jars-within-the jar. Tools like Maven make building such enhanced apps fairly easy.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic