Click to See Complete Forum and Search --> : simple classpath problem (java)


vasah20
01-29-2002, 02:16 AM
ok. this is insanely stupid, but I need your help.

I can't, for the life of me get JDK to play well when I set a classpath. I'm doing something real simple for school, and setting up the environment is killing me! God knows I can't ask my teacher (
me: can you help me set up JDK on my Drake 8 box?
him: <blink> uhh.. )

Anyways, I have to use some stupid cs1.jar file (he won't let me use BufferedReader and BufferedWriter), and I can't get the darn thing to work.

>CLASSPATH=~/dev/java/jars/cs1.jar
>set CLASSPATH=CLASSPATH
>export CLASSPATH
>javac assign2.java
>java assign2
Exception in thread "main" java.lang.NoClassDefFoundError: assign1

here's my code... real simple, so I know I'm setting up something wrong.

import cs1.Keyboard;

class assign2 {
public static void main(String args[]) {
int myInt;
System.out.print("Let's read an int: ");
myInt = Keyboard.readInt();
System.out.print("\nThe int is " + myInt);
}
}

I know it's in my setup, because I can't even run my assign1 class, after setting the classpath. (assign1 is just a series of .println).
Another verification is that if I open up another term window, I can run assign1 no problem, but my assign2 can't find the Keyboard class (as would be expected).

so - what am I doing wrong here?
TIA
leo

tnordloh
01-29-2002, 03:15 AM
I don't know the full solution, but you need to just put the name of the directory, not the actual filename, I believe. Also, you need to make sure the current directory is listed in the path, so like this

export CLASSPATH=.;~/mylibrary

the . of course refers to the current directory, whatever it is.

Also, the simplest way to make sure stuff links properly is to just put the files you want to link in your current working directory.

Hope this helps...

bwkaz
01-29-2002, 09:34 AM
Originally posted by vasah20:
>CLASSPATH=~/dev/java/jars/cs1.jar
<STRONG>&gt;set CLASSPATH=CLASSPATH</STRONG>
&gt;export CLASSPATH

The problem is that second command. First of all, you don't need it. You can just export CLASSPATH=.:~/dev/java/jars/cs1.jar, all in one nice compact (well mostly) command. But the reason your java stuff isn't working given the sequence of commands above is that the second command sets the CLASSPATH variable to the text "CLASSPATH". This is obviously not what you wanted it to do.

Like tnordloh said, you want the current directory in there too, but don't separate the directories with semicolons. Use colons.

Stuka
01-29-2002, 10:57 AM
tnordloh-
When you want to use classes inside a .jar file, you do need the file listed in the classpath (the exception being Java extensions, but that's another topic).

vasah20-
Other than what I just mentioned, tnordloh is right - your killing your CLASSPATH assignment. One way to test environment variables is to just print them out in the terminal window after you've set them.

vasah20
01-29-2002, 12:03 PM
thanks guys it works now... I was missing the .: in my classpath

one thing though: like stuka mentioned, I was checking my $CLASSPATH variable after creating the statements, and getting the correct value.

ex:
&gt;CLASSPATH=.:~/dev/java/jars/cs1.jar
&gt;set CLASSPATH=CLASSPATH
&gt;export CLASSPATH
&gt;echo $CLASSPATH
.:~/dev/java/jars/cs1.jar

I use a bash shell, so is this bash specific, or is it my Mandy 8.0 box with it's customized newbie helpfulness rearing it's ugly head again?

Dru Lee Parsec
01-29-2002, 02:18 PM
&gt;CLASSPATH=.:~/dev/java/jars/cs1.jar
&gt;set CLASSPATH=CLASSPATH
&gt;export CLASSPATH
&gt;echo $CLASSPATH
.:~/dev/java/jars/cs1.jar



You could clean it up this way.
export CLASSPATH=.:~/dev/java/jars/cs1.jar

&gt;echo $CLASSPATH
.:~/dev/java/jars/cs1.jar

The "SET" command doesn't do anything in Linux. Set is for Windows; export is for Linux.

Also, here are some things that may help.

To add a jar file to your classpath then add the full file name like this:


export CLASSPATH=$CLASSPATH:~/dev/java/jars/cs1.jar


This is what you did and it was correct. To add all the class files in a directory you just do this:

export CLASSPATH=$CLASSPATH:~/myjava/lib

Notice how I used $CLASSPATH to append the directory to the already existing classpath.

Now here's the cool part. Let's say you have classes that are in a package called com.foo.gui So you may have a splash screen class in that package called com.foo.gui.MySplashScreen

When you compile that class it will make a directory structure like this:

com/foo/gui

In that directory will be your file called MySplashScree.class

Now, if you build your projects such that the "com" directory is just below your "lib" directory such that you have a directory structure like this:

~/myjava/lib/com/foo/gui

Then by pointing your classpath to the ~/myjava/lib directory the JVM will automatically convert that the package name into a directory name and it will correctly find your MySplashScreen.class file.

This means that if you put all your classes in packages then you need only to add one directory to your classpath to allow the JVM to "see" them all.

So, what I do is I have a directory called "src" for my source code and a directory called "lib" where all my code compiles to. I use an ant build file to read all my files from the src directory and compiles them to the lib directory. It works great.

If you want to see a project built this way as an example feel free to go to tapper.sourceforge.net and download the latest code snapshot (the code download page is here https://sourceforge.net/project/showfiles.php?group_id=10887 ) Follow the instructions in the zip file (which basically tell you how to install the ant build tool) and you'll see a fully working example that demonstrates this.

You're also welcome to use the build.xml file for ant and modify it to any of your projects. It's all Open Source stuff so go have fun with it.

Hope it helps. Cheers! :cool: