5

I am running into problems when I try to run the jar file created in IntelliJ.
I followed the steps laid out here: How to build jars from IntelliJ properly? and searched far and wide for other people with the same problem using IntelliJ, and found no solution.
In the menu Project Structure>Artifacts section I have the package and main class name, and I have also tried specifying the 'Class path' bit using a relative and absolute path both to the source file and the .class file (which seems hidden in the IntelliJ Project display tree..), but even with all this (I also make sure to rebuild the jar every time I change something) I get the "Cannot find the main class. Program will exit." message when I try to run the jar.

In the main project folder, there is src and out.
out contains 'production' with the package tree and the class files
out also contains 'artifacts' with the jar folder and jar executable
src contains a folder of images, the META-INF and the package tree.
the first folder in the package contains the main class, so src/ravelDemo/RavelDemoMain.java

in Project Settings>Artifacts, the main class is listed as ravelDemo.RavelDemoMain
currently, the class path is out\production\RavelSequence v1.03\ravelDemo\ but this and every other class path I've specified (or left blank) doesn't seem to point to the right place. (RavelSequence v1.03 is the name of the project).

What am I doing wrong?

Community
  • 1
  • 1
sideways8
  • 153
  • 1
  • 1
  • 14
  • Check these screencasts: http://www.screenr.com/Eyg8, http://www.screenr.com/FWM8. If you can reproduce the problem with similar steps, attach a sample project to the question. – CrazyCoder Jun 23 '12 at 17:22
  • Thanks for the response! That is a really handy website. Here's what I've got: http://www.screenr.com/EMl8 – sideways8 Jun 24 '12 at 06:06
  • And here is a sample project built from scratch with the same problem, modelled after your sample: http://www.screenr.com/6Ml8 – sideways8 Jun 24 '12 at 06:32
  • Can you zip and share the project? What is your system default JDK? Can you try to run it from the command line using `java.exe` from the same directory as project JDK? `java.exe -jar Sample.jar` – CrazyCoder Jun 24 '12 at 10:03
  • I have jdk 1.7.0_03. I can run them from command line using those commands. I get a bunch of errors for fileIO and imageIO, but perhaps I didn't properly include them in the jar. Here is the project: https://dl.dropbox.com/u/4843072/RavelSequence%20v1.03.zip I would like to have a 'double-click' executable rather than a command line executable so I can share the project easily. Is that possible? – sideways8 Jun 24 '12 at 10:48

2 Answers2

3

Basing on the discussion is the comments above it appears that IntelliJ IDEA is building the valid executable jar with the main class correctly specified in the Manifest, this jar works fine when executed with java -jar ... command from the command line using the project target JDK version on the user's machine.

The problem is that it doesn't work on double click on @sideways8 system. It may be caused by the corrupted default Java installation or wrong .jar file type association in Windows registry. This is machine specific issue as I have no problem running the shared project jar on my system by double clicking on it.

To fix this problem you can try to uninstall all the Java versions present on this machine from the Control Panel | Programs and Features, then install new JDK from scratch so that it's the only JDK on the system and .jar file type is handled by it.

I use Total Commander which has a nice feature to check/edit file associations (File | Associate With...), here is how the .jar association is displayed when I press Edit type...:

jar file type

CrazyCoder
  • 389,263
  • 172
  • 990
  • 904
  • Ok, I am downloading a fresh jdk. If I try to share this jar with other people, are they likely to run into the same problem, in your experience? Is there a way to package the appropriate jdk with the jar so that there are no worries about a system's native environment? – sideways8 Jun 24 '12 at 11:25
  • It's very uncommon issue, most users with Java installed will be able to run your application. Note that you may want to target JDK 1.6 version as it's probably still the default on many machines. If you build with JDK 1.7, users with JDK 1.6 installed will not be able to run your application. You can fix it by changing the target JDK to 1.6 or by adding `-source 1.6 -target 1.6` options in `Settings` | `Compiler` | `Java Compiler`, **Additional command line parameters**. – CrazyCoder Jun 24 '12 at 11:29
  • If you want to be 100% sure that your app runs on the target machine, bundle JRE with it or wrap it into the installer that will verify that machine has the working JDK/JRE of minimum version required to run your application and downloads/installs it automatically if the system doesn't meet your requirements. – CrazyCoder Jun 24 '12 at 11:31
  • Ok, I have it working now, using jdk1.7. Still downloading the 1.6 version so I can cover those bases. Thanks! – sideways8 Jun 24 '12 at 12:11
  • When I try to run my projects on 1.6 I get a 'Exception in thread "main" java.lang.UnsupportedClassVersionError: ravelDemo/RavelDemoMain : Unsupported major.minor version 51.0' All the jetbrains support I read on the issue was addressing the use of jdk 1.4 or 1.5 and the solution was to install 1.6. I edited the Project SDK and the Module dependencies to jdk 6, when that didn't work, I also added the additional command line parameters you mentioned, but I still get the unsupported class error. – sideways8 Jun 24 '12 at 17:56
  • 1
    Rebuild the project and the artifact. – CrazyCoder Jun 24 '12 at 19:03
  • Delete all the output classes, rebuild the project with `-target 1.6` or JDK 1.6 configured in the Project/Module Dependencies, then it should run fine. – CrazyCoder Jun 25 '12 at 01:31
  • They do now run fine. I had to modify some code to make it work though. Apparently 1.6 doesn't support generics in JComboBoxes. It all runs fine (the jars too, still) on my machine, but the images don't load when I sent it to a friend to try. They load fine on my machine. – sideways8 Jun 25 '12 at 02:28
0

I had the same problem. Either the jar file was created with IntelliJ IDEA or other IDE. I found out the problem in my case that I had two version of java installed on my computer (java 6 & java 8). It's a problem of misconfiguration of the system. so remove any version of java you have and install it again and the problem is solved.