Carlos Goce
Carlos Goce

Reputation: 1665

Problems compiling my first java Application

I'm newbie with Java and I don't know if this is because I'm doing something wrong.

My app is not too complicated. It has 5 packages. One of them, the carlosgoce package has the App class with the main method. From there, I instantiate my Principal class which extends JFrame on package gui.Principal.

So, my App class is something like this:

package carlosgoce;

import javax.swing.JFrame;
import gui.Principal;

public class App {
    public static void main(String[] args) {    
        Principal ventana = new Principal();
        ventana.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        
        ventana.setLocationRelativeTo(null);

        ventana.setVisible(true);                        
    }        
}

Ventana means Window by the way.

So Principal is just a JFrame.

My App works perfectly when i click the "play" button on Netbeans. When I try to use the generated jar it just say that it can't be opened. I check on my project properties and check if the main class was selected, and it was / it is. I read on stackoverflow to disable "Compile on save". I did, but it didn't help.

When I try to open the .jar file it just doesn't work. I tried with Clean/Build also.

Then I realized that the JFrame class autogenerated by Netbeans also had a main method so I commented it (I don't know if a project can have multiples main methods). The app is still working when launched from Netbeans but not from the jar file.

Maybe it's because of Maven? First time using it also. I found that is similar to composer on PHP. I just add OpenCSV dependency.

I just don't know what to do next. Some help?

Thank you!

More info: This is my current pom.xml file:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>carlosgoce</groupId>
    <artifactId>distribuir.articulos</artifactId>
    <version>1.02-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>distribuir.articulos</name>
    <url>http://maven.apache.org</url>
    <build>
        <plugins>            
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <archive>
                      <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>carlosgoce.principal.App</mainClass>
                      </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>attached</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>net.sf.opencsv</groupId>
            <artifactId>opencsv</artifactId>
            <version>2.0</version>
        </dependency>           
    </dependencies>
</project>

If i compile it with clean/build from Netbeans i got this manifest file:

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: acliente7
Build-Jdk: 1.7.0_25
Main-Class: carlosgoce.principal.App
Class-Path: opencsv-2.0.jar

Looks great but i can't find the opencsv-2.0.jar inside the .jar file and the app it doesn't work.

If i compile my project with cleand and then "build with dependencies" i got this manifest file:

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: 23.25-b01 (Oracle Corporation)

So the "Class-Path: opencsv-2.0.jar" and more lines with the info of my project are missing. BUT the opencsv IS inside my .jar file.

Any tip?

FIXED compiling the app with the command mvn clean compile assembly:single

I don't know if i have to do something with the question

Upvotes: 0

Views: 232

Answers (1)

MariuszS
MariuszS

Reputation: 31587

assembly:single is not needed, just add executions configuration to your pom

 <plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.4</version>
    <configuration>
      <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>

Upvotes: 1

Related Questions