且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从命令行运行Eclipse项目

更新时间:2022-12-31 10:50:26

C:\temp\compile-test\src\a\b\c\D.java

其中D.java是:

package a.b.c;

public class D { }

第一个问题,阅读:myfile.java ,是因为使用 cp 命令行选项指向您的源代码是不正确的。

The first problem, cannot read: myfile.java, is because it is not correct to use the cp command line option to point to your source code.

C:\temp\compile-test\src>javac -cp c:\temp\compile-test\src\a\b\c D.java
javac: file not found: D.java
Usage: javac <options> <source files>
use -help for a list of possible options

从您的源文件夹运行javac ,我们可以使用相对路径的源文件(注 - javac 从源文件夹):

This should instead be the following, where javac is run from your source folder, and we can use relative paths to the source files (NOTE - javac is run from the source folder here):

C:\temp\compile-test\src>javac a\b\c\D.java

或者,我们指定源文件的完整路径,和 javac 可以从任何地方运行(注 - javac C:\ 这里):

Or this, where we specify full paths to the source files, and javac can be run from anywhere (NOTE - javac is run from C:\ here):

C:\>javac temp\compile-test\src\a\b\c\D.java

上述两个选项都会导致在您的类文件被创建在与源相同的文件夹中。 Ie:

Both of the above options will result in your class files being created in the same folder as the source. I.e.:

C:\temp\compile-test\src\a\b\c\D.class

对于第二个问题,如果尝试并运行一个包名为'里面'的包,这将导致名称错误(注 - java 从'里面'运行在这里包):

For the second problem, if you try and run a class that has a package name from 'inside' the package, this will result in the name being wrong (NOTE - java being run from 'inside' the package here):

C:\temp\compile-test\src\a\b\c>java D
Exception in thread "main" java.lang.NoClassDefFoundError: D (wrong name: a/b/c/D)
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
        at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: D.  Program will exit.

要运行 D 在包'root',并提供完全限定类名。 Ie:

To run the D class, you should be at the package 'root', and supply the Fully Qualified Class Name. I.e.:

C:\temp\compile-test\src>java a.b.c.D
Exception in thread "main" java.lang.NoSuchMethodError: main

注意我得到一个异常 D 类没有主方法,因此无法运行。要修复,我们添加一个main方法:

Note I get an exception as the D class doesn't have a main method, and so cannot be run. To fix, we add a main method:

package a.b.c;

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

并重新运行:

C:\temp\compile-test\src>java a.b.c.D
main