Skip to content
This repository has been archived by the owner on Jul 13, 2022. It is now read-only.

Could not find or load main class #406

Closed
anuj200199 opened this issue Jan 22, 2020 · 9 comments
Closed

Could not find or load main class #406

anuj200199 opened this issue Jan 22, 2020 · 9 comments

Comments

@anuj200199
Copy link

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
Copy link
Contributor

Could it be this problem?

spring-projects/sts4#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
Copy link

bberto commented Jan 24, 2020

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
Copy link

bberto commented Jan 24, 2020

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
Copy link
Contributor

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:

  • 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?

@martinlippert
Copy link
Contributor

@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
Copy link

bberto commented Jan 27, 2020

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
Copy link
Contributor

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
Copy link

bberto commented Jan 28, 2020

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
Copy link
Contributor

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests

4 participants