[FIXED]ClassNotFoundException on any Java program in Eclipse

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

User avatar
Squid Tamer
Posts: 220
Joined: Fri Apr 03, 2009 3:59 am UTC
Location: Over there
Contact:

[FIXED]ClassNotFoundException on any Java program in Eclipse

Postby Squid Tamer » Tue Dec 28, 2010 11:59 pm UTC

This problem is frustrating me. I've done a lot of searching but still can't find anything helpful. Maybe someone here will know how to fix it.
I'm trying to do a Hello World program in Java, using Eclipse. I've gone so far as to copy and paste the code from a website:

Code: Select all

public class HelloFour {  //'Four' because it's the fourth Eclipse project I've made trying to get this to work.

   public static void main(String[] args) {
      System.out.println("Hello World!");
   }

}


I've tried many different variations on this, so I don't think that the code itself is the problem. Here's what I get if I try to run it:

Code: Select all

Exception in thread "main" java.lang.NoClassDefFoundError:
Caused by: java.lang.ClassNotFoundException:
   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)
Could not find the main class: . Program will exit.

It gives me that no matter what I try. If I copy and paste the same code into a text file, and use the terminal to compile it as a java program, it runs fine. The problem lies somewhere in Eclipse, I guess. While I was searching I found some mildly similar problems (mostly they only had the problem for one program, or were just trying to run a game or something), but the solutions given were pretty random, and the authors rarely came back to say whether it worked or not. The only common answer was that the CLASSPATH was wrong, but being completely new to Java and Eclipse I don't know what that is or how to fix it.

Some info: I'm running Ubuntu 10.04, using Eclipse 3.5.2. When I create a new Java project, it asks me what JRE to use, but I haven't noticed any difference while trying different ones.

Thanks for any help anybody is able to give.
Last edited by Squid Tamer on Sun Jan 09, 2011 7:45 pm UTC, edited 1 time in total.

paramecium
Posts: 20
Joined: Mon Jun 21, 2010 10:58 pm UTC

Re: ClassNotFoundException on any Java program in Eclipse

Postby paramecium » Wed Dec 29, 2010 12:09 am UTC

I quickly copy-pasted this into eclipse, and it ran for me.

I'm not a big java guy, I just use it to mess around on android, but I'll try and help.

What's the structure of your source? I pasted it into a main package in src, with the name HelloFour.java, and added the line package main; to the beginning of the code. Worked fine for me.
[edit]
Capture.JPG
If this helps at all

[/edit]

Code: Select all

//

User avatar
Squid Tamer
Posts: 220
Joined: Fri Apr 03, 2009 3:59 am UTC
Location: Over there
Contact:

Re: ClassNotFoundException on any Java program in Eclipse

Postby Squid Tamer » Wed Dec 29, 2010 12:18 am UTC

I didn't post the structure because I played around with it a lot, but here's two of them. Both get the same results, so I don't think that that's the problem.
This picture pretty much illustrates the entire problem:

Javaprob.png
The problem and two of the 'structures'

Token
Posts: 1481
Joined: Fri Dec 01, 2006 5:07 pm UTC
Location: London

Re: ClassNotFoundException on any Java program in Eclipse

Postby Token » Wed Dec 29, 2010 12:24 am UTC

Have you tried doing a full clean and build?
All posts are works in progress. If I posted something within the last hour, chances are I'm still editing it.

User avatar
Squid Tamer
Posts: 220
Joined: Fri Apr 03, 2009 3:59 am UTC
Location: Over there
Contact:

Re: ClassNotFoundException on any Java program in Eclipse

Postby Squid Tamer » Wed Dec 29, 2010 12:33 am UTC

Token wrote:Have you tried doing a full clean and build?


