Java 我可以从 Eclipse 创建的命令行程序运行吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2276219/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 05:35:32  来源:igfitidea点击:

Can I run from command line program created by Eclipse?

javaeclipsecommand-line

提问by Roman

Using Eclipse I have created a SWT Hello World program. I was able to run this program from Eclipse and it worked fine.

我使用 Eclipse 创建了一个 SWT Hello World 程序。我能够从 Eclipse 运行这个程序,它运行良好。

In the "/home/myname/workspace/HelloWorldSWT" I found two files: HelloWorldSWT.java and HelloWorldSWT.class. I wanted to execute the corresponding program from the command line. First I tried to type "java HelloWorld" and I got the following error message:

在“/home/myname/workspace/HelloWorldSWT”中,我找到了两个文件:HelloWorldSWT.java 和 HelloWorldSWT.class。我想从命令行执行相应的程序。首先,我尝试键入“java HelloWorld”,但收到以下错误消息:

Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld
   at gnu.java.lang.MainThread.run(libgcj.so.90)
Caused by: java.lang.ClassNotFoundException: HelloWorld not found in gnu.gcj.runtime.SystemClassLoader{urls=[file:./], parent=gnu.gcj.runtime.ExtensionClassLoader{urls=[], parent=null}}
   at java.net.URLClassLoader.findClass(libgcj.so.90)
   at gnu.gcj.runtime.SystemClassLoader.findClass(libgcj.so.90)
   at java.lang.ClassLoader.loadClass(libgcj.so.90)
   at java.lang.ClassLoader.loadClass(libgcj.so.90)
   at gnu.java.lang.MainThread.run(libgcj.so.90)

I also tried this "java -cp /home/roman/workspace/ HelloWorld.HelloWorld". As the result I got the following error message:

我也试过这个“java -cp /home/roman/workspace/HelloWorld.HelloWorld”。结果我收到以下错误消息:

Exception in thread "main" java.lang.NoClassDefFoundError: loaded class HelloWorld.HelloWorld was in fact named HelloWorld
   at java.lang.VMClassLoader.defineClass(libgcj.so.90)
   at java.lang.ClassLoader.defineClass(libgcj.so.90)
   at java.security.SecureClassLoader.defineClass(libgcj.so.90)
   at java.net.URLClassLoader.findClass(libgcj.so.90)
   at gnu.gcj.runtime.SystemClassLoader.findClass(libgcj.so.90)
   at java.lang.ClassLoader.loadClass(libgcj.so.90)
   at java.lang.ClassLoader.loadClass(libgcj.so.90)
   at gnu.java.lang.MainThread.run(libgcj.so.90)

Does anybody know what I am doing wrong? Thank you in advance for any help.

有谁知道我做错了什么?预先感谢您的任何帮助。

回答by Uri

It is possible that you are not loading the SWT library correctly, and as a result your class fails to load.

您可能没有正确加载 SWT 库,因此您的类无法加载。

The SWT library is part of jars that are already loaded when you run Eclipse but are not loaded in a command line parameter. Did you modify your class path accordingly?

SWT 库是运行 Eclipse 时已加载但未在命令行参数中加载的 jar 的一部分。您是否相应地修改了课程路径?

Here is an old article about how to do this sort of stuff in older versions of Eclipse http://www.ibm.com/developerworks/opensource/library/os-ecgui1/You will need different jars today with latest version fo Eclipse. It might even come down to a single jar.

这是一篇关于如何在旧版本的 Eclipse 中执行此类操作的旧文章 http://www.ibm.com/developerworks/opensource/library/os-ecgui1/今天您将需要不同的 jars 和最新版本的 Eclipse。它甚至可能归结为一个罐子。

Check out the SWT FAQ; at least for Mac Carbon, you can use a single jar, I would bet you can do that for other platforms.

查看SWT 常见问题;至少对于 Mac Carbon,你可以使用一个 jar,我敢打赌你可以在其他平台上做到这一点。

Also, I'm not 100% sure that you can run Eclipse under openJDK, which seems to be the case on your platform.

另外,我不是 100% 确定您可以在 openJDK 下运行 Eclipse,这在您的平台上似乎是这种情况。

回答by GuruKulki

This is the problem which is caused because the JVM is not able to find the HelloWorld class as you have the class name as HelloWorldSWT with main method in it. try with

这是由于 JVM 无法找到 HelloWorld 类而引起的问题,因为您的类名为 HelloWorldSWT,其中包含 main 方法。尝试

java HelloWorldSWT

java HelloWorldSWT

回答by Persimmonium

Go to the Debug perspective, and select the program you just ran (where it says Terminated, exit value... in the Debug tab) Right click, and choose Properties, there you can see the whole command line command that was launched by eclipse.

转到 Debug 透视图,然后选择您刚刚运行的程序(在Debug 选项卡中显示 Terminated, exit value...)右键单击,然后选择 Properties,在那里您可以看到 eclipse 启动的整个命令行命令.

You can run this same command in the same directory eclipse did (see in Run Configurations, Arguments, Working directory) and it will work.

您可以在 eclipse 执行的同一目录中运行相同的命令(请参阅运行配置、参数、工作目录中的内容),它将起作用。

回答by user85421

You must add the directory where eclipse is storing the HelloWorldSWT.class file in the classpath. It is defined when the project is created, like "bin", "build" or "classes". Check the project properties or search the HelloWorldSWT.class file.

