从命令行运行Eclipse项目 [英] Run Eclipse project from command line

查看:181
本文介绍了从命令行运行Eclipse项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从命令行编译和运行Eclipse java项目时遇到两个问题。这工作正常,当我只是从eclipse IDE运行。我试过谷歌,但不能真正得到的东西工作。任何帮助是非常赞赏。

I am having two problems regarding compiling and running an Eclipse java project from command line. This works fine when I am just running from the eclipse IDE. I tried googling but couldn't really get the thing working. Any help is much appreciated.

问题1:当我尝试从与.java文件所在目录不同的位置编译时,会抛出错误无法读取:myfile。 java 。但是如果我迁移到这个目录,然后它编译。<​​br>
我给的命令是(当在其他目录中):

javac -cp C:\ ABC \src\XYZ myfile.java

我在XYZ目录中提供的命令:

javac myfile.java

生成两个.class文件myfile.class和Testing_Thread.class(我猜这是因为我的代码中有一个线程)

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". 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
The command that I was giving when in XYZ directory:
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)

问题2 :我通过转到其目录编译,当我尝试运行程序,我得到错误线程中的异常mainjava.lang.NoClassDefFoundError:myfile(错误的名称:XYZ / myfile.java) ,即使我试图从XYZ目录运行。当我尝试从其他地方运行时,我得到相同的错误。

当我在XYZ目录中给出的命令:

java myfile

在其他地方发出的命令:

java -cp C:\ABC\src\XYZ myfile

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. 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:
java myfile
The command that I was giving when in some other place:
java -cp C:\ABC\src\XYZ myfile

如果任何帮助,我还附加一个目录结构的层次结构:

I am also attaching a hierarchy of my directory structure if it is of any help:

推荐答案

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

这篇关于从命令行运行Eclipse项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