spring-attic / spring-ide

Spring Development Environment for Eclipse
300 stars 126 forks source link

Could not find or load main class #406

Closed anuj200199 closed 4 years ago

anuj200199 commented 4 years ago

The build is successful while executing maven install and the class files are also created. However, while selecting run as java application or Spring boot App, it shows could not find or load main class. moreover, if jar of the project is exported, it runs fine.

kdvolder commented 4 years ago

Could it be this problem?

https://github.com/spring-projects/sts4/issues/403

If it doesn't look like that's it. You have really not provided enough information for us to guess at the problem. So please provide a sample project and more detailed step-by-step instructions on how to to reproduce the problem.

bberto commented 4 years ago

I have the same problem. With some investigation using "Show command line" function I found out that when the classpath is very long, eclipse uses the @argfile jdk9+ option, and the command line is like this:

C:\path\to\java -Dfile.encoding=UTF-8 "C:\path\to\.temp-project-classpath-arg-1579863034624.txt MainClass

In the generated temp file the classpath includes double quotes (-classpath "C:\path\to\target"). Removing quotes and running from commandline, the main class is found correctly.

I think that in @argfile scenario double quotes should not be included

bberto commented 4 years ago

I found that the bug is documented here: https://bugs.eclipse.org/bugs/show_bug.cgi?id=558495

Has been introduced with this commit https://git.eclipse.org/r/#/c/151582/ in 2019-12 and should be fixed in Eclipse 2020-03.

There's a suggested workaround but didn't work for me

kdvolder commented 4 years ago

Great detective work @bberto . It does sound like that could be the bug responsible. Before closing this ticket as a duplicate of that bug, I think we need to somehow verify that:

a) it is indeed the bug responsible b) the fix put into Eclipse will also fix the bug in STS

So you think you could help me with that by trying to give me some steps to reproduce this bug in STS 4.5.1?

From reading the eclipse bug. I figured out:

Do we need something else in terms of special conditions to cause the problem? E.g. some paths with spaces in them? Or a particularly large project with many dependencies?

martinlippert commented 4 years ago

@bberto Please give the latest STS 4.5.2 CI builds on Eclipse 4.15 a try, they should already include the fix for the Eclipse platform: http://dist.springsource.com/snapshot/STS4/nightly-distributions.html

bberto commented 4 years ago

So you think you could help me with that by trying to give me some steps to reproduce this bug in STS 4.5.1?

From reading the eclipse bug. I figured out:

  • this affects Windows (so need to test on Windows specifically)
  • this affects JDK 9 or later (but not JDK 8)

Do we need something else in terms of special conditions to cause the problem? E.g. some paths with spaces in them? Or a particularly large project with many dependencies?

My setup: windows 10, JDK 11 / JDK 13 (tested both), STS 4.5.0

  1. create a new Java Project with a main class. Don't generate module-info.java.
  2. Add to classpath a lot of dependencies: all jar contained in sts/plugins folder are enough (630 elements).
  3. Create a run configuration. Using Show command line feature you can see that eclipse store the classpath in a temp file, in this way: C:\path\to\javaw.exe -Dfile.encoding=UTF-8 "@C:\path\to\workspace\test\.temp-Main-classpath-arg-1580129448019.txt" test.Main
  4. try to execute the run configuration, and you'll get:
    Error: Could not find or load main class test.Main
    Caused by: java.lang.ClassNotFoundException: test.Main

This is the minimal test case, but it's the same with Spring Boot run configurations. I discovered the issue in a Boot project that includes lots of apache camel deps.

I can confirm that the issue is solved in STS-4.5.2-CI based on eclipse 2020-03.

Also the workaround proposed in linked issue works: you have to manually edit .metadata.plugins\org.eclipse.debug.core.launches run configuration to set org.eclipse.jdt.launching.ATTR_USE_CLASSPATH_ONLY_JAR=true

kdvolder commented 4 years ago

This is the minimal test case, but it's the same with Spring Boot run configurations

My main concern is that we need to confirm the fix in Eclipse which is specifically addressing the problem for plain Java projects also fixes the issue for boot projects/launches. This is probably the case but not guaranteed because "Run As Boot App" is a separate launch configuration implementation. It inherits some functionality from the Java implementation but it is not identical.

I can confirm that the issue is solved in STS-4.5.2-CI based on eclipse 2020-03.

Did you confirm this with a "Run as Boot App" launch, or only with the 'minimal' plain Java scenario?

bberto commented 4 years ago

Performed the same test with a minimal Spring Boot project, including just an ApplicationRunner class that prints Hello World and all the eclipse plugin folder imported in classpath.

"Run as Boot App" launch fails in STS-4.5.0 and works in STS-4.5.2-CI

kdvolder commented 4 years ago

@bberto Thanks a lot for confirming that. We can close this ticket now.