Main class not found, IntelliJ building a jar - java

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?

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...:

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.

Related

I created an installer via install4j for my app using a jar generated from maven. Why can't it find the main class when executing the .exe?

Here's how I created a jar file using maven.
​
Now for my JavaFX Application, I'm using afterburner FX Framework. Now I need to create an installer for this app to be deployed to other devices. I'm using Install4j. My steps:
mvn clean package
copy and paste the generated jar file into a different directory
add that directory to install4j Files
on Launcher under Java invocation, I select the jar file, and then I select my main class: `BOOT-INF.classes.inc.pabacus.TaskMetrics.TaskMetricsApplication`
I Build the installer and run it, install to Program files, and then open the exe file
But then an error dialog shows up:
java.lang.NoClassDefFoundError: BOOT-INF/classes/inc/pabacus/TaskMetrics/TaskMetricsApplication (wrong name: inc/pabacus/TaskMetrics/TaskMetricsApplication)
So what I did wrong was two things:
Initially, like way way back, I tried using JavaFX with Spring Framework - unsuccessfully. I'm no longer using Spring, but I still had some leftover Spring in my pom file, which caused it to put the files in a BOOT-INF directory when i package it to jar. I just simply had to remove the Spring leftovers, and the boot-inf directory was gone.
So in install4j, you select a directory that would contain the files you would add to your installer. In the tutorials, they had a separate lib directory which contained external libraries. So I thought that's all I needed. I copied my dependencies into a lib folder via maven, then i put them into a directory along with my jar. So that's all my directory had - the jar file and the lib folder. That doesn't work. I didn't know. Apparently, it needs all the files inside the target folder generated by maven. I should've just used the target folder itself.
So there you have it. I have now successfully created an installer. I do hope no one walks as silly as me, but if you had also encountered the same mess up, well... here ya go.
You seem to have configured
BOOT-INF.classes.inc.pabacus.TaskMetrics
as the main class when the correct package name is
inc.pabacus.TaskMetrics.TaskMetricsApplication
Alternatively, your VM parameters configuration for the launcher is incorrect and includes text that can be interpreted main class.

JAR files created by IntelliJ IDEA do not run

I wrote a program that spans a few classes in IntelliJ and it works fine when I was testing it in the IDE. However, whenever I follow tutorials to make my project into a .jar executable, it does not run.
The file in the out folder does not run when double-click on it and says "The Java JAR file "projectName.jar" could not be launched.
When opening it from the terminal, I get the error "Could not find or load main class". I used "$ java -jar projectName.jar "
I followed these steps:
Open Project Structure, Artifacts and click the (+).
Under JAR, from modules with dependencies.
Point to the class with my main method and click OK.
Click apply and OK to exit.
Then I close project structure and open Build and then Build Artifacts...
Then I click Build.
Navigate to ~/IdeaProjects/projectName/out/artifacts/projectName_jar/projectName.jar
Then I would try to run it but it does not run. My project has both a GUI and can be run from command lines. When I compiled the main method in Terminal, it worked fine, but I need a JAR file. I'm not sure what I should include in the question, since I'm sure that my code is unrelated. I also utilize Maven, not sure if that is related. Are there other things that I should be doing or adding to my project before I build the JAR?
This can be a result of the Manifest being created in the wrong place.
First make sure it is in src/main/resources/META-INF/MANIFEST.MF
(Intellij sometimes incorrectly places it in /src/main/java/META-INF/MANIFEST.MF)
Then Make sure the jar is looking for META-INF in the src dir NOT in /java OR /resources. This means:
When you set up File>Project Structure>Artifacts>+>JAR>From modules with dependencies... then your Directory for META-INF/MANIFEST.MF should end at src. Example:
ONLY C:\Users\User\StudioProjects\Project\src\
NOT THE PREPOPULATED C:\Users\User\StudioProjects\Project\src\main\java
If your Project Structure jar is already set up change the Manifest File path to:
C:\Users\User\StudioProjects\Project\src\META-INF\MANIFEST.MF
INSTEAD OF C:\Users\User\StudioProjects\Project\src\main\java\META-INF\MANIFEST.MF
You need to make sure that the Main-Class attribute is set. One way to check this is by opening the Jar with a Zip utility and looking for META-MF/MANIFEST.MF extract or open that file with a notepad, notepad++ or any document reader. If "Main-Class" is not in their or set to the correct main class you will received the error you have received. Seeing the IntelliJ tutorial it has a Main-Class field when setting up artifact publishing. I have included the link below to the IntelliJ page.
IntelliJ Creating an Artifact Tutorial

