Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception in thread "main"

 
Rodney Knight
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am extremely new to Java.... I have type in some of the code examples I have and always get the following error:
Exception in thread "main" java.lang.NoClassDefFoundError

I googe this and got some things about setting my classpath and so on, but I really have no idea what that menas. Any help would be appreciated.

The code I have tried is:
class Shuffel1 {
public static void main (String []args) {
int x=3;
while (x>0) {
if(x>2) {
System.out.print("a");
}
x=x-1;
System.out.print("-");
if(x==2) {
System.out.print("b c");
}
if(x==1) {
System.out.print("d");
x=x-1;
}
}
}
}
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch!

I assume that this compiled without errors and you have a Shuffel1.class file in the directory. When you run the code, try using a classpath flag for the current directory (denoted by a dot ".").

>java -cp . Shuffel1

If this works, then the problem is you have a system CLASSPATH set and it does not include a dot for the current directory. The best approach is often to remove the system CLASSPATH entirely. But if it contains something you need, then just add a dot to it. (If you need help with this, be sure to tell us what operating system you're running.)
 
Rodney Knight
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes, Shuffel1.class is a file in the directory where I am trying to run the program from. That file is created after compiling, correct? The code compiles with no errors. I tried the "java -cp. Shuffle1", but that gave me:

Unrecognized option: -cp.
Could not create the Java virtual machine.

I am using Windows XP Professional 2002 SP2 and we installed the jdk1.6.0 download from Sun on Monday.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rodney Knight:
...I tried the "java -cp. Shuffle1"...

Make sure there's a space between -cp and the dot.

(Also, check the spelling: "Shuffle1" vs. "Shuffel1".)
[ September 12, 2007: Message edited by: marc weber ]
 
Tony Docherty
Bartender
Posts: 2958
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried the "java -cp. Shuffle1"
You need a space between the -cp and the fullstop. Also you've typed Suffle1 instead of Shuffel1
 
Rodney Knight
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by marc weber:

Make sure there's a space between -cp and the dot.

(Also, check the spelling: "Shuffle1" vs. "Shuffel1".)

[ September 12, 2007: Message edited by: marc weber ]


Thanks. That worked. So, since it worked, how do I fix the problem so that I don't always have to type the '-cp .'? I don't have a CLASSPATH in my envionmental variables. I typed the name wrong in the email only, it was the '-cp .' that worked.
[ September 12, 2007: Message edited by: Rodney Knight ]
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If there is no specified classpath, Java's default is to look in the current directory. So I would look again at your Environment variables.

In Windows, use Start > Settings > Control Panel > System. Under the "Advanced Tab" of System Properties, click on the button "Environment Variables." There should be "User variables" and "System variables." Check both of these for a CLASSPATH. If there it's there, post its complete value here and we'll go from there.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15284
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rodney Knight:
I don't have a CLASSPATH in my envionmental variables.

Are you sure? What do you see if you type the following command in a command prompt window:

echo %CLASSPATH%

If you don't have CLASSPATH set and you're using Java 5 or newer, then Java should automatically look in the current directory for classes and the "-cp ." should not be necessary. I suspect that you do have the CLASSPATH variable set somewhere, even though you think you haven't.
 
Rodney Knight
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jesper Young:

Are you sure? What do you see if you type the following command in a command prompt window:

echo %CLASSPATH%

If you don't have CLASSPATH set and you're using Java 5 or newer, then Java should automatically look in the current directory for classes and the "-cp ." should not be necessary. I suspect that you do have the CLASSPATH variable set somewhere, even though you think you haven't.


This returned a path for a test folder I set on the desktop. Obviously I set it somehow, how would I correct that, or delete it, or reset it to the original setting?

Thanks for all the help.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by marc weber:
...In Windows, use Start > Settings > Control Panel > System. Under the "Advanced Tab" of System Properties, click on the button "Environment Variables." There should be "User variables" and "System variables." Check both of these for a CLASSPATH. If there it's there, post its complete value here and we'll go from there.
 
Rodney Knight
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by marc weber:
[QB][/QB]


OK. The classpath does not exist either in the User or System variables. The CLASSPATH that came from the echo command above yielded a directory that would have been set when I tried using NetBeans... that was the only place I ever typed that path in.

UPDATE::: I uninstalled NetBeans adn then Re-installed it and the CLASSPATH was reset. I did the ECHO and and it only return %CLASSPATH%. I ran my simple program and it ran w/o the '-cp .' in the command line.

I'll not use NetBeans until I understand more about what is going on.

Thanks for all the help.
[ September 12, 2007: Message edited by: Rodney Knight ]
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rodney Knight:
...I'll not use NetBeans until I understand more about what is going on...

Yeah, IDEs (like NetBeans) can complicate things when you're starting. Glad to hear it's working!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic