简体   繁体   English

使用NetBeans编译Java类,但在Linux上的控制台错误

[英]Compile java class with NetBeans but error with console on Linux

In NetBeans 8.0.2 I've done a simple "Hello World" class that compiles and everything is fine. 在NetBeans 8.0.2中,我完成了一个简单的“ Hello World”类,该类可以编译并且一切正常。

public class OOP_HW3 { 
    public static void main(String[] args) {
      System.out.println("Hi ");
    }
}

On the other hand when I navigate to this folder with console and run: 另一方面,当我使用控制台导航到该文件夹​​并运行时:

$ javac OOP_HW3.java // OK 
$ java OOP_HW3 
Exception in thread "main" java.lang.NoClassDefFoundError: OOP_HW3 (wrong name: oop_hw3/OOP_HW3)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
....

I can easily run this file with console in any other directory, but here in the NetBeans project gives me error. 我可以使用控制台在任何其他目录中轻松运行此文件,但是在NetBeans项目中,这给了我错误。 What am I missing ? 我想念什么?

Run the following commands from where you are running current command like: 从运行当前命令的位置运行以下命令,例如:

cd ..
java oop_hw3.OOP_HW3

Since you are using package, you need to be on one top ie parent directory and then run with fully qualified package name followed by class where you define main method. 由于您使用的是包,因此您需要位于顶层(即父目录),然后以完全合格的包名称运行,后跟定义主方法的类。

从Java文件顶部删除package定义。

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

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