14

This is a totally newbie question. I'm running Eclipse on Ubuntu. I created a test project that I want to compile to an executable (whataver the linux equivalent is of a Windows .exe file). Here's the contents of my program:

public class MyTest {
    public static void main(String[] args) {

        System.out.println("You passed in: " + args[0]);

    }
}

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

5 Answers 5

26

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

Right click on the project and select JAR file under Export.

enter image description here

Enter the path where you want it to be saved. The example here is of Windows. Change according to your platform.

enter image description here

Click Next.

enter image description here

Edit the Main Class field by browsing and selecting the location of your class that contains the main method.

enter image description here

Run it

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

java -jar path/to/test.jar

Sign up to request clarification or add additional context in comments.

3 Comments

how come I would choose jar file over runnable jar file?
Try it. It's a new feature which didn't exist in earlier versions of eclipse.
I'll give you some bonus points if you tell me how to compile it with dependencies located elsewhere in the system. I added them to the build path, but get errors when I try to run it.
3

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

2 Comments

To execute it, type java -jar myapp.jar in the terminal.
This worked like a charm, The simple jar option was failing because it was not able to get the class path of dependent jar. Thank you !!
2

The Eclipes tutorials are very helpful, if you do the "create a Hello World application" tutorial it will walk you through the process of setup the project, building the app and running the jar file.

Comments

1

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 <args>.

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

how come I would choose jar file over executable jar file?

  • Because a JAR file is portable, and a binary executable is not.
  • Because JIT compiled code runs faster than code that is compiled ahead of time.
  • Because the standard Java tool chain does not support creation of binary executables. Ditto for Eclipse, AFAIK.

Comments

0

Marked solution does not work. Use "runnable" jar instead and then java -jar <jarfile>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.