Configure and Run Plain Java Apps in Eclipse Che

Datetime:2016-08-22 23:17:11          Topic: Java           Share

This post will demonstrate how to develop and run standard Java applications in Eclipse Che. We’ll start by creating and running a simple “Hello World” example using one of the Che template apps. Then we’ll clone a plain Java project from Git, configure the classpath, add a library to the classpath, and run the application.

Create an Empty Java Workspace

In the user dashboard select “Workspaces” from the left nav and hit the “+” in the upper-right corner to create a new workspace. Then select Java and hit the “Create Workspace” button at the bottom:

You’ll be taken to the workspace details screen where you can hit the ‘Run’ button to start the workspace and, when it completes, hit the ‘Open’ button in the top right to open the IDE.

Create a Java Application From a Template

After creating and running your workspace, open the IDE. You’ll see the image below.

In the Che IDE menu, select Workspace > Create Project .

In the ‘Create New Project’ pop-up, select “Java” from the language chooser and type the name of the project, then click the ‘Create’ button.

Expand the project and open the Main.java class. The Java file contains a main method and the project is pre-configured for compiling and running.

To run the application we’ll need to create a command for it using the interactive menu in the Che IDE. Go to Run > Edit Commands .

In the ‘Commands’ pop-up, we need to select ‘Java type’ and click the ‘+’ button.

This creates a new Java command. Replace the default name with something more appropriate like ‘start app’ . The command defaults will work for our plain Java app, so we can simply hit Save.

The new command appears on the the toolbar panel in the Che IDE. Click on the icon arrow to run the application.

In the console panel you will see a new entry for your command and in the console output will be the command that was run (in green at the top) and “Hello World!” output. Congratulations!

Because we consistently sync content from the container file system and the project explorer, if you open folder ‘bin’ you’ll see the compiled  Main.class java file .

Configure and Run an Application Cloned From GitHub

In the Che IDE menu select Workspace -> Import Project -> Git and type the repository url (e.g. https://github.com/artaleks9/plain-java-project.git). You can do this in the same workspace because Che workspaces allow multiple projects inside.

After importing the project the Project Configuration pop-up will appear. Select type  Java and press the  Next button. Hit ‘Browse’ beside the Source Folder entry and select the project’s  src folder.

In our example, the project already includes folder store with a library ( mockito jar ), so we also can add it to classpath by hitting  Browse next to the Library Folder entry. The jar will be automatically added to project classpath during compilation and runtime and made available in the code-assistant when you hit  ctrl+space . Click  Save .

We can expand the project and make sure that it is configured (if it is you’ll see the package indicator on the com.codenvy  package entry (if  com and  codenvy appear as separate, normal folders something is misconfigured).

We can easily change configuration of a project late to add / replace the source folder, or add / delete the library JAR.

To try this let’s upload a new JAR to the project. First download junit-4.4.jar from https://sourceforge.net/projects/junit/files/junit/4.4/junit-4.4.jar/download .

Next select the ‘store’ folder in the Project Explorer. Then hit Project > Upload File to add the JUnit JAR to that directory. Now choose  Project > Configure Classpath . Select  Add JAR and choose the desired JAR in the  Select Path widget.

You can add folder test to project classpath as source folder.

As you see, we added folder to project classpath

We can also easily add or unmark a folder to the project classpath as source using context menu in project explorer.

Now that we’ve made some changes to the project, we need a new Command to run this project (because the command we created previously will look for the Main.java in the source folder, not in a package).

Choose Run > Edit Commands . Create a new Java command. The best practice when you have multiple projects is to name each command with either of the following styles:

{project.name} : {command name}
{project.nickname} : {command name}

In this case let’s call the command “pjp : run” . Now select  Browse beside the Main Class entry box – select your Main class.

Hit Save . When we try to start the application, the running fails because in our project is not present the folder bin.

Let’s create the folder bin and try to run again.

This time, the application has successfully started.

Now we're all set to harness Che for plain Java projects.





About List