Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CLASSPATH - Question

Status
Not open for further replies.

munnanext

Programmer
Aug 20, 2004
49
US

I have two Java Files. one is HelloWorld.java and another is HelloWorldSource.java.
I am having these files in

c:\John\java\myPackage.

The CLASSPATH I have is

C:\john\java\myPackage>set CLASSPATH
CLASSPATH=.;C:\POIRA\JAVA\CLASSES;C:\POIRA\JAVA\CLASSES\EXTAPI.JAR;C:\Program Fi
les\DB2\SQLLIB\java\db2java.zip;C:\Program Files\DB2\SQLLIB\java\runtime.zip;C:\
Program Files\DB2\SQLLIB\java\sqlj.zip;C:\Program Files\DB2\SQLLIB\bin;c:\John\j
ava\myPackage

When I compile the file HelloWorldSource.java it is getting compiled. But when I compile the file
HelloWorld.java it is giving me the following error


HelloWorld.java:8: cannot access HelloWorldSource
bad class file: .\HelloWorldSource.class
class file contains wrong class: myPackage.HelloWorldSource
Please remove or make sure it appears in the correct subdirectory of the classpa
th.
HelloWorldSource objHelloWorld = new HelloWorldSource();

Here is the Source for the two files


Source for HelloWorldSource
===========================================

package myPackage;

class HelloWorldSource{

String getHelloWorld(){

return "Hello World";
}


}

Source for HelloWorld
==============================
import myPackage.*;

class HelloWorld{

public static void main(String args){

HelloWorldSource objHelloWorld = new HelloWorldSource();
String myString;

myString = objHelloWorld.getHelloWorld();

System.out.println(myString);
}

}

Appreciate your help.
 
replace the c:\John\java\myPackage by c:\John\java - the myPackage indicates that the class is in the package myPackage, which is the same as the subdirectory myPackage in any of your classpath's directories. Hm, that sounds a lot more complicated than it actually is...

haslo@haslo.ch - www.haslo.ch​
 
I removed myPackage in the ClassPath. But even then
I am getting the following error. An more over the other file which is HelloWorldSource is also not getting compiled now.

Error when I compile HelloWorldSource:
=========================================
C:\john\java\myPackage>java HelloWorldSource.java
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorldSource/java

Error when I compile HelloWorld:
=========================================

C:\john\java\myPackage>javac HelloWorld.java
HelloWorld.java:8: cannot access HelloWorldSource
bad class file: .\HelloWorldSource.class
class file contains wrong class: myPackage.HelloWorldSource
Please remove or make sure it appears in the correct subdirectory of the classpa
th.
HelloWorldSource objHelloWorld = new HelloWorldSource();
^
1 error


Thanks
 
Yes, same thing. If you'd have no package defined, it would work like this, but packages are the way to go, so do the following:
Code:
C:\john\java>javac myPackage/*.java
C:\john\java>java myPackage.HelloWorld
I'd also advise getting a GUI like Eclipse (altough others would not), it makes things like compiling and running programs a lot easier. Or you could look into Apache Ant, it lets you define compilation paths with XML and there's some good tutorials available...

A little more advanced: I normally even have the source and the binaries separate, you can compile it then by going to the binary path and compiling with javac -sourcepath ../src [...] (supposing you have the directories named something like C:\john\java\src and C:\john\java\bin, which is pretty common). Like this, you can also ensure you have a really clean build - by simply wiping out everything in the binary directory. That's also why you can add additional stuff in the classpath, to ensure the librairies are still available if they're in another path.

So, if you have a complicated project with a librairy in the lib path, a source path and a binary path, you'll have something like this:
Code:
C:\john\java\bin>javac -sourcepath ../src -classpath .;../lib/my_lib.jar ../src/package/ClassFile.java
C:\john\java\bin>java -classpath .;../lib/my_lib.jar package.ClassFile
Note that I have used forward slashes (that's the standard with Java, backslashes work only on Windows), that the JRE uses points and not slashes, that I have put the classpath in the command itself (which is not strictly necessary, but makes you independent of the system's classpath) and that the classpath now also contains the library.

However, as I said, with a GUI or Ant you'll never have to worry about what and when to compile again :)

Phew, that was a lot of info :) - just post again if something was not clear.

haslo@haslo.ch - www.haslo.ch​
 
...and of course I did forget something really important. Make that (for the split src and bin paths I suggested)...
Code:
C:\john\java>javac -sourcepath src src/myPackage/*.java -d bin
C:\john\java>java -classpath bin myPackage.HelloWorld
...thus selecting the output path as the current path as well.

If you want to be in the bin directory, that works as well, of course:
Code:
C:\john\java\bin>javac -sourcepath ../src ../src/myPackage/*.java -d .
C:\john\java\bin>java -classpath . myPackage.HelloWorld

haslo@haslo.ch - www.haslo.ch​
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top