您必须在类路径中添加 eclipse 存储 HelloWorldSWT.class 文件的目录。它是在创建项目时定义的,如“bin”、“build”或“classes”。检查项目属性或搜索 HelloWorldSWT.class 文件。

Assuming it is the builddirectory inside the HelloWorldSWT workspace and that your class is in no package (default package), the command should be:

假设它是buildHelloWorldSWT 工作区中的目录并且您的类不在包(默认包)中,命令应该是:

java -cp /home/roman/workspace/HelloWorldSWT/build HelloWorldSWT

or just change to that directory and use:

或者只是更改到该目录并使用:

java -cp . HelloWorldSWT

EDIT:
The -cp .means that only the actual directory is used for finding the class files. You must also add all libraries (JARs or other directories) used by your program (databse, SWT, ...).
For more details you can have a look at the documentation: How Classes are Found, Setting the Classpathand java comand

编辑:
-cp .意味着只有实际目录用于查找类文件。您还必须添加程序使用的所有库(JAR 或其他目录)(数据库、SWT 等)。
有关更多详细信息,您可以查看文档:How Classes are Found, Setting the Classpathand java comand

回答by deleted

You have not set classpath correctly then.

你没有正确设置类路径。

I think it is usually more convenient to have the IDE build a jar-file with all non-jre libs included and execute that from the command-line.

我认为让 IDE 构建一个包含所有非 jre 库的 jar 文件并从命令行执行它通常更方便。

java -jar myprogram.jar

java -jar myprogram.jar

What is the use of the program written in Eclipse if it can be easily executed only within the Eclipse?

如果只能在 Eclipse 中轻松执行,那么用 Eclipse 编写的程序有什么用?

That's really a good question, and deployment of java applications is really an art in itself. Both compilation and execution is - if not complicated - at least cumbersome when performed by hand. But Eclipse and other tools like Netbeans can help you perform these tasks in an easy way, and even package up your program for you so that others that do not use these tools also can execute the programs easily.

这确实是一个很好的问题,Java 应用程序的部署本身就是一门艺术。编译和执行 - 如果不复杂 - 至少在手动执行时很麻烦。但是 Eclipse 和 Netbeans 等其他工具可以帮助您轻松执行这些任务,甚至可以为您打包程序,以便其他不使用这些工具的人也可以轻松执行程序。

回答by Riduidel

Seems to me you don't have a class named HelloWorldSWT``, but rather a class named HelloWorldSWTin a package named HelloWorldSWT(you can confirm this by going at the first line of HellowWorldSWT.java, where you will find package HelloWorldSWT;

在我看来,您没有名为 HelloWorldSWT`` 的类,而是在名为HelloWorldSWT的包中命名的类HelloWorldSWT(您可以通过转到 的第一行来确认这一点HellowWorldSWT.java,您将在其中找到package HelloWorldSWT;

If so, go in parent directory and type

如果是这样,请进入父目录并键入

java HelloWorldSWT.HelloWorldSWTThis would work.

java HelloWorldSWT.HelloWorldSWT这会奏效。

回答by Ian

If you want to run the program with command line arguments from within Eclipse you can go to Run->Run Configurations, which will bring up a window with the program you're running as well as a box where you can enter arguments.

如果您想在 Eclipse 中使用命令行参数运行程序,您可以转到 Run->Run Configurations,这将打开一个包含您正在运行的程序的窗口以及一个可以输入参数的框。

回答by Kevin Lee

I found another quick and dirty solution if you just want to see some output on the command line. But this is not a good practicefor the long term!

如果您只想在命令行上看到一些输出,我找到了另一个快速而肮脏的解决方案。但从长远来看,这不是一个好习惯

  1. Remove the package declaration in your code (Eclipse will complain and give you a red cross, ignore that first), e.g. package hello;

  2. Save.

  3. Open your command line and navigate until you are in the src folder (use cd).

  4. Compile the java file, e.g. javac HelloWorld.java

  5. Execute the java class file, e.g. java HelloWorld

  1. 删除代码中的包声明(Eclipse 会抱怨并给你一个红叉,先忽略它),例如 package hello;

  2. 节省。

  3. 打开命令行并导航,直到进入 src 文件夹(使用 cd)。

  4. 编译java文件,例如 javac HelloWorld.java

  5. 执行java类文件,例如 java HelloWorld

It should work, as long as you are in the correct directory without any package declarations in the code! The package declarations do cause issues..and Eclipse is set up to make sure everything works so that's why it's fine to have the package declarations there. An alternative workaround is to really go and set the classpath on your own (which always confuses me every time I have to do it, which I rarely do).

只要您在正确的目录中且代码中没有任何包声明,它就应该可以工作!包声明确实会导致问题......并且 Eclipse 被设置为确保一切正常,所以这就是为什么在那里有包声明很好。另一种解决方法是真正去自己设置类路径(每次我必须这样做时总是让我感到困惑,而我很少这样做)。

回答by foxwendy

I do some workaround so as to take full advantage of the Eclipse convenience. Below is what I found, and it worked well for me. Hope it will help: enter image description here

我做了一些变通方法以充分利用 Eclipse 的便利。下面是我发现的,它对我来说效果很好。希望它会有所帮助: 在此处输入图片说明