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,
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.
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.
An IDE is no substitute for an Intelligent Developer.
Joined: Sep 24, 2008
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.
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.