Add classpath to netbeans maven project - java

I have a java project which uses .properties files for configuration. On the server, when launching, I set the classpath to include a folder which contains all the properties files. On my local machine, I'd like to point to a different folder.
I'm looking to add to the classpath, ideally for all projects, but adding it to each project is also fine. I've tried changing the Run > VM Options to include classpath, but with that change it can't find the main class, and I get java.lang.NoClassDefFoundError. I've also tried changing nbactions.xml directly to set the classpath to -classpath ~\MyFolder\;%classpath, but this has the same problem.
To add to the difficulty, the server is running linux while my local machine is running Windows.

I stuck with topic-starter issue also for a long time. My goal - put config files for debug purpose in project root and extend classpath to ${basedir}, so this code:
String appConfigLocation = System.getProperty("config.location");
if (appConfigLocation == null) {
logger.error("System property 'config.location' is not set...");
System.exit(1);
}
InputStream appConfigStream = Main.class.getClassLoader().getResourceAsStream(appConfigLocation);
if (appConfigStream == null) {
logger.error("Can't find resource {} in classpath, fix 'config.location'...", appConfigLocation);
System.exit(1);
}
Properties appProps = new Properties();
try {
appProps.load(appConfigStream);
} catch (IOException ex) {
System.out.println("IO error during loading of {}...", appConfigLocation);
System.exit(1);
}
read configs from ${basedir}. I like that instead of putting them to src/main/resources.
Check sources of ExecMojo.java for v1.2.1 http://grepcode.com/file/repo1.maven.org/maven2/org.codehaus.mojo/exec-maven-plugin/1.2.1/org/codehaus/mojo/exec/ExecMojo.java?av=f :
if ( CLASSPATH_TOKEN.equals( args[i] ) ) {
commandArguments.add( computeClasspathString( null ) );
}
and and v1.3.2 http://grepcode.com/file/repo1.maven.org/maven2/org.codehaus.mojo/exec-maven-plugin/1.3.2/org/codehaus/mojo/exec/ExecMojo.java?av=f:
if ( args[i].contains( CLASSPATH_TOKEN ) ) {
commandArguments.add( args[i].replace( CLASSPATH_TOKEN,
computeClasspathString( null ) ) );
}
So update NB config Execute goals to new version:
process-classes org.codehaus.mojo:exec-maven-plugin:1.3.2:exec
and use complex -classpath args in exec.argsparams:
exec.args=-classpath %classpath:.:"${basedir}" \
-Dconfig.location=app.properties \
-Dlogback.configurationFile=logback.xml \
${packageClassName}
Fix any action you need to have such behavior:
See also:
http://wiki.netbeans.org/FaqEnvVarsDuringRun
http://wiki.netbeans.org/FaqSysPropsDuringRun

Hi I had a similar need to give NetBeans7.4 a classpath to a jar with a driver outwith Maven dependencies e.g. c:\Program Files\Java\jdk1.7.0_25\db\lib\derby.jar in a Java Maven project called MyProject.
As you were thinking with the Run > VM Options, I would suggest the following:
1) right-click on MyProject to open project Properties
2) in 'Project Properties' pop-up, select 'Actions'
3) locate 'Run pproject' among the 'Actions' and select it
4) edit in 'Set Properties' text box entering
exec.args=-cp %classpath;.;"c:\Program Files\Java\jdk1.7.0_25\db\lib\derby.jar" biz.letsweb.derbyconnect.App
exec.executable=java
exec.workingdir=c:\Users\Tomasz\Documents\NetBeansProjects\DerbyConnect\target\classes
Alternatively edit nbactions.xml analogically. Once I did this, I could simply run the project inside NetBeans by pressing green arrow.

what about having the properties included as ? and those you use locally only have in a 's and activate that profile on local machine?

This is how I've been adding classpath to many of my projects
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.nitinsurana.policereports.GUI</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>

