0

After building the Java program in NetBeans, I compressed the dist folder, placed the program in a USB. In another computer, after extracting all the files, I tried running the JAR file but a Window prompt said:

"Could not find the main class: logic.Main. Program will exit."

After researching and tried the solutions of similar problems (i.e. creating Manifest file, creating .bat file) but nothing works.

Then I ran it in command prompt and these were the results: enter image description here

Are there 2 problems: could not find main class and that in the other computer, the Java is not updated? How to solve this?

Cœur
  • 37,241
  • 25
  • 195
  • 267
beyonchayyy
  • 93
  • 2
  • 12

2 Answers2

1

It was actually able to find a logic.Main, but it wasnt able to load it because it was compiled with Java 8 and the user's machine is running an earlier version of Java. Compiling the file on an earlier version of Java or updating Java on the target machine will fix the issue.

Kiskae
  • 24,655
  • 2
  • 77
  • 74
  • err would you know how to compile the program in an older version of Java in Netbeans or how to set the Java version to 6 in Netbeans? – beyonchayyy May 23 '16 at 01:49
  • 1
    @beyonchayyy http://stackoverflow.com/questions/9290848/how-to-set-a-java-compiler-in-netbeans Seems to have the answer for that question. – Kiskae May 23 '16 at 01:53
0

There are multiple ways of creating executable jar.

In netbeans there is a option

Project Properties -> Build -> Packaging -> Build JAR after compiling

Maven Build can also be used for creating executable jar. Define main class in below maven plugin. Also you can select the compiler version to avoid major minor issue.

   <plugin>
    <!-- Build an executable JAR -->
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.6</version>
    <configuration>
      <archive>
        <manifest>
          <addClasspath>true</addClasspath>
          <classpathPrefix>lib/</classpathPrefix>
          <mainClass>com.kulhade.elasticsearch.Indexer</mainClass>
        </manifest>
      </archive>
    </configuration>
  </plugin>
  <plugin>
    <groupId>org.dstovall</groupId>
    <artifactId>onejar-maven-plugin</artifactId>
    <version>1.4.4</version>
    <executions>
      <execution>
        <goals>
          <goal>one-jar</goal>
        </goals>
      </execution>
    </executions>
  </plugin>
Vijendra Kumar Kulhade
  • 2,217
  • 3
  • 15
  • 25