简体   繁体   English

Eclipse - 编译和执行程序

[英]Eclipse - Compiling & executing program

In the past, I have used JCreator to develop my java applications. 过去,我使用JCreator开发我的java应用程序。 I wanted to try Eclipse and see its IDE. 我想尝试Eclipse并查看它的IDE。

On JCreator, I could just open a java file and run it in the command prompt. 在JCreator上,我可以打开一个java文件并在命令提示符下运行它。 On Eclipse, I cannot find out how I can compile and run a java file written by someone else without first creating a project, creating a new java class and then copy and pasting the code from the file. 在Eclipse上,我无法找到如何编译和运行由其他人编写的java文件,而无需先创建项目,创建新的java类,然后从文件中复制并粘贴代码。

I cannot even figure out how to import a java file into the project. 我甚至无法弄清楚如何将java文件导入项目。

So my questions: 所以我的问题:
1. How can I open, compile and run a java file without first creating a java project. 1.如何在不首先创建java项目的情况下打开,编译和运行java文件。
2. How can I import a java file into a project I already have. 2.如何将java文件导入到我已有的项目中。
3. How can I run the program in command prompt, instead of on the console that is in Eclipse, (update: to be more clear) directly from the Eclipse. 3.如何在命令提示符下运行程序,而不是直接从Eclipse运行Eclipse中的控制台(更新:更清楚)。

Thank you. 谢谢。

1/ Do not forget scrapbook page (also presented here ) 1 /不要忘记剪贴簿页面此处也有介绍

In theory, you could copy a class in it, but since that class is actually encapsulated into a "main invisible" class, as an inner class, it would not support any static methods in it. 理论上,你可以在其中复制一个类,但由于该类实际上被封装到一个“主要不可见”类中,作为一个内部类,它不支持其中的任何静态方法。

However, for a quick chunk of code, it does the trick just fine. 但是,对于快速的代码块,它可以正常工作。

检查剪贴簿中的代码
(from this Eclipse tutorial ) (来自这个Eclipse教程

3/ Since you want it run directly from eclipse, use as indicated in this thread , an External tool 3 /因为您希望它直接从eclipse运行,请使用此线程中指示外部工具

That External Tool would run C:\\WINDOWS\\system32\\cmd.exe with working directory ${container_loc} 该外部工具将使用工作目录${container_loc}运行C:\\WINDOWS\\system32\\cmd.exe

More details in this article "How do I open a Windows Command Prompt in my console ?" 本文中的更多详细信息“如何在控制台中打开Windows命令提示符?” .


(source: avajava.com ) (来源: avajava.com

That will give: 这会给:


(source: avajava.com ) (来源: avajava.com

  1. As far as I know, you can't 据我所知,你做不到
  2. Right click project > Import 右键单击项目>导入
  3. With respect to your platform, something roughly equivalent to: java <classname> . 关于您的平台,大致相当于: java <classname> (Make sure java is in your path, otherwise use the absolute path to the it.) (确保java在您的路径中,否则使用它的绝对路径。)

A useful feature is available in Eclipse since version 3.4: you can paste the text of a Java class into a Java project (copy the content of the class, select the project or source folder, paste). 从版本3.4开始,Eclipse中提供了一个有用的功能:您可以将Java类的文本粘贴到Java项目中(复制类的内容,选择项目或源文件夹,粘贴)。 This will create an appropriately named file, in the correct package. 这将在正确的包中创建一个适当命名的文件。 This is probably the easiest way to import a single class. 这可能是导入单个类的最简单方法。

2) Use the "Import" command. 2)使用“导入”命令。 Ensure the java file goes to the "src" directory. 确保java文件转到“src”目录。

3) One way is to export your classes to a JAR file and then run the JAR file at the command prompt (eg java -jar file.jar) 3)一种方法是将类导出到JAR文件,然后在命令提示符下运行JAR文件(例如java -jar file.jar)

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

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