简体   繁体   English

从命令行运行Eclipse项目

[英]Run Eclipse project from command line

I am having two problems regarding compiling and running an Eclipse java project from command line. 关于从命令行编译和运行Eclipse java项目,我遇到两个问题。 This works fine when I am just running from the eclipse IDE. 当我刚从eclipse IDE运行时,这很好用。 I tried googling but couldn't really get the thing working. 我尝试使用谷歌搜索,但无法让事情发挥作用。 Any help is much appreciated. 任何帮助深表感谢。

Problem 1: When I try to compile from a location different from the directory where the .java file is, it throws the error " cannot read: myfile.java ". 问题1:当我尝试从与.java文件所在目录不同的位置进行编译时,会抛出错误“ 无法读取:myfile.java ”。 But if I migrate to this directory then it compiles. 但是如果我迁移到这个目录然后编译。
The command that I was giving is (when in some other directory): 我给出的命令是(当在其他目录中时):
javac -cp C:\\ABC\\src\\XYZ myfile.java javac -cp C:\\ ABC \\ src \\ XYZ myfile.java
The command that I was giving when in XYZ directory: 在XYZ目录中我给出的命令:
javac myfile.java javac myfile.java
This generated two .class files myfile.class and Testing_Thread.class(I guess this because I have a thread in my code) 这生成了两个.class文件myfile.class和Testing_Thread.class(我想这是因为我的代码中有一个线程)

Problem 2: After I have compiled by going to its directory, when I try to run the program, I get the error " Exception in thread "main" java.lang.NoClassDefFoundError: myfile (wrong name: XYZ/myfile.java) " even when I am trying to run from the XYZ directory. 问题2:通过转到目录编译后,当我尝试运行程序时,我收到错误“ 线程中的异常”主“java.lang.NoClassDefFoundError:myfile(错误名称:XYZ / myfile.java) ”即使我试图从XYZ目录运行。 I get the same error when I try to run from some other place also. 当我尝试从其他地方跑步时,我也得到了同样的错误。
The command that I was giving when in XYZ directory: 在XYZ目录中我给出的命令:
java myfile java myfile
The command that I was giving when in some other place: 我在其他地方给出的命令:
java -cp C:\\ABC\\src\\XYZ myfile java -cp C:\\ ABC \\ src \\ XYZ myfile

I am also attaching a hierarchy of my directory structure if it is of any help: 如果有任何帮助,我还附加了我的目录结构的层次结构:
在此输入图像描述

These examples assume the following source structure: 这些示例假定以下源结构:

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

Where D.java is: D.java是:

package a.b.c;

public class D { }

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. 第一个问题, 无法读取:myfile.java ,是因为使用cp命令行选项指向源代码是不正确的。

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

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): 这应该是以下,从源文件夹运行javac ,我们可以使用源文件的相对路径(注意 - javac从源文件夹运行):

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

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): 还是这个,在这里我们指明完整路径源文件和javac可以在任何地方运行(注- javac是从运行C:\\这里):

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

Both of the above options will result in your class files being created in the same folder as the source. 上述两个选项都将导致您的类文件与源文件在同一文件夹中创建。 Ie: 即:

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

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): 对于第二个问题,如果您尝试运行一个具有包名称来自包内的包名的类,这将导致名称错误(注意 - java在此处从包里面运行):

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.

To run the D class, you should be at the package 'root', and supply the Fully Qualified Class Name . 要运行D类,您应该位于包“root”,并提供完全限定的类名 Ie: 即:

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

Note I get an exception as the D class doesn't have a main method, and so cannot be run. 注意我得到一个例外,因为D类没有main方法,因此无法运行。 To fix, we add a main method: 要修复,我们添加一个main方法:

package a.b.c;

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

and re-run: 并重新运行:

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

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

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