30

I tried to run a jar file by going to Run configurations and then creating a new Java App, but Eclipse wants me to give it a main class and reference to the project. Can't I just give Eclipse a jar file?

7 Answers 7

58

You cannot directly execute a jar file. Instead, you need to run it using the java -jar command. In order to do that you:

  1. Go to Run > External Tools > External tools Configurations
  2. Create an item under "Program"
  3. Fill in:

    Location: /usr/bin/java (or the route to your java executable, in Linux you can find this by running which java on a terminal, in Windows you can use where java in command prompt)

    Working directory: The working directory for your jar program (usually its location)

    Arguments: -jar [path to your jar]

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

1 Comment

On windows you can use where java instead of which java. If you want a JDK and not a JRE you need to look in the returned directories for 'bin/javac/`
13

You could run the JAR with an "External Tool" launch configuration, found at Run > External Tools > External tools Configurations.

3 Comments

:D thanks! I found it under Run > External Tools > External tools Configurations and then selected new Program.
I also try this but it is giving me this issues Exception occurred executing command line. Cannot run program "D:\CapsON.jar" (in directory "D:\"): CreateProcess error=193, %1 is not a valid Win32 application
@SanjayJain I got a similar error and found GermanK's answer helpful.
5

Run -> External Tools -> External Tools Configuration.

Program -> New

enter image description here

Comments

2

Is the jar file you provided have "public static void main(String [] arg)" method ? In order for java to run your and application, a main class with a "public static void main(String [] arg)" method must exist. So that jvm will understand the starting execution point of your jar.

Comments

2

Just create a Java application and start it with the jar flag:

enter image description here

You can use eclipse variables such as workspace_loc or git_work_tree to point to your jar-file.

Comments

1

Try the following command line

C:\Program Files (x86)\Java\jdk1.6.0_24\bin\javaw.exe 

with the following arguments (one per line)

-cp C:\dev\tcpmon-1.0-bin\build\tcpmon-1.0.jar org.apache.ws.commons.tcpmon.TCPMon 

Comments

-2

Try running 'java -jar yourprogramname' in a console.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.