Java noob: Program runs in NetBeans, but not from shell.

Relli

Weaksauce
Joined
Jan 16, 2001
Messages
98
I'm very new to Java, but a seasoned C programmer. I wrote very simple multi-threaded client-server chat program. It works perfectly once in NetBeans, but after the server makes a new thread to interact with the client, the parent thread quits listening.
I figured this was a problem that would be easier to debug from outside the IDE. I tried to run the class file NB created, but I get this error:

Exception in thread "main" java.lang.NoClassDefFoundError: Assignment1

I'm sure this is just something easy I'm missing. I googled a little and read something about the CLASSPATH env variable. I echo'd mine and it wasn't set.

Does that sound like it could be the problem? What should it be set to on Linux with standard NB and Java installation?
 
Yeah, all that is saying is that java can't find the class. You do need to set your classpath so that java knows where to find the class you are trying to run. My classpath is set just set to "." (current directory).
 
I set the CLASSPATH to ., but I figured out I typed one letter wrong. Then just kept using the command history every time I tried it. Stupid command history without magic spell check.

Now, I get about the same error:

Exception in thread "main" java.lang.NoClassDefFoundError: Assignment1 (wrong name: wwwassignment1/Assignment1)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
at java.net.URLClassLoader.access$100(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)


but a lot more of them. I still think this is something simple because it mostly works in NB. Anything else I have to check? Should I try and get the classes that NB created to run, or should I try the original *.java file. Also, it's a package so should I be up one directory from the class files?
 
I think I got it for now. It had to do with the package line in my code. I don't really understand packages yet, but I'll google that later.
I was trying to compile and run from inside the package directory. I just commented out the package line and decided I would worry about it later.

Also, my multi-threaded part does work. It just wasn't working from within NetBeans.

Hopefully this post will help some other java noobie some time.
 
are you executing the file correctly?


javac Assignment1.java to compile

java Assignment1 to run (not Assignment1.java)
 
I was. I think it is because I was trying to execute the class files from within the directory that contained them, but I had a package statement that pointed to a directory up. I just took out the package statement and it worked.
 
Back
Top