• Post Reply Bookmark Topic Watch Topic
  • New Topic

Difference in classpath result between javac and java  RSS feed

 
Joe Lemmer
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,

I'm using a third party library which is packaged as a jar in some code that I'm writing. I put the jar file in the classpath environment variable which allowed me to compile my TestJar class (which used the 3rd party library).

However, when I came to run my TestJar class using java, I got an exception that said that it couldn't find a class TestJar. I was running my program from the command line having cd'd into the directory that the TestJar file was in. I solved my problem by putting the folder that TestJar is in in the classpath, but I don't understand why that is necessary. When I run any other java class that I'm playing around with, I don't have to put it in the classpath and surely that will be using other libraries such as java.util etc.

Grateful for any help.

Thanks

Joe
 
Wim Vanni
Ranch Hand
Posts: 96
Eclipse IDE Java Oracle
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Part of your answer is here:
In the CLASSPATH, you do not need to specify the location of normal J2SE packages and classes such as java.util or java.io.IOException.


Could it be that your TestJar class was part of a package?

Wim
 
Joe Lemmer
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Wim,

Thanks for your answer, but no my class wasn't a part of a package, so it should have the default package.

I understand that when the javac or java commands are used, then any required classes are looked for first in the standard files that come as standard with a java installation and then in the locations specified by the classpath. So I've set the classpath for the jar and it compiles fine, but when I try to run it with the 'java' command it can't seem to find my TestJar class. How come it could find it when using the 'javac' command I wonder?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe Lemmer wrote:How come it could find it when using the 'javac' command I wonder?

It couldn't. When you ran the javac command you were creating the class file - it didn't exist at that point.
When you run java you need to include the location of your class file in the classpath so that it knows where to find it.
 
Joe Lemmer
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cool. Thanks Joanne. But how come I don't have to specify a classpath if I'm just running a simple program normally. If I'm just writing some code, then I can normally just write a class, compile it and run it without specifying any classpaths. I understand why I need to put the jar in the classpath, but how come in this situation I have to put my TestJar class in the classpath as well?

Thanks
 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you don't specify a classpath, then it defaults to the current directory. However, if you do specify a classpath, it doesn't include the current directory automatically.
 
Joe Lemmer
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see. Thanks for that. I didn't realise that I had to do that even when I had set the classpath via the environment variable, rather than at the command line.

I have included:



in the classpath environment variable so it now always looks in the current directory.

Many thanks

Joe
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!