1

I wrote a program in NetBeans, and now want to share it with my coworker. However, when he tries running it on his computer, he gets an error message:

"Could not find the main class: excelcomparator.ExcelComparator. Program will exit."

Here's the confusing part: I wrote this on my laptop which has NetBeans, and to make sure that it worked, copied the dist folder onto a flash drive, and ran it on my computer. It worked fine. When I emailed it to my coworker, he got that error.

Based on what I've seen, some solutions are to run the .jar from the command line. While that might work, I need the file to be double-clickable.

I sent my coworker the file via email in a zipped folder, is it possible that unzipping the entire folder also messed up the .jar file (don't see why it should, but included anything that might help)?

This is the MANIFEST.mf that's within the .jar file. There is a carriage return at the end of the file, it just doesn't copy well into this text box:

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.9.4
Created-By: 1.7.0_71-b14 (Oracle Corporation)
Class-Path: lib/poi-3.12-20150511.jar lib/poi-examples-3.12-20150511.j
 ar lib/poi-excelant-3.12-20150511.jar lib/poi-ooxml-3.12-20150511.jar
  lib/poi-ooxml-schemas-3.12-20150511.jar lib/poi-scratchpad-3.12-2015
 0511.jar
X-COMMENT: Main-Class will be added automatically by build
Main-Class: excelcomparator.ExcelComparator

I sent the dist folder, which includes the lib folder which contains all the additional .jars.

If there's any other relevant information I need to include, let me know. Thanks!

Salmononius2
  • 295
  • 5
  • 14
  • Your program depends on a lot of jar files supposed to be located in a lib folder. Did you also sent them to your coworker? What's the complete stack trace of the exception? Use java -jar yourjar.jar from the command line. – JB Nizet Jun 18 '15 at 19:18
  • @JBNizet There is no exception thrown on my machine, and my coworker isn't running it from command line. The only error that comes up is the popup window with the error message. I did include the additional jar files, and have edited it into the question accordingly. Thanks! – Salmononius2 Jun 18 '15 at 19:26
  • 1. Your coworker should have the lib folder in the same directory with the jar file he's trying to run. Does he have it? If he only has the dist folder, it will not pick up the jar files 2. Is your main class in a package? – melis Jun 18 '15 at 19:30
  • Ask him to run it from the command line. Also, post the result of `jar tvf yourjar.jar`. – JB Nizet Jun 18 '15 at 19:30

2 Answers2

0

If you want the JAR to be double-clickable then you should bundle all the dependencies into a single JAR file.

You achieve this using the maven assembly plugin: http://maven.apache.org/plugins/maven-assembly-plugin/usage.html

If you don't know how to use Maven, Eclipse has a nice wizard. I'm not sure how it works on NetBeans but the same question has been asked many times, e.g. here Netbeans Export to Jar, include all library files

Community
  • 1
  • 1
Franz Becker
  • 705
  • 5
  • 8
  • One more useful link might be: http://arunasujith.blogspot.de/2011/08/how-to-build-fat-jar-using-netbeans.html – Franz Becker Jun 18 '15 at 19:46
  • I use eclipse, (File Export), never had an issue with it. However, it may depend on the computer's security settings whether or not a jar can be double-clicked. Sometimes you have to right click and run as administrator. – Perry Monschau Jun 18 '15 at 20:03
  • The standard jar format doesn't allow embedding jar files inside jar files. This would require a special class loader to be used. Bundling the libraries outside of the jar file is the standard way of doing. – JB Nizet Jun 18 '15 at 20:10
  • Don't know how I missed that question, I was searching for hours... Figured instead of just trying to emulate an Eclipse function in NetBeans, might as well just do it in Eclipse. Hopefully this'll work. – Salmononius2 Jun 18 '15 at 20:32
  • @JBNizet if he is distributing his program using a flash drive I think it's fair for him to use the "non-standard" way and use the customized class loader as provided by the Eclipse wizard. – Franz Becker Jun 18 '15 at 20:57
0

Just to finish this off, I'll write what ended up working for me:

As per Franz Becker's suggestion based on this question, I bundled the entire program into a single .jar file.

I was still getting the same error on my coworkers computer, so based on other suggestions I saw, I guessed that the problem was based on different versions of Java installed on our computers. What I did was recompile the program to an earlier version of Java, which works with any version from then and onward (other people suggested reinstalling Java on the users computer. That's a bit too unrealistic in most scenarios).

To change the compliance level (in Eclipse): right click on the project in Package Explorer, select properties. Choose Java Compiler in the options on the left. Uncheck 'Use compliance from execution environment [default target JavaSE] on the 'Java Build Path'', and change 'Compiler compliance level' to your level of choice (I picked the lowest option available, 1.3, as I assumed anything higher than that wouldn't be an issue).

I then exported the file (File -> Properties -> Java -> Runnable JAR File), and had a working, clickable .jar.

Thanks to everyone for their help!

Community
  • 1
  • 1
Salmononius2
  • 295
  • 5
  • 14