java.lang.ClassNotFoundException while executing .jar - java

I have created a .jar of my image processing project which runs on intellij idea.While running .jar using java -jar project.jar it outputs an error Error: Could not find or load main class sample.Main. But when i extract the .jar and go to the inside and open the terminal and then run java sample.Main [sample is the package] , it works perfectly. I think problem may be the class path. How can i solve this.

Related

Resolve issue with creating and running a jar file in IntelliJ java. Error: Could not find or load main class TestJar

I have created a main class (TestJar) and two addition classes which run from the main. I can run the application from IntelliJ with no issues but when I build and run the jar file if fails.
"C:\Program Files\Java\jdk-17.0.2\bin\java.exe" -Dfile.encoding=windows-1252 -jar D:\Data\JavaPGMS\TestJar\out\artifacts\TestJar_jar\TestJar.jar
Error: Could not find or load main class TestJar
Caused by: java.lang.ClassNotFoundException: TestJar
Process finished with exit code 1
I was able to create the jar file and run it up until I added the code in one class file which includes the apache.poi library.
I had same problem with JavaFX exporting. You can export your library with project (fat jar using gradle/maven) or declare path to library in VM options.

Running Java files inside a package with a jar library

Im extremely new to Java so please bear with me.
I would like to run a java project from the terminal.
The directory looks like this :
I went on the terminal inside the "src" folder and tried java InnerFolder.Main but got the following error msg:
Error: Could not find or load main class
How can I run my project?
edit:
compiled files

java run class using jawin

I'm trying to run a class that I wrote that is importing the jawin library.
My command line is as below:
java -cp C:\class_path ClassName
I'm getting:
Exception in thread 'main' java.lang.NoClassDefFoundError org/jawin/funcptr
What am I doing wrong?
Doesn't the JAR that I'm adding to the build path get into the binary file that I've compiled?
If not, then when I publish my class file how will it know to find the external JAR? What if it doesn't exist on the target computer?

Jar File Won't Execute

Alrighty, so I packaged my files into an executable .jar using Eclipse, however when I try to run it by clicking or opening it informs that it "could not find or load main class Calculator.jar".
However, if I launch it from the command prompt, (java -jar Calculator.jar), it runs like a charm. What's the issue here?
maybe you can try this: Jarfix

Exporting project in Java(Eclipse)

I wrote a simple game with 2D graphics in Java (Eclipse) and lib Slick2D. When I export project to Runnable Jar File in Eclipse and try to run this file just nothing happens. I can run this game in Eclipse without any problems. When I write java and put my jar in console to get path and hit enter i have sth like that:
C:\Users\Me>java C:\Users\Jolanta\Desktop\pnd.jar Error: Could not
find or load main class C:\Users\Jolanta\Desktop\pnd.jar
In launch configuration while exporting I selected right main class. Any ideas?
it's command for starting jar file with main class:
java -jar C:\Users\Jolanta\Desktop\pnd.jar
You need to provide native libraries along with the jar file. Here are some scripts that will launch your game:
windows (.bat file) :
java -Djava.library.path="native/windows/" -jar pnd.jar
linux :
java -Djava.library.path="native/linux/" -jar pnd.jar
where your directory will look like this:
your .jar and scripts are in the same directory along with 'native' folder.
not sure where I got this folder from, I can send it to you if you want

Categories