Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a java executable with Eclipse

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.

Thanks!

like image 778
Micah Avatar asked Mar 13 '11 23:03

Micah


People also ask

Can Java create executable?

The jar (Java Archive) tool of JDK provides the facility to create the executable jar file. An executable jar file calls the main method of the class if you double click it. To create the executable jar file, you need to create .

How do I make a Java jar executable?

Right click on your Java Project and select Export. Select Java -> Runnable JAR file -> Next. Select the Destination folder where you would like to save it and click Finish.


Video Answer


2 Answers

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

like image 84
adarshr Avatar answered Oct 23 '22 10:10

adarshr


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.

like image 21
whirlwin Avatar answered Oct 23 '22 09:10

whirlwin