I have tried Clean, and I think (If I know the proper meaning of build. Yes, I'm one of those clueless Python people) that it does a new build when you click run, doesn't it?
Thanks for the help already, everybody!

User avatar
TheChewanater
Posts: 1279
Joined: Sat Aug 08, 2009 5:24 am UTC
Location: lol why am I still wearing a Santa suit?

Re: ClassNotFoundException on any Java program in Eclipse

Postby TheChewanater » Wed Dec 29, 2010 3:05 am UTC

Code: Select all

cd path/to/project/
javac Hello.java
java Hello


Does this work? If it does, it's a problem with the way Eclipse is configured with the JRE.
ImageImage
http://internetometer.com/give/4279
No one can agree how to count how many types of people there are. You could ask two people and get 10 different answers.

User avatar
Squid Tamer
Posts: 220
Joined: Fri Apr 03, 2009 3:59 am UTC
Location: Over there
Contact:

Re: ClassNotFoundException on any Java program in Eclipse

Postby Squid Tamer » Wed Dec 29, 2010 5:08 am UTC

TheChewanater wrote:

Code: Select all

cd path/to/project/
javac Hello.java
java Hello


Does this work? If it does, it's a problem with the way Eclipse is configured with the JRE.

Yes it does work. The problem lies somewhere in Eclipse. I can compile stuff from the terminal, and I can for sure run Java programs just fine. Just not in Eclipse, it seems. The reason I'm so adamant on using Eclipse is that I'd like to play around with making Android apps, and the best and most standard way to do this is using a good plugin for Eclipse.

I'm going to try installing the latest version from the website, not the Ubuntu repository. I probably should have tried that a while ago.

Edit: No effect.

User avatar
TheChewanater
Posts: 1279
Joined: Sat Aug 08, 2009 5:24 am UTC
Location: lol why am I still wearing a Santa suit?

Re: ClassNotFoundException on any Java program in Eclipse

Postby TheChewanater » Wed Dec 29, 2010 5:31 am UTC

<p class="offtopic">I've done some Android development on the command line. It's not that much of a hassle, since the "android create project" and "emulator @..." commands are included with the SDK.
ImageImage
http://internetometer.com/give/4279
No one can agree how to count how many types of people there are. You could ask two people and get 10 different answers.

User avatar
Berengal
Superabacus Mystic of the First Rank
Posts: 2707
Joined: Thu May 24, 2007 5:51 am UTC
Location: Bergen, Norway
Contact:

Re: ClassNotFoundException on any Java program in Eclipse

Postby Berengal » Wed Dec 29, 2010 5:42 am UTC

You should check eclipse's global classpath, jre and jdk configurations. Also, eclipse puts the .class files it builds in project/bin (unless you changed it), so check to see that those exist and that eclipse actually builds the classes for you.
It is practically impossible to teach good programming to students who are motivated by money: As potential programmers they are mentally mutilated beyond hope of regeneration.

User avatar
phlip
Restorer of Worlds
Posts: 7550
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia
Contact:

Re: ClassNotFoundException on any Java program in Eclipse

Postby phlip » Wed Dec 29, 2010 7:23 am UTC

Squid Tamer wrote:

Code: Select all

Caused by: java.lang.ClassNotFoundException:
Could not find the main class: . Program will exit.

These lines stand out to me, since they usually contains the name of the class:

Code: Select all

phlip@boris:~$ java SomeClassWhatIsNotExist
Exception in thread "main" java.lang.NoClassDefFoundError: SomeClassWhatIsNotExist
Caused by: java.lang.ClassNotFoundException: SomeClassWhatIsNotExist
        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: SomeClassWhatIsNotExist.  Program will exit.

The error you posted is as though you ran java with a blank class name:

Code: Select all

phlip@boris:~$ java ''
Exception in thread "main" java.lang.NoClassDefFoundError:
Caused by: java.lang.ClassNotFoundException:
        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: .  Program will exit.

This suggests that there's a misconfiguration in how Eclipse is launching java...

Code: Select all

enum ಠ_ಠ {°□°╰=1, °Д°╰, ಠ益ಠ╰};
void ┻━┻︵​╰(ಠ_ಠ ⚠) {exit((int)⚠);}
[he/him/his]

snoose
Posts: 15
Joined: Thu Jan 07, 2010 10:04 pm UTC

Re: ClassNotFoundException on any Java program in Eclipse

Postby snoose » Wed Dec 29, 2010 9:39 pm UTC

In my experience this is usually because Eclipse expects the JAVA_HOME environment variable to point to the JDK and you have not done so yet.

Quick how to: http://www.cyberciti.biz/faq/linux-unix ... -variable/

akashra
Posts: 503
Joined: Tue Jan 01, 2008 6:54 am UTC
Location: Melbourne, AU
Contact:

Re: ClassNotFoundException on any Java program in Eclipse

Postby akashra » Fri Dec 31, 2010 11:26 pm UTC

In similar line with above, check that
a) The project debug/launch configuration is using the local/project classpath and that
b) The .class files actually exist where you're expecting.

And example of b, at work we use both an ant target and eclipse generating class files into the same folder. If you do an ant clean and then try to execute, say, a unit test from within eclipse, the class files won't be there, and eclipse doesn't know they don't exist until you do a refresh and a build. This can happen for a number of reasons (including eclipse having not even built in the first place), but start there.

Essentially it's a classpath issue.
( find / -name \*base\* -exec chown us : us { } \ ; )

User avatar
Squid Tamer
Posts: 220
Joined: Fri Apr 03, 2009 3:59 am UTC
Location: Over there
Contact:

Re: ClassNotFoundException on any Java program in Eclipse

Postby Squid Tamer » Sun Jan 09, 2011 7:45 pm UTC

Sorry for dredging this thread back up (It hasn't been too long...), but I think that I have fixed it! I decided to re-attempt running Java in Eclipse, and basically random clicking lead me to figure this out:

If I use 'Run> Run As > Java Application', then it runs perfectly, spitting out the expected "Hello, World"! I'm guessing that this fixes some messed up configuration in the project, because from then on it will run as expected while using the plain 'Run'. This 'fix' would probably be described in the Jargon File as 'Black Magic' :)

There is an extra step when making an Android app. I found that if I went to 'Run > Run Configurations...', and went to the Classpath tab, I saw that the project was not listed under it's own classpath. (This was not the case the vanilla Java projects, oddly enough.) I fixed that, and then ran it with 'Run> Run As> Android Application'. It started up the emulator and ran just fine!

I don't know if this counts as a true fix, but at least things are running! Hopefully this will help anyone that has this problem in the future.


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 12 guests