I am new to Gatling (2.1.2) and want to do a small prototype project to show to my colleagues.
According to the quick start page, there are several ways I can run a simulation with Gatling:
decompress the Gatling bundle into a folder and drop my simulation files into user-files/simulations folder. bin/gatling.sh will compile and run the simulation files.
use the gatling-maven-plugin maven plugin to execute the simulation.
create a project with gatling-highcharts-maven-archetype, and run the Engine class.
and I found those problems
For 1, it is hard to add dependencies for simulation classes. I have to figure out what the jars are needed and drop them to the lib folder.
For 2, it requires maven to be installed.
For 3, it only runs from an IDE
I just want a simple executable JAR file with all the dependencies bundled together (my simulation, Gatling and third party), and run it from any machine (like EC2 instances).
Is there a way to achieve this?
Update 1:
I tried method 3, but moving all the project files from test folder to main, and used maven-assembly-plugin to build a jar with dependencies. When I tried to run the file, I got the following error:
Exception in thread "main" java.lang.ExceptionInInitializerError
at Engine$.delayedEndpoint$Engine$1(Engine.scala:7)
at Engine$delayedInit$body.apply(Engine.scala:4)
at scala.Function0$class.apply$mcV$sp(Function0.scala:40)
at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
at scala.App$$anonfun$main$1.apply(App.scala:76)
at scala.App$$anonfun$main$1.apply(App.scala:76)
at scala.collection.immutable.List.foreach(List.scala:381)
at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:35)
at scala.App$class.main(App.scala:76)
at Engine$.main(Engine.scala:4)
at Engine.main(Engine.scala)
Caused by: java.nio.file.FileSystemNotFoundException
at com.sun.nio.zipfs.ZipFileSystemProvider.getFileSystem(ZipFileSystemProvider.java:171)
at com.sun.nio.zipfs.ZipFileSystemProvider.getPath(ZipFileSystemProvider.java:157)
at java.nio.file.Paths.get(Paths.java:143)
at io.gatling.core.util.PathHelper$.uri2path(PathHelper.scala:32)
at IDEPathHelper$.<init>(IDEPathHelper.scala:7)
at IDEPathHelper$.<clinit>(IDEPathHelper.scala)
... 11 more
I guess this is something to do with Gatling configuration, but don't know what has gone wrong.
I tried to do something similar. I could not use Maven as well. I will try to remember how I did this.
1) I have configured maven-assembly-plugin to generate single JAR with dependencies like this:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
You need to ensure all required libraries (gatling, scala runtime, zinc compiler) are present on your resulting classpath.
2) Check the scope of your dependencies as Maven packs only classes defined with scope=compile by default. The most simple way is probably to use no test dependencies.
3) Create a launch script, e.g. launch.sh. It should contain something like this:
#!/bin/sh
USER_ARGS="-Dsomething=$1"
COMPILATION_CLASSPATH=`find -L ./target -maxdepth 1 -name "*.jar" -type f -exec printf :{} ';'`
JAVA_OPTS="-server -XX:+UseThreadPriorities -XX:ThreadPriorityPolicy=42 -Xms512M -Xmx2048M -XX:+HeapDumpOnOutOfMemoryError -XX:+AggressiveOpts -XX:+OptimizeStringConcat -XX:+UseFastAccessorMethods -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled -Djava.net.preferIPv4Stack=true -Djava.net.preferIPv6Addresses=false ${JAVA_OPTS}"
java $JAVA_OPTS $USER_ARGS -cp $COMPILATION_CLASSPATH io.gatling.app.Gatling -s your.simulation.FullClassName
To explain, I took gatling`s own launch script for inspiration. Note mainly the presence of target directory in classpath parameter definition.
4) Compile your compiled target directory and launch.sh to a single directory and distribute this (e.g. as archive). Then you can the scenarios by executing ./launch.sh.
I know this is not a standard solution, but it worked for me. Hopefully it will help you too. If you have any problems or tips to improve, please share with us.
I think is a bit late for that but I face kinda the same problem related here, but instead to use maven I used gradle. Guess that the approach it's the same, a bit mix of the first solution and something or my own.
First, define a gradle build file with gatling dependencies and a task to build a fatjar
apply plugin: 'scala'
version 0.1
dependencies {
compile group: 'io.gatling', name: 'gatling-test-framework', version: '2.1.7'
compile group: 'com.typesafe.akka', name: 'akka-actor_2.11', version: '2.4.7'
compile group: 'org.scala-lang', name: 'scala-library', version: '2.11.7'
}
repositories{
mavenCentral()
mavenLocal()
}
task fatJar(type: Jar) {
manifest {
attributes 'Implementation-Title': 'Preparing test',
'Implementation-Version': version,
'Main-Class': 'io.gatling.app.Gatling'
}
baseName = project.name + '-all'
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } } {
exclude 'META-INF/MANIFEST.MF'
exclude 'META-INF/*.SF'
exclude 'META-INF/*.DSA'
exclude 'META-INF/*.RSA'
}
with jar
}
That task executed as
gradle clean build fatJar
will generate a self contained jar which will run the Gatling main class as default. So tell it witch test you want to run is made with the standard '-s' parameter.
So last step is create, if you want, a script to run it. I will "steal" the script for the first comment and change a bit
#!/bin/sh
if [ -z "$1" ];
then
echo "Test config tool"
echo
echo "Running Parameters : "
echo
echo " <Config file> : Test definition file. Required"
echo
exit 0;
fi
USER_ARGS="-DCONFIG_FILE=$1"
JAVA_OPTS="-server -XX:+UseThreadPriorities -XX:ThreadPriorityPolicy=42 -Xms512M -Xmx2048M -XX:+HeapDumpOnOutOfMemoryError -XX:+AggressiveOpts -XX:+OptimizeStringConcat -XX:+UseFastAccessorMethods -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled -Djava.net.preferIPv4Stack=true -Djava.net.preferIPv6Addresses=false ${JAVA_OPTS}"
java $JAVA_OPTS $USER_ARGS -jar test-project-all-0.1.jar -s FunctionalTestSimulation -nr
In my case I will run the same test with different, easy to configure, parameters so my Simulation is always the same.
All my scala files are compiled by gradle and package in the jar that's mean they are in the classpath, changing the "FunctionalTestSimulation" name for a Script variable make easy adapt this script for something more generic.
Guess that make a Maven version will be easy.
Hope that help somebody.
Update with folder structure
After a request will add an small draft of the folder structure for the project:
test-project
|_ build.gradle
|_ src
|_ main
|_ scala
|_ resources
|_ runSimulation.sh
|_ configFile.conf
When have time will provide a link to my github with a working one.
Cheers
You can always create a simple Java class that starts Gatling with the Gatling.fromArgs. With this setup you can have all in just one happy executable jar. Let this class be the jar mainClass instead of "io.gatling.app.Gatling". This example is for a scala simulation class "my.package.MySimulation".
import scala.Option;
import io.gatling.app.Gatling;
import io.gatling.core.scenario.Simulation;
public class StartSimulation {
public static void main(String[] args) {
Gatling.fromArgs(new String[]{}, new Option<Class<Simulation>>() {
private static final long serialVersionUID = 1L;
#Override
public int productArity() {
return 0;
}
#Override
public Object productElement(int arg0) {
return null;
}
#SuppressWarnings("unchecked")
#Override
public Class<Simulation> get() {
try {
return (Class<Simulation>) Class.forName("my.package.MySimulation");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return null;
}
#Override
public boolean isEmpty() {
return false;
}
#Override
public boolean canEqual(Object o) {
return false;
}
});
}
}
I had a similar issue, I fixed it as following:
Inside Gatling package there is bin/ and take a look at gatling.sh. You see that it simply adds certain configurations into classpath and then runs io.gatling.app.Gatling class in gatling-compiler-<version_number>.jar. So, all you need to do is to make a jar that includes compiler, add configurations and tests to classpath and run io.gatling.app.Gatling.
steps:
add compiler dependency:
<dependency>
<groupId>io.gatling</groupId>
<artifactId>gatling-compiler</artifactId>
<version>${gatling.version}</version>
</dependency
create jar with dependencies:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4.1</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<finalName>${project.build.finalName}</finalName>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
create test jar (this includes your gatling tests)
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
<configuration>
<excludes>
<exclude>src/test/resources/*</exclude>
</excludes>
<finalName>${project.build.finalName}</finalName>
</configuration>
</execution>
</executions>
</plugin>
create a package out of your configuration. You can use maven assembly for that. What I usually do, is to create a separate module that handles creating the package for different environments. This package contains your gatling.conf, logback.xmland all the other resources you application wants including test data.
Now you basically have three packages: application.jar, application-tests.jar and application-conf.zip.
Unzip application-conf.zip, copy application.jarand application-tests.jarin the same folder.
In this folder, You need to create target/test-classes/ folder, just
leave it empty. In my case, it was required. I think you can some how
change that in gatling.conf. But I am not sure how.
Run
java -cp ".:application-test.jar:application.jar" io.gatling.app.Gatling
I use IntelliJ Idea and I got this fixed by right clicking on the scala folder > Mark Directory as > Test Sources Root . Now Execute "Engine" and you will be all good !
I've recently blogged about this Creating a versionable, self-contained (fat-/uber-) JAR for Gatling tests, the source of which can be found in jamietanna/fat-gatling-jar.
For a Maven project, the steps would be as follows.
The main things you need are to add the dependency on gatling-charts-highcharts:
<project>
<!-- ... -->
<dependencies>
<dependency>
<groupId>io.gatling.highcharts</groupId>
<artifactId>gatling-charts-highcharts</artifactId>
<version>${gatling.version}</version>
</dependency>
</dependencies>
</project>
Next, you need to make sure your Gatling scenarios/simulations are in src/main instead of src/test.
Finally, you can use the maven-shade-plugin to build an executable JAR which uses Gatling's CLI runner as the mainClass:
<project>
<!-- ... -->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.1.1</version>
<configuration>
<filters>
<!-- https://stackoverflow.com/a/6743609 -->
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>io.gatling.app.Gatling</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Related
I have a Java 18, JavaFX 18 Maven project which has a lot of libraries, beside the javaFX libraries, that needs to be included in the artifact. I want to create an artifact, a jar, which contains all dependencies. I started following this video to create the jar: https://www.youtube.com/watch?v=UKd6zpUnAE4
Summarizing my steps, and referring to the steps in the video:
In IntelliJ in Project Structure/Project Settings/Libraries I removed all Maven added libraries, and added C:\Program Files\Java\javafx-sdk-18.0.2\lib
After, in Run/Edit Configurations... I added a VM options, and in that window I added
--module-path "C:\Program Files\Java\javafx-sdk-18.0.2\lib"
--add-modules javafx.controls,javafx.fxml
After, in the video, "Ken" the host of the video creates a class, with a main() method, that runs the application original main() class. I did not need this step, because I already has a class that does the same.
After, File/Project Structure/Project Settings/Artifact/ I added a JAR/From modules with dependencies/ and I choose the class I recently created, and shortened the path until the source folder (src)
Following this step, after I clicked add (+), and added the content of "...javafx-sdk-18.0.2/bin" all dll's and everything (all files).
Here, at this point, separate from the video, I also created a folder named "jars" and put all Maven dependencies jars in that folder.
According to the video, after these steps, with a double click on the artifact the jar runs without a problem.
However, I needed I more step. My dependency jars are signed jars, so I needed to open the artifact with WinRAR and remove the *.SF, *.DSA and *.RSA files. Earlier this caused me problems so I followed the idea here: Invalid signature file digest for Manifest main attributes exception while trying to run jar file, and here: "Invalid signature file" when attempting to run a .jar
After this, everything should be fine, however not :( The jar doesn't run on double click. When I run it from command line, I receive the following error:
$ java -jar jHasher.jar
jan. 15, 2023 3:19:07 DU. com.sun.javafx.application.PlatformImpl startup
WARNING: Unsupported JavaFX configuration: classes were loaded from 'unnamed module #3a178016'
javafx.fxml.LoadException:
unknown path:53
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2707)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2685)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2532)
at view.GUI.start(GUI.java:29)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:847)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:484)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at com.sun.javafx.fxml.BeanAdapter.put(BeanAdapter.java:263)
at com.sun.javafx.fxml.BeanAdapter.put(BeanAdapter.java:54)
at javafx.fxml.FXMLLoader$Element.applyProperty(FXMLLoader.java:523)
at javafx.fxml.FXMLLoader$Element.processValue(FXMLLoader.java:373)
at javafx.fxml.FXMLLoader$Element.processPropertyAttribute(FXMLLoader.java:335)
at javafx.fxml.FXMLLoader$Element.processInstancePropertyAttributes(FXMLLoader.java:245)
at javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:778)
at javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2924)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2639)
... 11 more
Caused by: java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:119)
at java.base/java.lang.reflect.Method.invoke(Method.java:577)
at com.sun.javafx.fxml.ModuleHelper.invoke(ModuleHelper.java:102)
at com.sun.javafx.fxml.BeanAdapter.put(BeanAdapter.java:259)
... 19 more
Caused by: java.lang.UnsupportedOperationException: Cannot resolve 'win10-document'
at org.kordamp.ikonli.AbstractIkonResolver.resolve(AbstractIkonResolver.java:61)
at org.kordamp.ikonli.javafx.IkonResolver.resolve(IkonResolver.java:73)
at org.kordamp.ikonli.javafx.FontIcon.setIconLiteral(FontIcon.java:251)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
... 22 more
I have searched the following error message. I also found some posts on StackOverflow, however they are not clear to me, and I was not able to fix this issue. Please, guide me how to proceed. All suggestions are highly appreciated.
After several hard day, I was able to create the executable jar. I'd like to share the know-how with you.
After 5th step, skipping the WinRAR for removing the *.SF, *.DSA and *.RSA files. I added maven-shade-plugin to my pom.xml. The shade plugin can automatically remove these unwanted files, but unfortunately by itself cannot create a runnable JAR, because throws again exceptions and doesn't run on double click (JavaFX 18 Maven IntelliJ: Graphics Device initialization failed for: d3d, sw Error initializing QuantumRenderer: no suitable pipeline found).
To avoid this exception and include the unlocated/missing JavaFX files we have to repack the already packed JAR. To do that, I used the spring-boot-maven-plugin. After setting up the plugins (code below), you have to run the plugins with maven in a correct order! My maven command was the following: mvn clean package spring-boot:repackage
That it, finally the created JAR (JAR of the JAR) can run on double click.
My pom.xml's corresponding parts:
Shade plugin setting:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.4.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>controller.Start</mainClass>
</transformer>
</transformers>
<minimizeJar>true</minimizeJar>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
The Spring-boot-maven-plugin setting (this should be placed outside the plugins section, at the very end of the pom.xml):
<pluginManagement>
<plugins>
<plugin>
<!-- mvn clean package spring-boot:repackage -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<classifier>spring-boot</classifier>
<mainClass>
controller.Start
</mainClass>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
Make sure to run the plugins in the correct order, as mentioned above! I found this resource very useful: https://www.baeldung.com/spring-boot-repackage-vs-mvn-package
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 "app.jar"
I get the following message:
no main manifest attribute, in "app.jar"
Normally, if I had created the program myself, I would have added a main class attribute to the manifest file. But in this case, since the file is from an application, i cannot do that. I also tried extracting the jar to see if I could find the main class, but there are to many classes and none of them has the word "main" in it's name. There must be a way to fix this because the program runs fine on other systems.
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:
Latest doc on this plugin: see https://maven.apache.org/plugins/maven-jar-plugin/
<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).
Alternatively, you can use maven-assembly-plugin, as shown in the below example:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.package.MainClass</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
In this example all the dependency jars as specified in section will be automatically included in your single jar. Note that jar-with-dependencies should be literally put as, not to be replaced with the jar file names you want to include.
That is because Java cannot find the Main attribute in the MANIFEST.MF file.
The Main attribute is necessary to tell java which class it should use as the application's entry point. Inside the jar file, the MANIFEST.MF file is located in META-INF folder. Wondering how you could look at what's inside a jar file? Open the jar file with WinRAR.
The main attribute inside the MANIFEST.MF looks like this:
Main-Class: <packagename>.<classname>
You get this "no main manifest attribute" error when this line is missing from the MANIFEST.MF file.
It's really a huge mess to specify this attribute inside the MANIFEST.MF file.
Update: I just found a really neat way to specify the Application's entry point in eclipse.
When you say Export,
Select Jar and next
[ give it a name in the next window ] and next
and next again
and you'll see " Select the class of the application entry point".
Just pick a class and Eclipse will automatically build a cool MANIFEST.MF for you.
I had the same issue. by adding following lines to pom file made it work. The plugin will make sure the build process of your application with all necessary steps.
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
I had this issue when creating a jar using IntelliJ IDEA. See this discussion.
What solved it for me was to re-create the jar artifact, choosing JAR > From modules with dependencies, but not accepting the default Directory for META-INF/MANIFEST.MF. Change it from -/src/main/java to -/src/main/resources.
Otherwise it was including a manifest file in the jar, but not the one in -/src/main/java that it should have.
The Gradle answer is to add a jar/manifest/attributes setting like this:
apply plugin: 'java'
jar {
manifest {
attributes 'Main-Class': 'com.package.app.Class'
}
}
For maven, this is what solved it (for me, for a Veetle codebase on GitHub):
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>org.lazydevs.veetle.api.VeetleAPI</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Cheers...
Try this command to include the jar:
java -cp yourJarName.jar your.package..your.MainClass
For me, none of the answers really helped - I had the manifest file in correct place, containing the Main-Class and everything. What tripped me over was this:
Warning: The text file from which you are creating the manifest must
end with a new line or carriage return. The last line will not be
parsed properly if it does not end with a new line or carriage return.
(source). Adding a newline at the end of the manifest fixed it.
If using Maven, include following in the pom
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
The MAVEN problem is that its try to include the first MANIFEST.MF file from first library from dependencies instead of THE OUR OWN MANIFEST.MF WHEN YOU USE ARTIFACTS!.
Rename yourjar.jar to yourjar.zip
Open MANIFEST.MF file from META-INF\MANIFEST.MF
Copy the real MANIFEST.MF that already generate in your project by MAVEN
That include somelike that:
Manifest-Version: 1.0
Main-Class: yourpacket.yourmainclass (for exmaple info.data.MainClass)
Replace the content of MANIFEST.MF from youjar.zip with it.
Rename yourjar.zip to yourjar.jar back.
Now java -jar yourjar.jar work perfectly.
OR!
Simple create you own MANIFEST.MF and:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<archive>
<manifestFile> Your path like: src/main/resources/META-INF/MANIFEST.MF </manifestFile>
<index>true</index>
<manifest>
<addClasspath>true</addClasspath>
</manifest>
</archive>
</configuration>
</plugin>
But if you use maven panel (or maven command line) you can force it to generate own manifest and include it into JAR file.
Add to the you pom.xml's build section this code:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<index>true</index>
<manifest>
<addClasspath>true</addClasspath>
<mainClass> yourpacket.yourmainclass (for exmaple info.data.MainClass)</mainClass>
</manifest>
<manifestEntries>
<mode>development</mode>
<url>${project.url}</url>
</manifestEntries>
</archive>
</configuration>
</plugin>
Open the MAVEN panel (in Intellij) and execute "Install". It will generate the MANIFEST file and compile property the JAR file with all dependencies into the "Target" folder. Also it will be installed to the local maven repository.
I had the same issue today. My problem was solved my moving META-INF to the resources folder.
I got same error just now.
If u're using gradle, just add next one in ur gradle.build:
apply plugin: 'java'
jar {
manifest {
attributes 'Main-Class': 'com.company.project.MainClass'
}
}
Where com.company.project.MainClass path to ur class with public static void main(String[] args) method.
If the jar isn't following the rules, it's not an executable jar.
If you are using the command line to assemble .jar it is possible to point to the main without adding Manifest file. Example:
jar cfve app.jar TheNameOfClassWithMainMethod *.class
(param "e" does that: TheNameOfClassWithMainMethod is a name of the class with the method main() and app.jar - name of executable .jar and *.class - just all classes files to assemble)
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
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 found a new solution to bad manifest generation !
Open the jar file with a zip editor like WinRAR
Click on for META-INF
Add or edit
Add:
Create a text file called MANIFEST.MF in a folder called META-INF
and add the following line:
Manifest-Version: 1.0
Main-Class: package.ex.com.views.mainClassName
Save the file and add it to the zip
Edit:
Drag the file out modify the MANIFEST.MF to add the previous line
Open cmd and type: java -jar c:/path/JarName.jar
It should work fine now !
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 tried this and it worked for me.
mvn clean install package should work.
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Any executable jar file Should run either by clicking or running using command prompt like java -jar app.jar (use "if path of jar contains space" - i.e. java -jar "C:\folder name\app.jar"). If your executable jar is not running, which means it is not created properly.
For better understanding, extract the jar file (or view using any tool, for windows 7-Zip is nice one) and check the file under /META-INF/MANIFEST.MF. If you find any entry like
Main-Class: your.package.name.ClaaswithMain - then it's fine, otherwise you have to provide it.
Be aware of appending Main-Class entry on MANIFEST.MF file, check where you are saving it!
You Can Simply follow this step
Create a jar file using
jar -cfm jarfile-name manifest-filename Class-file name
While running the jar file simple run like this
java -cp jarfile-name main-classname
You might not have created the jar file properly:
ex: missing option m in jar creation
The following works:
jar -cvfm MyJar.jar Manifest.txt *.class
For my case the problem is <pluginManagement> under <build> makes things cannot work properly.
My original pom.xml:
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
...
...
...
</pluginManagement>
</build>
After removing <pluginManagement>, the error is gone.
For me this error occurred simply because I forgot tell Eclipse that I wanted a runnable jar file and not a simple library jar file. So when you create the jar file in Eclipse make sure that you click the right radio button
The above answers were only partly helpful for me. java -cp was part of the answer, but I needed more specific info on how to identify the class to run. Here is what worked for me:
Step 1: find the class I need to run
jar tf /path/to/myjar.jar | more
The top lines of the result were:
META-INF/
META-INF/MANIFEST.MF
somepath/
somepath/App.class
META-INF/maven/
...
App.class contained the main class to run. I'm not 100% sure if you can always assume the class you need is the first one, but it was for me. If it isn't, I'd imagine it isn't too hard to use grep to exclude library-related results to pare the class list down to a manageable size.
From there it was easy: I just use that path (minus the ".class" suffix):
java -cp /path/to/myjar.jar somepath/App
(first post - so it may not be clean)
This is my fix for OS X 11.6, Maven-based Netbeans 8.2 program. Up to now my app is 100% Netbeans - no tweaking (just a few shell escapes for the impossible!).
Having tried most all of the answers here and elsewhere to no avail, I returned to the art of "use what works".
The top answer here (olivier-refalo thanx) looked like the right place to start but didn't help.
Looking at other projects which did work, I noticed some minor differences in the manifest lines:
addClasspath, classpathPrefix were absent (deleted them)
mainClass was missing the "com." (used the NB -> Project
Properties->Run->Main Class->Browse to specify)
Not sure why (I am only 3 months into java) or how, but can only say this worked.
Here is just the modified manifest block used:
<manifest>
<mainClass>mypackage.MyClass</mainClass>
</manifest>
most of the solutions did not work for me but my instructor helped me out
i would like to share his solution here
i used kali linux terminal but should be fine in all debian
javac *.java
nano MANIFEST.MF
in the file type
Main-Class: Main
or whatever your main file name is (make sure to add package name if it exists)
jar -cvmf MANIFEST.MF new.jar *.class
now to run the file use
java -jar new.jar
or you can go to propeties of file and check
Allow Execution of file as program
double click on it
it helped me while most of the above answers did not
Since you've add MANIFEST.MF, I think you should consider the order of Field in this file. My env is java version "1.8.0_91"
and my MANIFEST.MF as here
// MANIFEST.MF
Manifest-Version: 1.0
Created-By: 1.8.0_91 (Oracle Corporation)
Main-Class: HelloWorldSwing
// run
~ java -jar HelloWorldSwing.jar
no main manifest attribute, in HelloWorldSwing.jar
However, this as below run through
Manifest-Version: 1.0
Main-Class: HelloWorldSwing
Created-By: 1.8.0_91 (Oracle Corporation)
//this run swing normally
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 "app.jar"
I get the following message:
no main manifest attribute, in "app.jar"
Normally, if I had created the program myself, I would have added a main class attribute to the manifest file. But in this case, since the file is from an application, i cannot do that. I also tried extracting the jar to see if I could find the main class, but there are to many classes and none of them has the word "main" in it's name. There must be a way to fix this because the program runs fine on other systems.
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:
Latest doc on this plugin: see https://maven.apache.org/plugins/maven-jar-plugin/
<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).
Alternatively, you can use maven-assembly-plugin, as shown in the below example:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.package.MainClass</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
In this example all the dependency jars as specified in section will be automatically included in your single jar. Note that jar-with-dependencies should be literally put as, not to be replaced with the jar file names you want to include.
That is because Java cannot find the Main attribute in the MANIFEST.MF file.
The Main attribute is necessary to tell java which class it should use as the application's entry point. Inside the jar file, the MANIFEST.MF file is located in META-INF folder. Wondering how you could look at what's inside a jar file? Open the jar file with WinRAR.
The main attribute inside the MANIFEST.MF looks like this:
Main-Class: <packagename>.<classname>
You get this "no main manifest attribute" error when this line is missing from the MANIFEST.MF file.
It's really a huge mess to specify this attribute inside the MANIFEST.MF file.
Update: I just found a really neat way to specify the Application's entry point in eclipse.
When you say Export,
Select Jar and next
[ give it a name in the next window ] and next
and next again
and you'll see " Select the class of the application entry point".
Just pick a class and Eclipse will automatically build a cool MANIFEST.MF for you.
I had the same issue. by adding following lines to pom file made it work. The plugin will make sure the build process of your application with all necessary steps.
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
I had this issue when creating a jar using IntelliJ IDEA. See this discussion.
What solved it for me was to re-create the jar artifact, choosing JAR > From modules with dependencies, but not accepting the default Directory for META-INF/MANIFEST.MF. Change it from -/src/main/java to -/src/main/resources.
Otherwise it was including a manifest file in the jar, but not the one in -/src/main/java that it should have.
The Gradle answer is to add a jar/manifest/attributes setting like this:
apply plugin: 'java'
jar {
manifest {
attributes 'Main-Class': 'com.package.app.Class'
}
}
For maven, this is what solved it (for me, for a Veetle codebase on GitHub):
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>org.lazydevs.veetle.api.VeetleAPI</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Cheers...
Try this command to include the jar:
java -cp yourJarName.jar your.package..your.MainClass
For me, none of the answers really helped - I had the manifest file in correct place, containing the Main-Class and everything. What tripped me over was this:
Warning: The text file from which you are creating the manifest must
end with a new line or carriage return. The last line will not be
parsed properly if it does not end with a new line or carriage return.
(source). Adding a newline at the end of the manifest fixed it.
If using Maven, include following in the pom
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
The MAVEN problem is that its try to include the first MANIFEST.MF file from first library from dependencies instead of THE OUR OWN MANIFEST.MF WHEN YOU USE ARTIFACTS!.
Rename yourjar.jar to yourjar.zip
Open MANIFEST.MF file from META-INF\MANIFEST.MF
Copy the real MANIFEST.MF that already generate in your project by MAVEN
That include somelike that:
Manifest-Version: 1.0
Main-Class: yourpacket.yourmainclass (for exmaple info.data.MainClass)
Replace the content of MANIFEST.MF from youjar.zip with it.
Rename yourjar.zip to yourjar.jar back.
Now java -jar yourjar.jar work perfectly.
OR!
Simple create you own MANIFEST.MF and:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<archive>
<manifestFile> Your path like: src/main/resources/META-INF/MANIFEST.MF </manifestFile>
<index>true</index>
<manifest>
<addClasspath>true</addClasspath>
</manifest>
</archive>
</configuration>
</plugin>
But if you use maven panel (or maven command line) you can force it to generate own manifest and include it into JAR file.
Add to the you pom.xml's build section this code:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<index>true</index>
<manifest>
<addClasspath>true</addClasspath>
<mainClass> yourpacket.yourmainclass (for exmaple info.data.MainClass)</mainClass>
</manifest>
<manifestEntries>
<mode>development</mode>
<url>${project.url}</url>
</manifestEntries>
</archive>
</configuration>
</plugin>
Open the MAVEN panel (in Intellij) and execute "Install". It will generate the MANIFEST file and compile property the JAR file with all dependencies into the "Target" folder. Also it will be installed to the local maven repository.
I had the same issue today. My problem was solved my moving META-INF to the resources folder.
I got same error just now.
If u're using gradle, just add next one in ur gradle.build:
apply plugin: 'java'
jar {
manifest {
attributes 'Main-Class': 'com.company.project.MainClass'
}
}
Where com.company.project.MainClass path to ur class with public static void main(String[] args) method.
If the jar isn't following the rules, it's not an executable jar.
If you are using the command line to assemble .jar it is possible to point to the main without adding Manifest file. Example:
jar cfve app.jar TheNameOfClassWithMainMethod *.class
(param "e" does that: TheNameOfClassWithMainMethod is a name of the class with the method main() and app.jar - name of executable .jar and *.class - just all classes files to assemble)
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
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 found a new solution to bad manifest generation !
Open the jar file with a zip editor like WinRAR
Click on for META-INF
Add or edit
Add:
Create a text file called MANIFEST.MF in a folder called META-INF
and add the following line:
Manifest-Version: 1.0
Main-Class: package.ex.com.views.mainClassName
Save the file and add it to the zip
Edit:
Drag the file out modify the MANIFEST.MF to add the previous line
Open cmd and type: java -jar c:/path/JarName.jar
It should work fine now !
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 tried this and it worked for me.
mvn clean install package should work.
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Any executable jar file Should run either by clicking or running using command prompt like java -jar app.jar (use "if path of jar contains space" - i.e. java -jar "C:\folder name\app.jar"). If your executable jar is not running, which means it is not created properly.
For better understanding, extract the jar file (or view using any tool, for windows 7-Zip is nice one) and check the file under /META-INF/MANIFEST.MF. If you find any entry like
Main-Class: your.package.name.ClaaswithMain - then it's fine, otherwise you have to provide it.
Be aware of appending Main-Class entry on MANIFEST.MF file, check where you are saving it!
You Can Simply follow this step
Create a jar file using
jar -cfm jarfile-name manifest-filename Class-file name
While running the jar file simple run like this
java -cp jarfile-name main-classname
You might not have created the jar file properly:
ex: missing option m in jar creation
The following works:
jar -cvfm MyJar.jar Manifest.txt *.class
For my case the problem is <pluginManagement> under <build> makes things cannot work properly.
My original pom.xml:
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
...
...
...
</pluginManagement>
</build>
After removing <pluginManagement>, the error is gone.
For me this error occurred simply because I forgot tell Eclipse that I wanted a runnable jar file and not a simple library jar file. So when you create the jar file in Eclipse make sure that you click the right radio button
The above answers were only partly helpful for me. java -cp was part of the answer, but I needed more specific info on how to identify the class to run. Here is what worked for me:
Step 1: find the class I need to run
jar tf /path/to/myjar.jar | more
The top lines of the result were:
META-INF/
META-INF/MANIFEST.MF
somepath/
somepath/App.class
META-INF/maven/
...
App.class contained the main class to run. I'm not 100% sure if you can always assume the class you need is the first one, but it was for me. If it isn't, I'd imagine it isn't too hard to use grep to exclude library-related results to pare the class list down to a manageable size.
From there it was easy: I just use that path (minus the ".class" suffix):
java -cp /path/to/myjar.jar somepath/App
(first post - so it may not be clean)
This is my fix for OS X 11.6, Maven-based Netbeans 8.2 program. Up to now my app is 100% Netbeans - no tweaking (just a few shell escapes for the impossible!).
Having tried most all of the answers here and elsewhere to no avail, I returned to the art of "use what works".
The top answer here (olivier-refalo thanx) looked like the right place to start but didn't help.
Looking at other projects which did work, I noticed some minor differences in the manifest lines:
addClasspath, classpathPrefix were absent (deleted them)
mainClass was missing the "com." (used the NB -> Project
Properties->Run->Main Class->Browse to specify)
Not sure why (I am only 3 months into java) or how, but can only say this worked.
Here is just the modified manifest block used:
<manifest>
<mainClass>mypackage.MyClass</mainClass>
</manifest>
most of the solutions did not work for me but my instructor helped me out
i would like to share his solution here
i used kali linux terminal but should be fine in all debian
javac *.java
nano MANIFEST.MF
in the file type
Main-Class: Main
or whatever your main file name is (make sure to add package name if it exists)
jar -cvmf MANIFEST.MF new.jar *.class
now to run the file use
java -jar new.jar
or you can go to propeties of file and check
Allow Execution of file as program
double click on it
it helped me while most of the above answers did not
Since you've add MANIFEST.MF, I think you should consider the order of Field in this file. My env is java version "1.8.0_91"
and my MANIFEST.MF as here
// MANIFEST.MF
Manifest-Version: 1.0
Created-By: 1.8.0_91 (Oracle Corporation)
Main-Class: HelloWorldSwing
// run
~ java -jar HelloWorldSwing.jar
no main manifest attribute, in HelloWorldSwing.jar
However, this as below run through
Manifest-Version: 1.0
Main-Class: HelloWorldSwing
Created-By: 1.8.0_91 (Oracle Corporation)
//this run swing normally
I exportet a Jar of a JavaFX2 Project. It works fine, but when I run it on another machine there will be a
classDefNotFoundExeption: javafx.application.Application
Any hints how to tackle this Problem ?
This is my Manifest:
Class-Path: .
Main-Class: proj.view.Launcher
I also programmed Launcher that starts a Swing GUI in case JavaFX is not found.
Here is my Launcher Class
public class Launcher {
public static void main(String[] args) {
try {
Class c = javafx.application.Application.class;
proj.main.App.main(args);
}catch (NoClassDefFoundError e) {
String[] t = {"Swing Backup","Application start Error"};
MainFrame.remote(t);
}
}
}
The other computers are running on a Java installation that doesn't include JavaFX. It works on your machine because you do have JavaFX installed.
To test if javafx.application.Application is available, you need to use reflection, i.e.
boolean hasJavaFX;
try {
Class.forName("javafx.application.Application");
hasJavaFX = true;
} catch (ClassNotFoundException e) {
hasJavaFX = false;
}
if (hasJavaFX) {
MainFrame.remote(new String[] {"Swing Backup","Application start Error"});
} else {
proj.main.App.main(args);
}
You might get classDefNotFoundExeption: javafx.application.Application when you are running on machine having jdk above 11.
oracle has removed javaFX form JDK 11, hence need to provide dependency on javafx-controls module.
`<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>12-ea+9</version>
</dependency>`
add this to your dependency.
https://openjfx.io/
To me it looks like runnable jar issue if then same code is working through IDE's on those machines. You can try maven assembly plugin to pack your jar.
The Apache Maven Assembly Plugin allows users to aggregate the project output along with its dependencies, modules, site documentation, and other files into a single, runnable package.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<archive>
<manifest>
<mainClass>
package.your_main_class
</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
I figured it out:
It was the User Path variable that pointed to a JDK10 bin.
We changed the Path. Now it works with "java -jar programm.jar"
but not with "java programm.jar"
but not with a Regular click on the file. But we wrote a batchFile that Starts the Application with "java -jar" and it works fine.
Has anyone a explanation for that behavior ?
I have created a Java project in Eclipse and successfully executed it directly from Eclipse on my Windows PC. Now I have to run the same java program on Linux server.
I have tried to copy the .class files from my PC to server and run it but it didn't work. After that I copied the whole project and run javac MyProject.java from shell and it returned the following errors:
RecordImportBatch.java:2: error: package org.apache.commons.io does not exist
import org.apache.commons.io.FileUtils;
...
RecordImportBatch.java:3: error: package org.neo4j.graphdb does not exist
import org.neo4j.graphdb.RelationshipType;
which I guess are caused because I didn't include jar files in compile command.
There are many jar files included in this project and as a Java newbie so far I haven't found the way to compile the project which works in Eclipse from Shell.
Does anyone know if there is a way to get the appropriate compile command directly from Eclipse and just paste it to Shell or do I have to include all jars 'manually'? If this is the case, does anyone know how to include all jars, placed in lib directory which is located in the same folder as MyProject.java?
Thank you!
If you are just learning about java, this suggestion may be some challenge, but it would be good for you to use maven to build your project, which requires reorganizing your source files and directories. And then use the assembly plugin to create a zip that includes all dependencies. Then to run your program, you just do something like:
unzip myapp.zip
cd myapp
java -cp "lib/*" com.blah.MyApp
(you might need to adjust the syntax of the /* part, using single quotes, or removing quotes depending on your shell)
Here is a snippet for the assembly plugin (general purpose... nothing hardcoded other than version, and the path which follows conventions). This goes in pom.xml:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2</version>
<configuration>
<descriptors>
<descriptor>src/main/assembly/distribution.xml</descriptor>
</descriptors>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<!-- this is used for inheritance merges -->
<phase>package</phase>
<!-- append to the packaging phase. -->
<goals>
<goal>single</goal>
<!-- goals == mojos -->
</goals>
</execution>
</executions>
</plugin>
And here is an example assembly file (this goes in src/main/assembly/distribution.xml relative to pom.xml):
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd"
>
<id>${artifact.version}</id>
<formats>
<format>zip</format>
</formats>
<files>
<file>
<!-- an example script instead of using "java -cp ..." each time -->
<source>${project.basedir}/src/main/bin/run.sh</source>
<outputDirectory>.</outputDirectory>
<destName>run.sh</destName>
<fileMode>0754</fileMode>
</file>
</files>
<fileSets>
<fileSet>
<directory>${project.basedir}/src/main/resources/</directory>
<outputDirectory>/res/</outputDirectory>
<includes>
<!-- just examples... -->
<include>*.sql</include>
<include>*.properties</include>
</includes>
</fileSet>
<fileSet>
<directory>config/</directory>
<outputDirectory>/config/</outputDirectory>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<outputDirectory>/lib</outputDirectory>
<excludes>
<!-- add redundant/useless files here -->
</excludes>
</dependencySet>
</dependencySets>
</assembly>
Also, eclipse has a "jar packager" utility in the gui, but I found it to be not very good when I used it a few years ago. And I don't think it handles dependencies, so you would need to take my "-cp" argument above, and add all the jars, or put them in your lib directory yourself.
Also there is this http://fjep.sourceforge.net/ but I have never used it.... I just found it now while quickly looking up the eclipse jar packager. In his tutorial, his last line (showing running it) looks like:
> java -jar demorun_fat.jar
Hello
If what you need to do, is to compile and run your program in Eclipse on your pc and transfer the compiled result to the Linux machine, then use the File -> Export -> Java -> Runnable Jar file and choose the packaging most suitable for you.
The technologically most simple is to use "Copy required libraries into a sub-folder next to the jar" but then you need to distribute by zipping the files together, and unzip them on the Linux box.
I would strongly recommend using any kind of build tools, the de facto standards are Ant or Maven, but you can find several alternatives. Both of them are quite trivial to set up for a smaller project, and using them is also a piece of cake (note that Eclipse can also generate you a basic Ant build.xml file).
For instance, it could be one command to run your whole project:
> ant run
Buildfile: build.xml
clean:
compile:
[mkdir] Created dir: ...
[javac] Compiling N source file to ...
run:
[java] Running application...
main:
BUILD SUCCESSFUL