• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

Java ClassPath Problems

calhoun

Gawd
Joined
Jan 20, 2003
Messages
608
Hi

I am trying to get Java to work (not easy I know)

Assuming i'm in ~/Java
which has src, javadoc and classes

So I ran:
classpath=./classes/
CLASSPATH=./classes/
( both echo out ./classes/ now )

I make a Test1 directory in src and a file Address.java in src/Test1

this has
Code:
package uk.co.jsa3d.AP3.Test1;
public class Address {

public static void main(String[] argv) {

}

}

So I run javac -d ./classes/ src/Test1/Address.java
No errors, so one would imageine it worked.

Now, to test it works i issue java Address
expecting to to to the next prompt, but instead it yeilds:
Exception in thread "main" java.lang.NoClassDefFoundError: Address

So I look to see if it exists, and sure enough ./classes/uk/co/jsa3d/AP3/Test1/Address.class exists
So adding -classpath ./classes/ so it knows where the classpath is should fix it, or at least thats what I would've thought but it doesnt . If I give it the full classpath to the Test1 directory, it changes its complaint to Wrong Name.
#Exception in thread "main" java.lang.NoClassDefFoundError: Address (wrong name: uk/co/jsa3d/AP3/Test1/Address)
So the only way I can execute it is to give it the full class name and the classpath:
Code:
java -classpath ./classes/ uk.co.jsa3d.AP3.Test1.Address
So, is it just me or does this not somewhat defeat the point in classpath and packages ?
How can i get it so all I need to do is run java Address and it'll execute properly? (Granted, at present it wont attually do anything but it should execute it)
 
If you have a package called "uk.co.jsa3d.AP3.Test1", common practice is to place its classes in a directory tree like "src/uk/co/jsa3d/AP3/Test1". So, your source code would be located in "src/uk/co/jsa3d/AP3/Test1/Address.java". As you've found out, this isn't strictly a requirement, javac will create the dir structure for its output, but it's pretty much what everyone does.

Personally, I don't recommend setting the classpath env var. It may make things a bit more of a pain from the command line, but classpath evaluation seems to cause an undue amount of grief so I always like to be explicit about what to use.

Assuming the previously mentioned dir structure, and assuming you're currently at the same level as your src and classes dirs:

javac -d classes -classpath classes -sourcepath src src/uk/co/jsa3d/AP3/Test1/Address.java

java -cp classes uk.co.jsa3d.AP3.Test1.Address

As you've also discovered, you have to use the fully-qualified name of the class. The classpath just defines a set of "roots" for locating classes. If you really want to just run "java Address", then remove the package statement from the source and put the file directly into the src dir. Then do:

javac -d classes -classpath classes -sourcepath src src/Address.java

java -cp classes Address
 
Back
Top