2

I'm trying to run a simple program:

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

But I get /bin/bash: line 2: $'\r': command not found python: can't open file '/projects/GreetTest/main.py': [Errno 2] No such file or directory as an output.

What can be done to make such simple console programs to be compiled and executed? I tried googling but had trouble finding a thorough tutorial.

EDIT: When I press run a newCustom is run:

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

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

I tried deleting the python part so it's now:

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

and it gives an error:

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

My file is titled Greet.java.

Ans
  • 1,212
  • 1
  • 23
  • 51

2 Answers2

2
  1. Rename the file to be called Greet.java. That file should contain exactly the code that you posted in your question, nothing more.
  2. Run javac Greet.java.
  3. Run java Greet.

This assumes that you have a working JDK installed on your machine.

As stated in the comments before, it seems that you are trying to run your Java code with a Python interpreter. Python is a completely different programming environment.


Update: So, I had a look at Codenvy. They provide a complete Hello World example. Create a new workspace, select the "Java" quick start stack and add the console-java-simple project to the workspace. Then run the console-java-simple: run Maven task. You can get a list of the project's Maven tasks by pressing Shift+F10.

The HelloWorld.java file in their example is almost identical to your Greet.java.

Here's how to do it step by step:

  1. Log in to Codenvy.
  2. On the dashboard, click Create Workspace (under 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.

anothernode
  • 5,100
  • 13
  • 43
  • 62
  • You mean run it in my command promt? I don't have anything related to java installed on my Windows machine. I was hoping to find a 100% online environment. Am I mistaken for thinking codenvy could provide one/ – Ans Sep 19 '17 at 10:36
  • Yes, I overlooked the "Codenvy" part in the title. Could be a good idea to make it more clear in you question text that you are specifically asking about Codenvy, and not just Java in general. As you can see, people will most of the time assume you want to run your code example locally if not specified otherwise very clearly. – anothernode Sep 19 '17 at 11:35
  • Does the Codenvy specific part I added to my answer help you? – anothernode Sep 19 '17 at 14:23
  • I tried your answer, but can't figure out how you run `console-java-simple: run` Maven task? I opened a newly created workspace, is it supposed to be under a Run tab? Sadly I don't have a choice since I can't install ide where I am. – Ans Sep 20 '17 at 10:47
  • I added a step by step explanation. Can you make it run by following those steps? – anothernode Sep 20 '17 at 11:05
  • Thank you, it worked! Any chance you know some good tutorials for codenvy or something I could read to be able to use it more easily? That's my only option at the moment. – Ans Sep 21 '17 at 08:59
  • 1
    Codenvy is based on Eclipse Che which is basically a web application version of the classic Eclipse IDE. So, many things that you can learn about Eclipse will be applicable to Codenvy, too. At a minimum, besides Java programming itself, you'll probably have to know about basic Maven usage in order to do anything meaningful with Codenvy. Codenvy itself has Tutorials and a User Guide available: https://codenvy.io/docs/ – anothernode Sep 21 '17 at 10:21
  • Ok, I'll take a look at that. Thank you so much, you are super cool! – Ans Sep 21 '17 at 12:38
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

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135