Jhon Hernández
Jhon Hernández

Reputation: 313

Jar files doesn't do anything and bat file can find the main class

I have a java program I've just followed a tutorial and the .jar was created with Maven in Apache Netbeans 11. But the .jar doesn't execute... nothing happens. I even have a .bat file to run it but it says:Windows can't find file NewMain which is the main class.

This is the maven I've used:

<?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.mycompany</groupId>
    <artifactId>mavenproject3</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <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>com.apple</groupId> <artifactId>AppleJavaExtensions</artifactId> <version>1.4</version> </dependency>
    </dependencies>
       <build>
        <finalName>mavenproject3</finalName>
        <plugins>        
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-eclipse-plugin</artifactId>
                <version>2.9</version>
                <configuration>
                    <downloadSources>true</downloadSources>
                    <downloadJavadocs>false</downloadJavadocs>
                </configuration>
            </plugin>           
            <plugin>
                <groupId>com.mycompany</groupId>
                <artifactId>mavenproject3</artifactId>
                <version>1.0-SNAPSHOT</version>
                <configuration>
//Versión de JDK con la cual se ha construido el proyecto
// 1.8 significa que se utilizó Java8
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>    
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-eclipse-plugin</artifactId>
                <version>2.9</version>
                <configuration>
                    <downloadSources>true</downloadSources>
                    <downloadJavadocs>false</downloadJavadocs>
                </configuration>
            </plugin>    
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.4.1</version>
                <configuration>            
                    <descriptorRefs>
//Sufijo que se le agregara al fichero JAR ejecutable
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>           
                    <archive>
                        <manifest>
//Aqui se establece el nombre de la clase principal
                            <mainClass>com.MainClass</mainClass>
                        </manifest>
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase> 
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

Is there something missing or something misplaced?

Manifest.mf

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Built-By: Usuario
Created-By: Apache Maven 3.3.9
Build-Jdk: 1.8.0_231
Main-Class: MainClass

MainClass is not the main class

Update:
I've made the change

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Built-By: Usuario
Created-By: Apache Maven 3.3.9
Build-Jdk: 1.8.0_231
Main-Class: NewMain

Now: NewMain parent class not found or loaded

Upvotes: 0

Views: 480

Answers (3)

Jhon Hern&#225;ndez
Jhon Hern&#225;ndez

Reputation: 313

The name of the package has to come first:

<mainClass>com.mycompany.mavenproject3.NewMain</mainClass>

Now It works

Upvotes: 0

Rahul Chauhan
Rahul Chauhan

Reputation: 26

Jar file should have Main-Class: name of the class containing the main (with package name) parameter defined in Manifest.mf.

Upvotes: 1

PythonLearner
PythonLearner

Reputation: 1466

You have to diagnose in the following manner.

  1. First run the .jar file with the command java -jar <jar file name>.
  2. If it runs correctly, then the jar file is good.
  3. If the jar file does not run, unzip the .jar file using 7zip and check whether main class has been defined in Manifest.mf file.

Again .bat file is a helping tool to run the jar file, it will have the same command as mentioned in point 1.

Upvotes: 1

Related Questions