An outside the box solution I ended up using for my similar case in NetBeans 7.3.1:
Adding files to java classpath at runtime
private static void addSoftwareLibrary(File file) throws Exception {
Method method = URLClassLoader.class.getDeclaredMethod("addURL", new Class[]{URL.class});
method.setAccessible(true);
method.invoke(ClassLoader.getSystemClassLoader(), new Object[]{file.toURI().toURL()});
}
This gives me a hacky way to add files to my classpath at run time via program arguments. Related notes I compiled while researching:
To include a dependency for compilation only, not runtime set:
Dependency Scope
<dependency><scope>provided</scope>...</dependency>
To exclude a dependency from the shaded jar, set: Exclude
<exclude>groupId:artifactId[[:type]:classifier]</exclude>
To copy resources to the target directory from outside the typical source directory: Copy Resources
<project>
...
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<executions>
<execution>
<id>copy-resources</id>
<!-- here the phase you need -->
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>target/extra-resources</outputDirectory>
<resources>
<resource>
<directory>extra-resources</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
...
</build>
...
</project>
Note that the base path for directory is the project home. The linked post has <filtering>true</filtering> which can cause "invalid mark" in Netbeans 7.3.1.

Related

java -jar command is not working, no main manifest attribute [duplicate]

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

What happens when I run java -jar from command line [duplicate]

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

How can I run a jar application in the background? [duplicate]

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

Can't execute jar- file: "no main manifest attribute"

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

Injecting Maven project information into Swing Application Framework resources?

I have a Maven project using the Swing Application Framework and would like to inject project information from the pom.xml into my application's global resources to avoid duplication.
The base application (provided via netbeans) uses Application.title, Application.version, Application.vendor, Application.description resources etc for Window titles and about box configuration but I can't find a way to set these values programatically at run time and I'm not a maven maven so don't have the skills to inject them at build time.
Anyone have any recommendations on how best to achieve the desired result?
You could try using filtered resources. If you create a property file, say src/main/resources/com/myapp/app.properties that looks like this:
version=${project.version}
name=${project.name}
id=${project.artifactId}
Them you need to enable filtering in your pom.xml:
<build>
<resources>
<resource>src/main/resources</resource>
<filtering>true</filtering>
</resources>
</build>
Now when maven builds your project, it'll expand the property file, and place it on the classpath. Then you can just call getResourceAsStream("/com/myapp/app.properties") to read it into your app.
Whist maven does automatically create a file /META-INF/maven/$groupId/$artifactId/pom.properties, this may not have all the information you need.
You can keep those in separte property file and read it from both pom.xml and your application.
Another option is to read pom.xml file from classpath (mvn will put it in META-INF folder) and parse it from there as plain xml file.
I would go with first option.
I would try using the maven-antrun-plugin. Pass the necessary maven properties to ant and create an ant build script which modifies an application properties file or the spring context configuration directly.
Another way would be to generate a separate properties file with the properties-maven-plugin and then add this properties file to the application bundle names:
For the pom.xml to write application.properties file:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-2</version>
<executions>
<execution>
<phase>process-resources</phase>
<goals>
<goal>write-project-properties</goal>
</goals>
</execution>
</executions>
<configuration>
<outputFile>${project.build.outputDirectory}/application.properties</outputFile>
</configuration>
</plugin>
...
</plugins>
...
</build>
For including the application.properties into your application:
public class MyApplication extends SingleFrameApplication
public MyApplication() {
super();
addGeneratedApplicationProperties();
}
private void addGeneratedApplicationProperties() {
ResourceManager resourceManager = getContext().getResourceManager();
getContext().setApplicationClass(MyApplication.class);
List<String> bundleNames = new LinkedList<String>(resourceManager.getApplicationBundleNames());
bundleNames.add(0, "application");
resourceManager.setApplicationBundleNames(bundleNames);
}
...
}
However, I find the maven-filter-solution way more elegant.

Categories

Resources