Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

javac works, java doesn't

 
Bob Grossman
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 64700
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post the Test.java file (please UseCodeTags).
 
Darryl Burke
Bartender
Posts: 5125
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does your classpath include "." (the current working directory)?
 
Wim Vanni
Ranch Hand
Posts: 96
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This recent thread handles about the same topic.

Cheers, Wim
 
Bob Grossman
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try commenting out the classpath in the bash profile; you can always replace it if there are any problems.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic