aspose file tools*
The moose likes IDEs, Version Control and other tools and the fly likes Could not find the main class. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » IDEs, Version Control and other tools
Bookmark "Could not find the main class." Watch "Could not find the main class." New topic
Author

Could not find the main class.

Michael Sampson
Ranch Hand

Joined: Sep 24, 2008
Posts: 30
I am in the process of learning spring. I was using a book and had downloaded their example code. The first 2 examples worked fine. Simple imports into eclipse. The 3rd appeared to work fine. I imported, there are no errors. I select the main class and try to run it as a java application and a Java Virtual Machine Dialog pops up that states, Could not find the main class. Program will exit. I've done some digging on this and most folks seem to think it is involved with a run configuration setting. I double checked the class names, package statement and compared it to what is in Run Configurations. What is listed as the Main class has the fully qualified class name and that class has a public static void main(String[] args) method. I will say this project came with a maven build file. I have not learned much with Maven. I read in javaranch to ditch eclipse when first learning maven and after that I don't remember what was problem that killed that learning effort while working at the command line. I tried changing the jre. Nothing there. In the console window all I get is,
java.lang.NoClassDefFoundError: com/springinaction/springidol/SpringIdolMain
Exception in thread "main"

I am wondering if there is an eclipse setting I need to alter. I can find the .class file under a bin folder in my file system. It's like it can't find it.

Mike
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Almost certainly a run configuration problem--without more info it'll be hard to help though :(
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16070
    
  21

In Eclipse, when you set up a Run/Debug configuration for a Java Application, one of the dialog items you should have to supply is the Main Class. The dialog should display an error if that class cannot be located within the project's defined classpath (set up in the project's Properties/Build dialog).

First thing I'd do is verify that this classpath is correct. Out of the box, Eclipse likes to put its compiled classes in a <project root>/bin directory. Maven, however, likes to compile into <project root>/target/classes.

You can Eclipse-ize a Maven project by running the Eclipse goal on it:



IntelliJ users will be glad to know that they have a similar option.

It's best if you do this while Eclipse is not running. You may need to delete any existing ".classpath" and ".project" files from your project root for maven to build the new (and supposedly correct) ones.


Customer surveys are for companies who didn't pay proper attention to begin with.
Michael Sampson
Ranch Hand

Joined: Sep 24, 2008
Posts: 30
So Tim's idea definitely helped. I closed eclipse. Then at the command line where the project was which did come with the maven pom.xml file, I ran mvn intall and it worked (built fine and executed correctly) at the command line. When I looked in the file directory, yesterday when I was doing this all the class files ended up under /bin. The mvn install put class files under /target/classes. When I restarted eclipse, eclipse was then able to find the class with main. So eclipse ide was building the class files and putting them under /bin but when it went to run main, it was looking in /target/classes. I looked under the run configurations and the classpath folder and it does not look like you can alter whether the jvm looks in /bin or /target/classes. Now I just did some research and it looks like may be you can set buildOutputDirectory to bin in the pom.xml so may be java would have found the main class there.

Dave Newton, looking at your stats there, if you started doing this last year, you would have to be doing at least 8 of these posts a day for close to a year. Wow. Is that your full time job? I would have burned out after the 200th.

Mike
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16070
    
  21

Forget the bin directory. Maven will just fight you if you don't use target/classes. It's easier to make Eclipse follow Maven's requirements than go overridding Maven. A Maven Eclipse project doesn't even need a bin directory.

On the Java Build Path Project Properties dialog, Select the Source tab. Set the source directory to src/main/java. Set the Default output folder to target/classes. Bang! you're done! The Eclipse built-in compiler will now compile to the same place a standalone Maven build does and the debugger will find your compiled classes. Including the Main class.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Could not find the main class.