Java - IntelliJ generated executable jar not working - java

I have generated an executable jar in IntelliJ IDE. But when I try executing the jar file from windows folder it does not work. When I try running the .jar file from within the IDE in debug mode it gives the below error:
no main manifest attribute, in D:\... the file path
To generate the executable jar I referred to Link
I referred to Link for the no main manifest attribute issue but dint help as in my case its a Gradle build
(Note: If I execute the program(.java file) from within the IDE it works fine, so I believe the issue is not with the program)
Hope the issue I face is clear, await guidance

Adopted Gradle build tool to generate Jar.

Related

Java Could not find or load main class when running jar file

I have a springboot application, it runs just fine in intellij. I'm trying to create an executable jar to run the application on other machines. I followed this
tutorialhttps://www.jetbrains.com/idea/guide/tutorials/hello-world/packaging-the-application/
down to every step, and managed to build my jar. but when i try to run the jar with
java -jar nameofjar.java
I get a Could not find or load main class error.
I unzipped the jar file with java c and the classpath correctly points to the main class, so im really lost as to what i have to fix here to get java to detect the main class.
I've tried a bunch of solutions from here:
Error: Could not find or load main class in intelliJ IDE
such as deleting .idea or Rebuild Project or doing mvn clean package but nothing seems to work.
Using java -c to open the jar file and verify that the main classpath is indeed correct
Main class and file structure:
This run config works perfectly fine in intellij
here is my manifest.mf:
Manifest-Version: 1.0
Main-Class: com.owl.PosApi.OwlPosApiApplication
If you build jar artifact using IntelliJ IDEA, it may be incomplete or manifests from the other included dependencies can override your custom one with the main class defined.
Related answers:
https://stackoverflow.com/a/42200519/104891
https://youtrack.jetbrains.com/issue/IDEA-116209#comment=27-656589
https://stackoverflow.com/a/45169655/104891
https://stackoverflow.com/a/42660624/104891
The proper solution would be to build the artifact using Maven. It may work out of the box with mvn clean package or you may need to modify pom.xml to build the fat jar with all the dependencies per this document.

IntelliJ generated JAR, libraries don't work

I have included libraries using File->Project Structure->Libraries:
In Artifacts, I made sure to include the library in the output JAR file:
When I run the program inside IntelliJ, it works fine. However, when I try to run the JAR file it generates, it displays a NoClassDefFoundError:
So the libraries aren't working. How can I get libraries to work in IntelliJ?
(I have never run into the problem before...)

How to open a file from jar?

I have a project where I open some XML files to read and edit information.
This works fine when running in the IDE (Netbeans) but fails when I am building the project and running the .jar file.
Is there a reason why this might not work?
The path of the project in Netbeans is
NetBeansProjects\project\src\data
but when I am running the jar file, this path is
NetBeansProjects\project\build\classes
However, when I create the files in this folder, they still cannot be opened.
The XML files need to be in the classpath when the jar file is executed.
You can give the classpath using -cp option when executing the jar file. Similar question was answered here as well - Run a JAR file from the command line and specify classpath

Exception: com.mysql.jdbc.Driver | Eclipse isnt exporting jar

again me.
I dont know what the hell isnt working right, but everytime im exporting my Teamspeak Bot wrote in Java, it seems like he dont export the mysql-connector.jar.
Everytime im trying to start my jar, he always tells me that he cant find the MySQL-Driver.
Heres my Code:
As you can see, the jar file is in the buildpath. When i try running it through Eclipse, everything works fine. Only when im exporting he throws me the Exception.
Hope someone of you can help me.
ndslr
The way you're exporting your jar, your library jar isn't automatically added to the output jar. You would have to specify the classpath when you run it:
java -cp "C:\Users\Andy\Desktop\mysql-connector.jar;YourOutputJar.jar" TS3BotMySQL
Instead, you have to add the mysql-connector.jar to your output jar. To do this, instead of going to export > jar, go to export > runnable jar, and then select "Extract required libraries into generated JAR" or "Package required libraries into generated JAR" at the prompt.
See these related questions for more information:
How to create a jar with external libraries included in Eclipse?
Eclipse Java; export jar, include referenced libraries, without fatjar
Generating a Jar in Eclipse including external library
How export Java jar from Eclipse with dependencies on other jars?

Unable to export any of my projects into a .jar file

Everytime I try to export any of the projects from my workspace into a Java jar file, I end up with an error:
JAR creation failed. See details for additional information.
Class files on classpath not found or not accessible for: 'Server Manager/src/me/Zahach/ServerManager/main.java'
And this error is repeated for every class in my project.
I tried reinstalling Eclipse, that didn't work.
I then tried to recreate a project and drag and drop my packages into it, that didn't work either.
What is going on?
The error is caused because of a missing .class file(s) for corresponding .java file(s)
In Eclipse
Goto Project->Clean and Rebuild the Project and then try exporting,
If it doesn't work then make sure .class file exists in the bin folder for main.java file in
your case
Another solution is to ensure that the Classpath is Valid
Also make sure that the permissions in your workspace folders are ok. I ran into this problem after updating Eclipse and then downloading some Maven dependencies.
In the end, what fixed it was doing a sudo chmod 777 -R workspace/MyProject
I have faced similar problem while exporting my java files into a compressed jar file, so I checked the Java Build Path of my project and found that the External jars I was referring to from the project are not present in the designated folder locations. Once I have resolved that, I was able to export my java files into a jar.
You may have any java file that is totally empty. You have that file in project with somethingName.java but inside it is nothing. So no bytecode gets generated for it and it throws an error. You can find that file in error details and delete it and try building jar again. It works :) Thumbs up
I had empty Java file classes causing the error.

Categories