LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-18-2012, 11:01 AM   #1
Linux80209
LQ Newbie
 
Registered: Sep 2012
Posts: 4

Rep: Reputation: Disabled
First Java Program (NoClass)


Hello!

I just now got into java and seemed of gotten stuck before I could even get started.

I went through a basic "Hello World" tutorial but I can’t seem to run the program. When I run it from the command prompt I get a NoClassDefFoundError. I have tried to unset the java class with the command “unset CLASSPATH” but I keep getting the same error. Any ideas? I’m sure its an easy fix.

Here is what I have tried:

sean@CrunchBang:~/java$ dir
HelloWorldApp.class HelloWorld.java
sean@CrunchBang:~/java$ CLASSPATH=/home/sean/java
sean@CrunchBang:~/java$ echo $CLASSPATH
/home/sean/java
sean@CrunchBang:~/java$ dir
HelloWorldApp.class HelloWorld.java
sean@CrunchBang:~/java$ java HellowWorld.java
Exception in thread "main" java.lang.NoClassDefFoundError: HellowWorld/java
Caused by: java.lang.ClassNotFoundException: HellowWorld.java
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:334)
Could not find the main class: HellowWorld.java. Program will exit.
sean@CrunchBang:~/java$

Thanks

Last edited by Linux80209; 09-18-2012 at 11:15 AM.
 
Old 09-18-2012, 11:25 AM   #2
sackboy
LQ Newbie
 
Registered: Sep 2012
Posts: 20

Rep: Reputation: Disabled
Hi,

Check your code to see if you misspelled anything.

"Could not find the main class: HellowWorld.java. Program will exit."
 
Old 09-18-2012, 11:32 AM   #3
Linux80209
LQ Newbie
 
Registered: Sep 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
Ha, oops. I wish that was the problem but here is the error I'm getting:

sean@CrunchBang:~$ CLASSPATH=/home/sean/java
sean@CrunchBang:~$ echo $CLASSPATH
/home/sean/java
sean@CrunchBang:~$ cd java
sean@CrunchBang:~/java$ dir
HelloWorldApp.class HelloWorld.java
sean@CrunchBang:~/java$ java HelloWorld.java
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld/java
Caused by: java.lang.ClassNotFoundException: HelloWorld.java
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:334)
Could not find the main class: HelloWorld.java. Program will exit.
sean@CrunchBang:~/java$ dir
HelloWorldApp.class HelloWorld.java
sean@CrunchBang:~/java$

Last edited by Linux80209; 09-18-2012 at 11:41 AM.
 
Old 09-18-2012, 12:21 PM   #4
sackboy
LQ Newbie
 
Registered: Sep 2012
Posts: 20

Rep: Reputation: Disabled
Did you mean to run the following?

$ javac HelloWorld.java

then run

$ java HelloWorld
 
Old 09-18-2012, 01:09 PM   #5
Linux80209
LQ Newbie
 
Registered: Sep 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
I did, I believe this is when the .class file is generated. I'm not sure why it's not finding it when the program is run:


sean@CrunchBang:~/java$ javac HelloWorld.java
sean@CrunchBang:~/java$ java HelloWorld
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld
Caused by: java.lang.ClassNotFoundException: HelloWorld
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:334)
Could not find the main class: HelloWorld. Program will exit.
sean@CrunchBang:~/java$
 
Old 09-18-2012, 01:26 PM   #6
SecretCode
Member
 
Registered: Apr 2011
Location: UK
Distribution: Kubuntu 11.10
Posts: 562

Rep: Reputation: 102Reputation: 102
In your first post
Quote:
Originally Posted by Linux80209 View Post
Code:
sean@CrunchBang:~/java$ dir
HelloWorldApp.class  HelloWorld.java
the class has a different name.

Try again, compiling from scratch, and don't bother with the CLASSPATH variable - it will look in the current directory automatically.

If it still fails, post your .java code.

Please use [code][/code] tags for all code and command output.
 
1 members found this post helpful.
Old 09-18-2012, 01:45 PM   #7
Linux80209
LQ Newbie
 
Registered: Sep 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
Awesome! After restarting from scratch and recompiling the code, it worked! Thanks for everyone's help.

sean@CrunchBang:~/java$ dir
HelloWorld.java
sean@CrunchBang:~/java$ javac HelloWorld.java
sean@CrunchBang:~/java$ dir
HelloWorld.class HelloWorld.java
sean@CrunchBang:~/java$ java HelloWorld
Hello, World
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to connect a jsp program with a java program? aparna.v Linux - General 1 05-01-2010 11:26 AM
Please help with my Java program filsed Programming 1 05-02-2005 04:58 AM
Stop java program(threaded program..should end cleanly) rmanocha Programming 4 11-09-2004 10:36 AM
java error NoClass Def FoundError - SAPGui - dummy help please jburford Linux - Software 5 01-20-2004 10:04 PM
Java Program merlin371 Linux - Software 11 01-18-2004 05:09 PM


All times are GMT -5. The time now is 10:48 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration