I have installed an application, when I try to run it (it\'s an executable jar) nothing happens. When I run it from the commandline with:
java -jar \
I personally think all the answers here are mis-understanding the question. The answer to this lies in the difference of how spring-boot builds the .jar. Everyone knows that Spring Boot sets up a manifest like this, which varies from everyones asssumption that this is a standard .jar launch, which it may or may not be :
Start-Class: com.myco.eventlogging.MyService
Spring-Boot-Classes: BOOT-INF/classes/
Spring-Boot-Lib: BOOT-INF/lib/
Spring-Boot-Version: 1.4.0.RELEASE
Created-By: Apache Maven 3.3.9
Build-Jdk: 1.8.0_131
Main-Class: org.springframework.boot.loader.JarLauncher
Perhaps it needs to executed with org.springframework.boot.loader.JarLauncher
on the classpath?
I faced the same issue and it's fixed now:) Just follow the below steps and the error could be for anything, but the below steps makes the process smoother. I spend lot of time to find the fix.
1.Try restart the Eclipse (if you are using Eclipse to built JAR file) --> Actually this helped my issue in exporting the JAR file properly.
2.After eclipse restart, try to see if your eclipse is able to recognize the main class/method by your Java project --> right click --> Run as --> Run configurations --> Main --> click Search button to see if your eclipse is able to lookup for your main class in the JAR file. --> This is for the validation that JAR file will have the entry point to the main class.
After this, export your Java Dynamic project as "Runnable JAR" file and not JAR file.
In Java launch configuration, choose your main class.
Once export the jar file, use the below command to execute. java -cp [Your JAR].jar [complete package].MainClass eg: java -cp AppleTCRuleAudit.jar com.apple.tcruleaudit.classes.TCRuleAudit
You might face the unsupported java version error. the fix is to change the java_home in your shell bash profile to match the java version used to compile the project in eclipse.
Hope this helps! Kindly let me know if you still have any issues.
I had the same problem. A lot of the solutions mentioned here didn't give me the whole picture, so I'll try to give you a summary of how to pack jar files from the command line.
If you want to have your .class
files in packages, add the package in the beginning of the .java
.
Test.java
package testpackage;
public class Test
{
...
}
To compile your code with your .class
files ending up with the structure given by the package name use:
javac -d . Test.java
The -d .
makes the compiler create the directory structure you want.
When packaging the .jar
file, you need to instruct the jar routine on how to pack it. Here we use the option set cvfeP
. This is to keep the package structure (option P
), specify the entry point so that the manifest file contains meaningful information (option e
). Option f
lets you specify the file name, option c
creates an archive and option v
sets the output to verbose. The important things to note here are P
and e
.
Then comes the name of the jar we want test.jar
.
Then comes the entry point .
And then comes -C . <packagename>/
to get the class files from that folder, preserving the folder structure.
jar cvfeP test.jar testpackage.Test -C . testpackage/
Check your .jar
file in a zip program. It should have the following structure
test.jar
META-INF
| MANIFEST.MF
testpackage
| Test.class
The MANIFEST.MF should contain the following
Manifest-Version: 1.0
Created-By: <JDK Version> (Oracle Corporation)
Main-Class: testpackage.Test
If you edit your manifest by hand be sure to keep the newline at the end otherwise java doesn't recognize it.
Execute your .jar
file with
java -jar test.jar
First, it's kind of weird, to see you run java -jar "app"
and not java -jar app.jar
Second, to make a jar executable... you need to jar a file called META-INF/MANIFEST.MF
the file itself should have (at least) this one liner:
Main-Class: com.mypackage.MyClass
Where com.mypackage.MyClass
is the class holding the public static void main(String[] args) entry point.
Note that there are several ways to get this done either with the CLI, Maven, Ant or Gradle:
For CLI, the following command will do: (tks @dvvrt)
jar cmvf META-INF/MANIFEST.MF <new-jar-filename>.jar <files to include>
For Maven, something like the following snippet should do the trick. Note that this is only the plugin definition, not the full pom.xml:
<build>
<plugins>
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.mypackage.MyClass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
(Pick a <version> appropriate to your project.)
For Ant, the snippet below should help:
<jar destfile="build/main/checksites.jar">
<fileset dir="build/main/classes"/>
<zipfileset includes="**/*.class" src="lib/main/some.jar"/>
<manifest>
<attribute name="Main-Class" value="com.acme.checksites.Main"/>
</manifest>
</jar>
Credits Michael Niemand -
For Gradle:
plugins {
id 'java'
}
jar {
manifest {
attributes(
'Main-Class': 'com.mypackage.MyClass'
)
}
}
That should have been java -jar app.jar
instead of java -jar "app"
.
The -jar
option only works if the JAR file is an executable JAR file, which means it must have a manifest file with a Main-Class
attribute in it. See Packaging Programs in JAR Files to learn how to create an executable JAR.
If it's not an executable JAR, then you'll need to run the program with something like:
java -cp app.jar com.somepackage.SomeClass
where com.somepackage.SomeClass
is the class that contains the main
method to run the program. (What that class is depends on the program, it's impossible to tell from the information you've supplied).
Just to make one point clear about
Main-Class: <packagename>.<classname>
If you don't have package you have to ignore that part, like this:
Main-Class: <classname>