Zharin
Zharin

Reputation: 75

Build Project with Maven

I have build my project which i created in Netbeans with Maven. I would like to open the built .jar - file but nothing happens. I think that the problem is, that I have to specifiy the Main Class in the pom.xml file?

<?xml version="1.0" encoding="UTF-8"?>
<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>com.myproject</groupId>
<artifactId>My_Project_name</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<build>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
    <dependency>
      <groupId>org.apache.pdfbox</groupId>
      <artifactId>pdfbox</artifactId>
      <version>2.0.4</version>
    </dependency>
    <dependency>
        <groupId>org.apache.derby</groupId>
        <artifactId>derby</artifactId>
        <version>10.10.1.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.derby</groupId>
        <artifactId>derbyclient</artifactId>
        <version>10.10.1.1</version>
    </dependency>
    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20171018</version>
    </dependency>
    <dependency>
        <groupId>com.googlecode.json-simple</groupId>
        <artifactId>json-simple</artifactId>
        <version>1.1</version>
    </dependency>
</dependencies>
</project>

This is also what my main class looks like.

package com.myproject.My_Project_name;
import java.awt.Rectangle;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;


public class App {
     public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new OverviewScreen().setVisible(true);
            }
        });
    }
}

Please help!

Upvotes: 1

Views: 1312

Answers (2)

Zharin
Zharin

Reputation: 75

I have now fixed the problem. I added the following code to my pom.xml file and now it works fine.

 <build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-jar-plugin</artifactId>
      <version>3.0.2</version>
      <configuration>
        <archive>
          <manifest>
            <mainClass>Path.To.My.Mainfile</mainClass>
          </manifest>
        </archive>
      </configuration>
    </plugin>
  </plugins>
</build>

Upvotes: 0

Jamin
Jamin

Reputation: 1402

You need to ensure that your dependencies are up to date and that you have everything you need to be working on your project. The best way to troubleshoot this problem is to start a new maven project and start adding each dependency one by one until you find which dependency is the problem.

Here is the example pom.xml that should be working...For today.

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>com.myproject</groupId>
  <artifactId>My_Project_name</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>My_Project_name</name>
  <url>http://maven.apache.org</url>

  <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>org.apache.derby</groupId>
        <artifactId>derbyclient</artifactId>
        <version>10.10.1.1</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.json/json -->
        <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20171018</version>
    </dependency>

    <dependency>
        <groupId>org.apache.pdfbox</groupId>
        <artifactId>pdfbox</artifactId>
        <version>2.0.4</version>
    </dependency>
    <dependency>
        <groupId>com.googlecode.json-simple</groupId>
        <artifactId>json-simple</artifactId>
        <version>1.1</version>
    </dependency>




  </dependencies>
</project>

And so the main class will look like this:

package com.myproject.My_Project_name;
//import java.awt.Rectangle;
//import java.io.BufferedReader;
//import java.io.File;
//import java.io.FileInputStream;
//import java.io.FileOutputStream;
//import java.io.IOException;
//import java.io.InputStreamReader;
//import java.io.RandomAccessFile;


public class App {
     public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                //error in commented line below
                //new OverviewScreen().setVisible(true);
                System.out.println("hello");
            }
        });
    }
}

So it looks like you are encountering your issue when you are using the following code:

    new OverviewScreen().setVisible(true);

Then before exporting the project to a .jar, make sure you select the main class of the application entry point. In my case the wizard for exporting to .jar has it on the final page of the wizard and you must click next until you see it. For this example, select App.java (not AppTest.java) as your main.

Specifying the main entry point for the program can also be done other ways. If you want to ensure it's working, check to see if the program is manifested within command prompt:

C:\Users\Me\Documents>java -jar myProjectName.jar

Once the project has been exported as a .jar, attempt to execute it on the command prompt.

C:\Users\Me\Documents>java -cp myProjectName.jar com.myproject.My_Project_name.App

If your dependencies are working, and you exported the .jar with the main file selected properly you should result with the command line outputting:

hello

from running the .jar. Also make sure you are entering the command when you are in the containing folder of the .jar file.

Upvotes: 1

Related Questions