简体   繁体   English

无法从src \\ test文件夹“运行文件” - >找不到主要类或无法加载主类

[英]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. 我只想在我的测试文件夹中运行一个.java文件进行调试和测试。 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: MainSrc / MainTest.java包含:

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: 现在,如果我右键单击并在netbeans中同时点击“运行文件”:

main\\java\\MainSrc.java 主\\ java的\\ MainSrc.java

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

test\\java\\MainTest.java 测试\\ 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): 我的动作是(也试过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. 我不是Java的专业人士,大多数我用Google搜索,但我找不到解决方案。 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. 我在NB用户邮件列表上已经看过很多次了,每年我也遇到过这个神奇的例外。 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). 收集了我找到的解决方案 ,这通常帮助我修复了这种异常/错误(列表基于NB 6.8的时间,但可能仍然存在)。

  • Ensure you have a public class containing a main method 确保您有一个包含main方法的公共类
  • This class must have a well defined main method: “public static void main(String[] args) { }“ 这个类必须有一个定义良好的main方法:“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). 尝试重命名该类(Foo - > Foo1)并将其重命名(Foo1 - > Foo)。 This should invalidate the NetBeans cache for this file (Just assuming – I am not a NetBeans Developer). 这应该使此文件的NetBeans缓存无效(假设 - 我不是NetBeans开发人员)。
  • 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). 您可能必须通过删除目录“.netbeans \\ 6.8 \\ var \\ cache”来清除NetBeans缓存(6.8当然可能因您的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. 在删除缓存之前关闭NetBeans - 下次启动可能需要一些时间,因为NB很可能必须再次扫描您的代码。
  • 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). 您可能需要咨询NetBeans用户论坛或nbusers-Mailinglist(请保持友好,耐心并以其他人能够提供帮助的方式报告错误)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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