Useful Tips

How to create an executable in Eclipse

This is a completely new question. I am running Eclipse on Ubuntu. I created a test project that I want to compile for an executable file (the linux equivalent of the Windows.exe file). Here is the contents of my program:

I want to know how to compile it, and then how to execute it from the command line.

You need to create an executable JAR file. Steps will pass shortly.

Right-click on the project and select the JAR file in the "Export" section.

Enter the path where you want to save it. An example here is Windows. Change to suit your platform.

Edit the field of the main class by viewing and selecting the location of your class containing the main method.

To run the JAR file, open a shell or command prompt and run the following command:

java -jar path / to / test.jar

In Eclipse, select the file, then export, and you will need to select the runnable jar. In addition, you will be prompted to select the MyTest main class in your case.

The Eclipes tutorials are very useful if you complete the tutorial “Create a Hello World Application”, it will guide you through the process of setting up a project, creating an application, and launching a jar file.

I want to know how to compile it.

See other answers on how to get Eclipse to create a JAR file.

. and then how to execute it from the command line.

In the simple case, you execute it by running java -jar yourApp.jar.

If your application depends on external libraries, then this is a bit more complicated.

How can I select a jar file over a jar executable?

Build automation

As mentioned earlier, when creating a JAR using Eclipse, you can save the generated Ant script containing all the necessary actions.

In the project properties (Project -> Properties) there is a section Builders, in which you can create additional builders, and in particular, Ant can be such a builder, to which you palm off this file. After that, every time you start the project (Ctrl + F11), the JAR will be collected automatically if there have been changes in the source code.

This option of permanent assembly of the JAR is not particularly convenient, because may take an uncomfortable time. Instead, you can create this builder in External Tools (Run -> External Tools -> External Tools Configurations) and run the JAR assembly only when you really need it.

You have a class Myapp.classwhich is in the package my.application. You can put image files for example in my.application.images and access them from the MyApp class like this:
Or from any other class:

Using this approach, you can access both unpackaged resource files and those that are packed in the JAR along with classes.