6

I tried to create an executable jar from my project on Intellij 13 (win8), following these answers How to build jars from IntelliJ properly? and execute the jar created, then I get "Error: Invaid or corrupt jarfile" followed by the path of the jar.

My project is simple, but have multiple java files and an UI form.

I call Build|Build Artifacts|Jar after create jar artifact on Project Structure|Artifacts, after build.

I created the project with a maven scafold, but there are no dependencies on pom.xml.

I'm using java7. Is something missing?

Artifact

Community
  • 1
  • 1
Arthur Julião
  • 849
  • 1
  • 14
  • 29
  • What is the error message you are getting to indicate the the JAR is invalid or corrupt? Where are you getting the message? (i.e. when building or when you try to use it? Is it an executable jar that you are trying to run? Given you included the `executable-jar` tag, I would have to say yes.) Can you also provide a screenshot of your artifact configuration? Please edit your question with this information. Also, if you have a maven based project, you should be able to build a non-executable JAR just by running the `package` goal in the maven tool window. – Javaru Apr 14 '14 at 21:05
  • Is your project set to Java7 as well, if you're using Java7 as the version for your system? – Dropout Apr 15 '14 at 05:17
  • @markVedder I added a link to screenshot and the other informations that you asked. – Arthur Julião Apr 15 '14 at 05:25
  • @dropout I have only java7 installed on computer, can't be version conflic I think – Arthur Julião Apr 15 '14 at 05:26
  • @ArthurJulião thanks for the info. May I also ask you what does your file structure look like? Is your class wrapped at least in one package? – Dropout Apr 15 '14 at 05:35
  • @dropout 2 packeges, 3 classes in the first and 2 in the second – Arthur Julião Apr 15 '14 at 10:42
  • @ArthurJulião -- Whatever you have on twitter is useless to us here on SO. Can you post that info, and any screenshots here so that the rest of us can benefit from them too? Thanks. – Software Engineer Apr 15 '14 at 14:26
  • That doesn't look like it was created with Maven. I'd expect to see a folder called target, not artifacts, and your jar should sit under there. Also, you need a package called main, with a class called Main, that contains the method `public static void main(String[] args)`. I can't tell if you have one of these or not, but without it nothing will work. I'd recommend starting again with the basic maven setup and a simple pom and telling intellij to import the pom. – Software Engineer Apr 15 '14 at 21:23
  • @EngineerDollery well, there are a class Main, with the main method. The program works, I just can't pack it in an executable jar. – Arthur Julião Apr 15 '14 at 23:55

2 Answers2

7

Here is a quick solution WITHOUT pom.xml

I´ve solved it with the artifact settings of IntelliJ by creating a "META-INF" subfolder and copying the Manifest file inside.

I don´t know why this is even needed, but it seems that IntelliJ IDEA doesn´t include the META-INF folder by default

enter image description here

Pointing to the file:

enter image description here

Marian Klühspies
  • 15,824
  • 16
  • 93
  • 136
5

Since I couldn't create a jar with the IDEA itself, and I previously create my project with maven scaffold, I decided to use maven to create executable jar. To do it I added to the pom.xml:

<packaging>jar</packaging>
<properties>
    <jdk.version>1.7</jdk.version>
</properties>
<name>Project Name</name>

In build tag:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>${jdk.version}</source>
                <target>${jdk.version}</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>main.Main</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>

And I also had problems with the UI created by GUI-Designer: NullPointerException in the Pane created. To make it work I added a dependency:

<dependencies>
    <dependency>
        <groupId>com.intellij</groupId>
        <artifactId>forms_rt</artifactId>
        <version>5.0</version>
    </dependency>
</dependencies>

After that, I just used mvn package inside project directory on cmd.

Fonts:

Community
  • 1
  • 1
Arthur Julião
  • 849
  • 1
  • 14
  • 29