10

I’ve created simple java program (maven with pom ) which when I run some command with CMD it should created a file under given path... I do mvn clean install which finish successfully, Now I want to use this created jar from the command line like follwoing:

java -jar   "/Users/i012/IdeaProjects/myproj/target/test.rts-1.0-SNAPSHOT.jar" path2genfile2create 

Which should run my program (this the first time that I try something like this…)

But the error which Im getting is:

no main manifest attribute, in /Users/i012/IdeaProjects/myproj/target/test.rts-1.0-SNAPSHOT.jar

What could be missing here ? which manifest attribute ?

The error is not coming from the class i’ve created

i've created some META-INF/MANIFEST.MF not helping but maybe Its wrong

Jenny Hilton
  • 1,297
  • 7
  • 21
  • 40

5 Answers5

11

If you're using the Maven assembly plug-in, or your IDE tooling is, you need a mainClass element. This is what I use:

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
      <archive>
        <manifest>
          <mainClass>com.foo.MyMainClass</mainClass>
        </manifest>
      </archive>
      <descriptorRefs>
        <descriptorRef>jar-with-dependencies</descriptorRef>
      </descriptorRefs>
    </configuration>
    <executions>
      <execution>
      <id>make-assembly</id>
      <phase>package</phase>
      <goals>
        <goal>single</goal>
      </goals>
      </execution>
    </executions>
  </plugin>
Kevin Boone
  • 4,092
  • 1
  • 11
  • 15
  • How can I run it ? – Jenny Hilton Aug 27 '17 at 08:00
  • if the myprojet is build like myproj->src->main->java>myclass , what should I put in the mainClass attr ? – Jenny Hilton Aug 27 '17 at 08:06
  • @JennyHilton The FQN class name of the class containing the `main()` method you want to run, like `Main-class: my.package.MainClass`. – user207421 Aug 27 '17 at 08:11
  • My proj structure in this context is:`myproj->src->main->java>myclass` – Jenny Hilton Aug 27 '17 at 08:13
  • Done 1+ it works...when I use only myclass , 1. why ? 2. does it best practise to use maven-assembly-plugin ? thanks! – Jenny Hilton Aug 27 '17 at 08:16
  • @JennyHilton No, in your case `myclass`. Neither `src` nor `main` nor `java` is part of the package name, and neither is `/`. As far as I can see, you don't have a package name. – user207421 Aug 27 '17 at 08:26
  • The default with Maven Java builds is for the package hierarchy to begin under src/main/java. So if your Java package name were com.foo, you would need Java source files in src/main/java/com/foo/. Of course, you'd need a `package` declaration in your source as well. As for maven-assembly-plugin, I use that when I want to bundle everything into one big executable JAR file. I don't claim it's the only way, or even a good way. It's just what works for me. – Kevin Boone Aug 27 '17 at 12:29
5

in my case, I was using spring-boot but i did not mentioned my builder in my pom so i fixed it by:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
saman
  • 51
  • 1
  • 2
4

A manifest is a file in the path META-INF/MANIFEST.MF within the jar which defines attributes like the classpath and the main class for running the jar file.

The basic structure would be like:

Manifest-Version: 1.0
Created-By: 1.7.0_06 (Oracle Corporation)

You can define your entry point by adding the property Main-Class: classname.

In order to create your jar file with a given manifest you can:

  1. Use your IDE to add a manifest to the jar it generates.
  2. Use a command like jar cfm MyJar.jar Manifest.txt MyPackage/*.class to manually create a jar with the given manifest and classes.
  3. Manually decompress the jar, add the manifest, and compress it again. Compression tools generally could do this with a drag/drop.

You can find out more about the jar manifest file here.

alirabiee
  • 1,286
  • 7
  • 14
  • 1
    In your project settings -> artifacts, click add, and select the main class. Make sure the directory for the manifest is filled. Click OK. On the page check the _Build on make_ checkbox, then press OK. Now rebuild your project. – alirabiee Aug 27 '17 at 06:20
  • I dont find the project settings can you please say where – Jenny Hilton Aug 27 '17 at 06:24
  • File -> Project Structure – alirabiee Aug 27 '17 at 06:26
  • I didnt find the make checkbox but I did the steps and It create META-INF under src->main->java , Now I did mvn clean install and I got the same error, do I miss someting in addition ? – Jenny Hilton Aug 27 '17 at 06:35
  • Extract the jar you're trying to execute and see if it's properly structured. – alirabiee Aug 27 '17 at 06:38
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/152931/discussion-between-jenny-hilton-and-alirabiee). – Jenny Hilton Aug 27 '17 at 07:20
  • Can you please share what is the right structure in your answer ? – Jenny Hilton Aug 27 '17 at 07:22
  • @JennyHilton you can a file named MANIFEST.MF be located at directory resource/META-INF,of course ,if the directory no exist,you should create it. – dabaicai Aug 27 '17 at 07:32
1

You need a main class to execute you application. Try the following. It worked for me.

Add the following code snippet to your pom.xml file if you are using maven build tool.

 <packaging>jar</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>

    <build>
        <plugins>

        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>com.validator.App</mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

        </plugins>


    </build>
Du-Lacoste
  • 11,530
  • 2
  • 71
  • 51
1

I had the same problem and it was solved by adding this plugin:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>com.somepackage.CLASSWITHMAINMETHODE</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>

Youssef N
  • 85
  • 1
  • 8