简体   繁体   中英

Cannot “Run File” from src\test folder -> main class not found or could not be loaded

I simply want to run a single .java file in my test-folder for debugging and testing. If I run it in my main folder, all is fine, If I run it in my test folder, i get an error:

My project sturcture:

src
|_____ main
      |_____ java -> MainSrc.java
|_____ test
      |_____ java -> MainTest.java

MainSrc / MainTest.java contains:

public class MainSrc / MainTest
{
    public static void main(String[] args)
    {
        System.out.println("test");
    }
}

Now if i right-click and hit "run file" on both in netbeans:

main\\java\\MainSrc.java

run:
test
BUILD SUCCESSFUL (total time: 0 seconds)

test\\java\\MainTest.java

cd C:\Java\MainTest; "JAVA_HOME=C:\\Program Files (x86)\\Java\\jdk1.7.0_02" 
"\"C:\\Program Files\\NetBeans 7.2\\java\\maven\\bin\\mvn.bat\"" 
"-Dexec.args=-classpath %classpath Test" 
"-Dexec.executable=C:\\Program Files (x86)\\Java\\jdk1.7.0_02\\bin\\java.exe" 
-Dexec.classpathScope=test --offline --errors process-classes org.codehaus.mojo:exec-maven-plugin:1.2:exec
Error stacktraces are turned on.
Scanning for projects...

------------------------------------------------------------------------
Building Test 1.0
------------------------------------------------------------------------

[resources:resources]
[debug] execute contextualize
Using 'windows-1252' encoding to copy filtered resources.
Copying 0 resource

[compiler:compile]
Nothing to compile - all classes are up to date

[exec:exec]
Error: could not find or load main class MainTest
------------------------------------------------------------------------
BUILD FAILURE

my actions is (also tried classPathScope=test):

    <action>
        <actionName>run</actionName>
        <goals>
            <goal>process-classes</goal>
            <goal>org.codehaus.mojo:exec-maven-plugin:1.2:exec</goal>
        </goals>
        <properties>
            <exec.args>-classpath %classpath MainTest</exec.args>
            <exec.executable>java</exec.executable>
            <exec.classpathScope>${classPathScope}</exec.classpathScope>
            <exec.workingdir>D:\Dropbox\Java\Test Automation Selenium</exec.workingdir>
        </properties>
    </action>

I am not a pro in Java, most i googled together but I could not find a solution for this. I also don't understand why it does not work. I tried to access the test package from my default package, but ofc that was not possible...

Help please? :) Thank you

This sounds like the magic "no main class found" exception (sorry if not!).

I've seen this quite some times on the NB users mailinglist and once per year I also ran into this magic exception. I gathered the solutions that I found and that usually helped me fixing these kind of exception/error (the listing is based on the times of NB 6.8 but might still hold).

  • Ensure you have a public class containing a main method
  • This class must have a well defined main method: “public static void main(String[] args) { }“
  • You have set up the project configuration with the correct class: Project Properties > Run > Main Class (maybe just set it again, so that the project properties file is written again)
  • Try to rebuild the project: Menu > Run > Clean & Build Main Project
  • Still no success? Try renaming the class (Foo -> Foo1) and rename it back (Foo1 -> Foo). This should invalidate the NetBeans cache for this file (Just assuming – I am not a NetBeans Developer).
  • Still no success?? You might to have to clear the NetBeans cache by deleting the directory “.netbeans\\6.8\\var\\cache” (the 6.8 might of course vary according to your version of NetBeans). Close NetBeans before deleting the cache – the next start might take some time as NB will most likely have to scan your code again.
  • Still no luck? You might want to consult the NetBeans user forum or nbusers-Mailinglist (please be friendly, patient and report the bug in a way that others are able to help).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM