2

我正在尝试运行一个简单的程序:

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

但我得到/bin/bash: line 2: $'\r': command not found python: can't open file '/projects/GreetTest/main.py': [Errno 2] No such file or directory了输出。

可以做些什么来编译和执行这样简单的控制台程序?我尝试使用谷歌搜索,但无法找到完整的教程。

编辑:当我按运行时newCustom运行:

# execute a JAR
java -jar ${current.project.path}/target/application.jar

# execute python script
python ${current.project.path}/main.py

我尝试删除 python 部分,所以现在是:

# execute a JAR
java -jar ${current.project.path}/target/application.jar

它给出了一个错误:

Error: Unable to access jarfile /projects/GreetTest/target/application.jar

我的文件名为 Greet.java.

4

2 回答 2

2
  1. 重命名要调用的文件Greet.java。该文件应该包含您在问题中发布的代码,仅此而已。
  2. 运行javac Greet.java
  3. 运行java Greet

这假设您的机器上安装了一个正常工作的 JDK。

如之前的评论中所述,您似乎正在尝试使用 Python 解释器运行您的 Java 代码。Python 是一个完全不同的编程环境。


更新:所以,我看了一下 Codenvy。它们提供了一个完整的 Hello World 示例。创建一个新的工作区,选择“Java”快速启动堆栈并将console-java-simple项目添加到工作区。然后运行console-java-simple: run​​Maven 任务。您可以按 获取项目的 Maven 任务列表Shift+F10

他们示例中的HelloWorld.java文件几乎与您的Greet.java.

以下是如何一步一步地做到这一点:

  1. 登录 Codenvy。
  2. 在仪表板上,单击Create Workspace(在 下Recent Workspaces
  3. On the following page, in the "Select Stack" section, select the stack labeled Java - Default Java Stack with JDK 8, Maven and Tomcat. (not the preselected one that includes MySQL).
  4. On the same page, in the "Projects" section, click Add or Import Project and check the console-java-simple project. Then click Add.
  5. Click the big green Create button at the bottom, or the one in the upper right corner. The workspace gets created and the view switches to the new workspace. Wait until you see a notification in the upper right corner telling you that the console-java-simple project was successfully imported.
  6. Press Shift+F10.
  7. Double click the run Maven task.
  8. See the output of the Maven build and finally the "Hello World" at the very bottom of the console.

In the project explorer, under console-java-simple/src/main/java/org.eclipse.che.examples/, you can find the HelloWorld.java.


Update: By the way, I don't think that Codenvy is the right tool to get started with Java. It's not meant to be a quick and easy way to play around with Java but instead a full blown IDE running in the browser. You have to deal with a lot of distractions from actual Java programming, like build tools (e.g. Maven) to get anything running.

If getting started with Java is what you are aiming at, I'd recommend to find a way to get a JDK installed locally and then play around with that.

于 2017-09-19T10:16:09.683 回答
0

All you have to do to run your program is to remove current macro and add a new one.

In order to do it select second icon from the left in the top left corner ("Manage Commands"). Then select current macro and remove it. Next click plus sign and from drop down list select "Java".

If you don't like default name of the macro you can rename it. In order to do so double-click it, enter new name and then click "SAVE" button located below.

screenshot

于 2018-06-02T00:49:35.543 回答