• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java lang error  RSS feed

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I am trying to run a java file from command line and i get a lang error.

My environment variable is set:

Classpath
C:\Program Files\Java\jre6\bin

Why is the error happening?
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, Eric!

Please note that ItDoesntWorkIsUseless(⇐click). You need to TellTheDetails(⇐click). For instance, paste in the exact, complete error message, along with the code that caused it, and indicate clearly which line was the culprit.

Also note that precision is very important. A "lang error" is not a standard term. Perhaps you meant java.lang.Error?
 
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's the exact error? Please TellTheDetails.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Woo hoo! I beat Rob! What do I win?
 
Eric Rounding
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I type: java GreetingsUniverse.class

Error:
Exception in thread "main" java.lang.NoClassDefFoundError: GreetingsUniverse/class
Caused by: java.lang.ClassNotFoundException: GreetingsUniverse.class
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown source)
Could not find the main class: GreetingsUniverse.class. Program will exit.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eric Rounding wrote:I type: java GreetingsUniverse.class

Error:
Exception in thread "main" java.lang.NoClassDefFoundError: GreetingsUniverse/class
Caused by: java.lang.ClassNotFoundException: GreetingsUniverse.class


That tells us that you did something like
java -cp . GreetingsUniverse.class
when you should have done something like
java -cp . GreetingsUniverse

Java takes the class name, and then adds ".class" and searches for a file with that name that defines the class in question. And dots in the class name are used to specific a package, which end up corresponding to directories when searching for the .class file. So in short: Just provide the class name, without the ".class" extension.

Make sure you're following the instructions in your book or tutorial exactly. You may also want to check out http://docs.oracle.com/javase/tutorial/getStarted/cupojava/index.html if you haven't already. It provides pretty complete and precise instructions to get you started.
 
Eric Rounding
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



I type: java -cp . Bun

I get:
Exception in thread "main" java.lang.NoSuchMethodError: main
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eric Rounding wrote:


I type: java -cp . Bun

I get:
Exception in thread "main" java.lang.NoSuchMethodError: main


Which of these are false?

1. You first did javac Bun.java, AFTER your last edit to the above file.

2. The Bun class is not in a package. That is, Bun.java does NOT start with package something_or_other;

3. In your current directory, there exists Bun.class.
 
Eric Rounding
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All three of the above were true.

I amended enironment variables:

Classpath
C:\Program Files\Java\jre6\bin

PATH
C:\Program Files\Java\jdk1.6.0_30\bin

And all my class files are able to be run now.

perhaps it was that I had set up the compiler but not the interpreter (to run) perhaps. Does one of the above refer to the compiler and one refer to the interpreter?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eric Rounding wrote:All three of the above were true.

I amended enironment variables:

Classpath
C:\Program Files\Java\jre6\bin

PATH
C:\Program Files\Java\jdk1.6.0_30\bin


No, it was something else. There is no way these changes would affect the above. For one thing, if you use -cp on the command line, it supersedes the CLASSPATH env var. For another thing PATH has nothing to do with finding classes, only with finding executables, and since you were able to get that error, it definitely found and executed the java VM executable.

Also, you should generally not set the CLASSPATH env var at all, and you should definitely not set it to include anything in your Java installation directory tree.

You must have changed something else as well. I'm glad it works now, but it would have been nice to figure out what you were doing wrong so that you won't have to guess next time it happens.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:Woo hoo! I beat Rob! What do I win?

Eternal glory.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:
Jeff Verdegan wrote:Woo hoo! I beat Rob! What do I win?

Eternal glory.


Oh. I was kind of hoping for a Sham-Wow, but that's nice too.
 
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
Rob Spoor wrote:
Jeff Verdegan wrote:Woo hoo! I beat Rob! What do I win?

Eternal glory.


Oh. I was kind of hoping for a Sham-Wow, but that's nice too.


Eternal glory expires after 15 minutes, you know.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!