Eclipse Debugger throwing Source Not Found Error

I'm using Eclipse to debug a Java program I'm developing. Upon stepping into an insert method of one of my classes called from another class I get a source not found error. I have no idea why because when I hit F3 to go to the class' definition it does so without a problem. I'm using Eclipse Kepler
The class in question is found in a jar file on your classpath and jar files do not correspond to editable source. (but it may have a read-only source zip file attached).
This is most likely because the jar file is placed before the project containing the class on the launchers build path.
Fix your build path.

Eclipse giving errors whenever I try to export to executable jar

I'm trying to export a small program that I have made in Eclipse Indigo today to an executable, however, every time I do so one of two problems occur. The program uses one resource which I was hoping to put inside of the JAR but Eclipse will not put in the executable jar no matter which option I tick when I export or which folder the resource is in - the first problem!
The second problem is that whenever I tell eclipse to "Extract required libraries into generated JAR" I receive the following error when I double click on the executable Jar:
Could not find the main class: main.Launcher. Program will exit.
I don't suppose that the second problem is too much of an issue at the minute but the first one is extremely frustrating so I would appreciate any help or advice. Thanks in advance.
(Strangely, and even more frustrating, if I go through the same process with a project I made a while ago with a previous version of Eclipse it works perfectly.)
The folder structure of the project is as follows:
In the project folder there are the following directories .settings, bin, src as default. I have put the resource, which is a png in the bin folder but I have also tried it in the src folder.
Can you give some details?
What is the structure of your eclipse project?
Of what type is the resource?
More important where in the project structure resides the resource file?
Is the folder of that resource file included as a source folder ? (seems to be a prerequiste to get exported to the jar)
Is it copied over to the bin folder when the project is built?
Many Thanks
Michael
Update:
I have built a very simple java project 1 Java class with a main method and put a sample png file in the source folder. I refreshed the project to have the png visible in the project, then I cleaned the project to have it build again, then I ran the Main class inside eclipse. This will give you a run configuration when exporting throught the wizard. Thereafter I exported the complete project as "runnable jar", selected the run configuration and selected "Copy required libraries in a sub-folder next to the generated JAR". The png was included and the Main class was found. The executable JAR could be executed.
(Environment: Eclipse Indigo R1, Ubuntu, JDK 6)
You might want to play through the complete sequence of steps I went through. Maybe eclipse just did not pick up a file or other updates.
regards, Mike
First of all, I would like to thank Mike (marksml) for being so helpful and attempting to provide a solution for my problem. Unfortunately, his answer did not work for me!
I began to look at all of my previous projects and noticed that the one I was having trouble with was the odd one out (because it was the only one that didn't work) and the factor that made it the odd one out, I found to be the JRE system library version. The project was using the JavaSE-1.7 library but when I changed it to the JavaSE-1.6 like my other projects were using it miraculously worked and exported flawlessly! I'm still curious as to why this is the case, but at least I have it working now...
With thanks and kind regards, Andy

Distributable JAR not working

I created a project in Netbeans 7.1 on my netbook which uses Windows XP. I used the Clean and Build command which created the distributable JAR file in the /dist folder. When I double clicked the file it worked.
Now I sent the entire project folder to my desktop, which uses Windows 7 Ultimate 64 bit. When I double clicked the executable JAR I got an error saying:
Could Not find or load main class myapp.jar
I checked the manifest file and it contains the main class name. Of course, it was autogenerated by Netbeans.
What is going on?
The common mistake is to forget to also take the "layout" folder. If you have used specific features of Netbeans, such as bindings, in the "dist" you have the jar but also a folder with the name "layout" in it and you must also copy it with the jar file and put it at the same target place.
In my case it was a pity having to also send this folder so I decided to stop using binding and other specific stuff and used to reprogram things by hand. Now I only send the jar and it is working.
Note that you can easily know if you have specific features.
Open Netbeans and look in your project folder. There you'll have 2 folder, "Source Packages" and "libraries". Check in libraries. If you have only the JDK it is ok. If you have other libraries listed, then there will be the supplementary folder in the dist and you will have to send it with the Jar.
Hope it helps

Categories

Resources