NetBeans created Jar file "cannot find main method" - java

I realize there are similar questions on StackOverflow, but none of the answers are helping me.
I have a project that I made in netbeans and after setting as my main project I did a clean build. I win.rar-ed the created .jar file and the library file in the dist folder. The jar file runs fine on MY PC. However, when I copy the .rar file on to my flashdrive and try to run the .jar file on any other computer it gives me a missing main method error.
The manifest.mf file only contains the following:
Manifest-Version: 1.0
X-COMMENT: Main-Class will be added automatically by build
This is my first attempt at packing a program as a .jar file so I'm not sure what I'm missing.

My two cents..
First idea: Fix configuration problem
Check Project configuration. Right click on your Project: Properties -> Run
Is Main Class attribute well defined?
Second one: Get a fresh and well defined build! (Ant XML file/targets and so on)
Create New Project - Category [Java] Project [Java Application]
Import existing sources to this new one
Define the Main Class as above
Good luck!

Related

How to put a java project in an exe or jar?

I want to put my java Project in a .jar or in an .exe. I can not find a solution for my problem.
That is my project structure:
project
1.1. pom.xml
1.2. .idea
1.2.1. libaries
1.2.1.1. many maven dependency files
1.3. .GUI
1.3.1. .idea
1.3.1.1. libaries
1.3.1.1.1. many maven dependency file
1.3.2. src
1.3.2.1. main
1.3.2.1.1. java
1.3.2.1.1.1. .java files
1.3.2.1.2. recources
1.3.2.1.2.1. .fxml files
1.3.2.1.2.2. CSSStylesheets
1.3.2.1.2.2. .css files
I follow instructions on youtube for creating a .jar in Intellij (https://www.youtube.com/watch?v=ZlyTn8PZ3Fc) but the .jar was not executable. It just print this Display parameters as parsed by Maven (in canonical form) and comparison result: to the console after running java -jar file.jar. (After the double dot there does not come more information.)
Next I found another youtube video that shows me it does not work like in the first video because of the maven dependencies (https://www.youtube.com/watch?v=HGHu-SzL-5E). In this video I do not know what to do in 5:39. He copies something but I do not know from where that is. I tried to put the same files in there like he did but it does not work. There comes a ClassNotFoundError that it can not be found the .fxml files. Then I do not write them in the command but all other .java files but it was the same error.
On this (https://introcs.cs.princeton.edu/java/85application/jar/jar.html) site I had my next try. I follow the instructions like this: I dreated a .mf file and put all .fxml and .java and .class files in the command but it can not be found the .class files so I put them out of the command. And then ist comes a ClassNotFoundError. It can not find the Main class. Intellij mark it in the .mf file red as well and I do not know why.
Then I follow the instructions of this (https://cwiki.apache.org/confluence/display/MAVEN/Tutorial%3A+Build+a+JAR+file+with+Maven+in+5+minutes) site but there an empty .jar file was created.
Now I do not know what I did wrong or what I miss so I hope someone can help me.

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

Main class not found, IntelliJ building a jar

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.

Error running Jar File

I have created a project in java project using NetBeans IDE. I have few forms in it. The netBeans created a Jar file by its own. when i try to run that Jar file its give an error "Failed to load Main-Class manifest attribute from". I Have set my Starting form in application and when i run from netbeans its running correctly but when i try to run my Jar file its give me this error. Please any one can help me how to solve this netbeans.
Thanks
I don't use NetBeans myself, but there's a good step-by-step guide which has screenshots for everything you need to build an executable jar file.
In particular, did you fill in the "Main Class" property in the project properties? I know you says you've filled in the "Starting form" but that may not be the same thing...
Look in the MANIFEST.MF file in the jar file. It should contain a Main-Class line containing the name of the class you want to start when invoking in this manner. If the line is there, doublecheck the spelling of both the class name and Main-Class.

Categories

Resources