wood burning stoves
The moose likes Beginning Java and the fly likes javac works, java doesn't Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "javac works, java doesn Watch "javac works, java doesn New topic

javac works, java doesn't

Bob Grossman
Ranch Hand

Joined: Dec 18, 2008
Posts: 69
I have a short program called Test.java that depends on classes in a jar file.

I put the jar file in the CLASSPATH variable in my .bash_profile file. I type,

javac Test.java

and, like magic, a Test.class file appears in the directory. So then I type,

java Test

and I get this nonsense:

Exception in thread "main" java.lang.NoClassDefFoundError: Test
Caused by: java.lang.ClassNotFoundException: Test
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: Test. Program will exit.

I have tried running java using the -classpath variable with the necessary jar file; no joy.

I'm sure the solution must be very simple, but everything I see about the java program tells me that "java Test" should work.

Help is most appreciated.
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63221

Please post the Test.java file (please UseCodeTags).

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Darryl Burke

Joined: May 03, 2008
Posts: 4997

Does your classpath include "." (the current working directory)?

luck, db
There are no new questions, but there may be new answers.
Wim Vanni
Ranch Hand

Joined: Apr 06, 2011
Posts: 96

This recent thread handles about the same topic.

Cheers, Wim
Bob Grossman
Ranch Hand

Joined: Dec 18, 2008
Posts: 69
Thanks for your answers. My classpath variable indeed did not include the current directory. I added ".:" to the front of my CLASSPATH variable in my bash profile, and now my java program works.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 44013
Why have you got a CLASSPATH in your bash profile in the first place? Setting a system classpath often does more harm than good.
Bob Grossman
Ranch Hand

Joined: Dec 18, 2008
Posts: 69
I dunno. Someone created that profile for me when I was just getting started. I assume it's useful for some of the other stuff I do.

Most of the time, when I compile Java code, I use a Makefile that someone else wrote, so it's not usually an issue.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 44013
Try commenting out the classpath in the bash profile; you can always replace it if there are any problems.
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
subject: javac works, java doesn't
It's not a secret anymore!