How to Run a Jar File

Running JAR file on Windows

Easiest route is probably upgrading or re-installing the Java Runtime Environment (JRE).

Or this:

  • Open the Windows Explorer, from the Tools select 'Folder Options...'
  • Click the File Types tab, scroll down and select JAR File type.
  • Press the Advanced button.
  • In the Edit File Type dialog box, select open in Actions box and click Edit...
  • Press the Browse button and navigate to the location the Java interpreter javaw.exe.
  • In the Application used to perform action field, needs to display something similar to C:\Program Files\Java\j2re1.4.2_04\bin\javaw.exe" -jar "%1" % (Note: the part starting with 'javaw' must be exactly like that; the other part of the path name can vary depending on which version of Java you're using) then press the OK buttons until all the dialogs are closed.

Which was stolen from here: http://windowstipoftheday.blogspot.com/2005/10/setting-jar-file-association.html

Run jar file in command prompt

Try this

java -jar <jar-file-name>.jar

Run a JAR file from the command line and specify classpath

When you specify -jar then the -cp parameter will be ignored.

From the documentation:

When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored.

You also cannot "include" needed jar files into another jar file (you would need to extract their contents and put the .class files into your jar file)

You have two options:

  1. include all jar files from the lib directory into the manifest (you can use relative paths there)
  2. Specify everything (including your jar) on the commandline using -cp:

    java -cp MyJar.jar:lib/* com.somepackage.subpackage.Main

How can I run a jar file?

There is at least one cause, because there is no single command with the name

"java -jar C:/Users/lordb/Desktop/Server/NewBuildTest/spigot-1.18.1.jar"

What you want, is to execte the command "java" with the 2 arguments "-jar" and "C:/Users/lordb/Desktop/Server/NewBuildTest/spigot-1.18.1.jar". You can do that by passing the command and its arguments via String array. In your case that would be an array of 3 Strings.

Javadoc: https://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html#exec(java.lang.String[])

In that javadoc you can see that exec returns a new Process object. Such Process provides more information, e.g. the exit code of your process (via waitFor) and access to (error) output messages (via getInputStream).

Javadoc:
https://docs.oracle.com/javase/7/docs/api/java/lang/Process.html

Example: Printing a Java InputStream from a Process

Note: You didn't specify which Java version you are using, so i just picked a "random" version for the javadoc links. You might need to look for the version you are using.



Related Topics



Leave a reply



Submit