简体   繁体   中英

java - Sharing an executable .jar file

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!

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, eg here Netbeans Export to Jar, include all library files

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM