Error: Could not find or load main class jar - java

I exported a Java project of mine from Eclipse to a JAR File, and after searching the internet for an hour, have still not resolved my problem. The jar file works on the computer that I compile, and exported it on, yet when I transfer it to my laptop it states that "A Java Exception has occurred" when I try to double click it, and when I run the code below, it retorts "Error: Could not find or load main class jar"
java -jar C:\Users\Me\Desktop\test.jar
I exported it once more after specifying to the MANIFEST.MF the location of my main class, yet the same error is being reported. I have seen the MANY other threads on this on this forum, yet the advice has either been too vague or just plain didn't work.
Thank you in advance.

Check the version of java you are running in your laptop, the error could present itself if you compiled your program with a different version that the one you are using to run it.

Related

How can I get stdlib.jar to work in IntelliJ IDEA?

I​ had used the IntelliJ IDEA installer provided in the booksite for Computer Science: An Interdisciplinary Approach and I had no problem to use the library. However, I noticed that installation had screwed my Git bash display settings, so I uninstalled everything.
Later, I installed the latest version of IntelliJ (2021.3.3), from the JetBrains website and apparently all the previous settings were overwritten, since I could no longer use the library. So, I downgraded back to the version provided in the booksite, however, I still can't use the Std library.
I've tried adding the versions of the library .jar files that I found:
On the Standard Libraries Princeton's webpage
On the Jar Download website
On this post from Stack Overflow
In the .jar file from the hello.zip file I found on this other Princeton webpage
Together and independently to the module and libraries, via IntelliJ IDEA, following the paths: "File > Project settings > Modules > + > JARs or directories" and "File > Project settings > Libraries > + > Java" , respectively, and choosing the corresponding directory/file, with no luck, since I've got the "cannot find symbol" error message:
Barnsley.java:16: error: cannot find symbol
StdDraw.setScale(-0.1, 1.1); // leave a 10% border
^
symbol: variable StdDraw
location: class Barnsley
For literally every single mention of the Std libraries in the code. I​ also tried editing the environment system variables and created a variable named "CLASSPATH" and linked it to the Stdlib.jar file and then, to the .lift folder that came with the hello.zip file, from the first week assignment. Again, no luck, because I kept getting the same error message.
Next up, I typed:
javac -cp stdlib.jar Barnsley.java
In the command line, and although that command itself didn't lead to any error message, when I tried to run the program, typing:
java -cp stdlib.jar Barnsley 10000
This error message showed up:
Error: Could not find or load main class Barnsley
Caused by: java.lang.ClassNotFoundException: Barnsley
I also vainly tried copying and pasting every .java file from Standard libraries webpage to the same directory where I keep the program files.
I know some (maybe lots of people) have asked the same or a similar question before, either here or in other forums, but I've been googling and looking for a solution for three days now and, as you can see, none of the reported solutions that I've found have been of help to me, that's why I decided to ask again, on my own.
Thank you for reading my post and/or for any help you can provide.
PS.: The Barnsley.java file in the error messages corresponds to the one provided in the 5th section of this webpage from the booksite.
[Edit - April 7th, 2022]:
I tried to follow #CrazyCoder advice, so I created a folder named "src", extracted all the files from the stdlib.jar file that came with the hello.zip file into src and also moved Barnsley.java to that folder. Next, Next, I tried to follow the instructions from the webpage he mentioned, as shown in the following screenshot:
And somehow, got the same error message when compiling:
I don't know what went wrong :C Any assistance would be highly appreciated.
[Edit - Later on April 7th, 2022]:
Something weird happened. I downloaded the file provided by #CrazyCorder and there was a warning about Amazon Corretto not being installed, that I decided to ignore.
I tried to run the program from the command line, with no luck, because I got the same error message as always. So, I noticed that there was no JDK assigned to the SDK slot in the Project structure, and I chose to use JDK 18 (following the path "File > Project structure > Project"), which is the one that I had previously installed on my computer, but that didn't really change a thing.
Lastly, I tried to run it from the IDE and that's when it finally worked!!! So, thank you, #CrazyCoder :D I still don't know why it doesn't work from the command line, though...
I also vainly tried copying and pasting every .java file from Standard libraries webpage to the same directory where I keep the program files.
This works just fine, you need to place all the .java files from the stdlib.jar into the sources root directory (marked in blue in the project view). Barnsley.java needs to be in the same directory.
The jar with .class files will not work in the dependencies/classpath as you cannot import classes from the default package in Java.
It was an extremely poor choice to keep the classes in the default package and it's the fault of the creators of this library/course. This simple oversight has caused major headaches over the years.

runnable .jar file runs on my windows device but won't on others(windows too)

So I've made up a little programm for my mom to use. I exported it into a runnable .jar, which is a first for me, so I obviously screwed it up.
Actually the file works just fine on my computer. But when I try it on hers, double clicking on the file just gives me an error : "could not find Main class : controller.Main". And the programm won't start
I used eclipse GUI to export it (right click on projet -> export -> runnable JAR), and it gave out a file working well on my computer.
If it is of any help, my programm uses a bunch of resources files, but I included them in the right place in the folder that I gave to my mother so it should not be the problem (besides, it would glitch out when trying to use the images, but not stop it from launching, according to some personnal tests)
What can be the Reason for it to work on my device, but not another one (both Windows) ? And how to fix this ?
N.B: I have included a MANIFEST.MS file in my project before exporting, but Eclipse didn't make any explicit mention to it when exporting, so I think it didn't take it into account. Besides, I reffered to my main class as Main (and not controller.Main as in the error message I got on my mother's computer). This was probably wrong of me, but I think it shows that Eclipse didn't care much for my manifest and kinda made his own.
Thanks in advance !
If you want a link to download the folder, to check it out yourself, let me know ! (it's a bit heavy though, there's quite a lot of images)
EDIT : I get different error messages with different computer. One computer would give me this first message :
first computer error
Another computer gives me those 2 error messages (one after another)
2nd computer error (1/2)
2nd computer error 2/2
And of course, it still runs fine on mine, just with a double click !
My .jar file (opened as an archive) contains :
3 folders corresponding to my packages : controller, model, view
1 folder META-INF containing the MANIFEST.MF generated by eclipse, looking like so :
Manifest-Version: 1.0
Main-Class: controller.Main
Class-Path: .
Ok ! I could figure it out. So there were actually 2 problems on top of each other :
first, I think Martin's suggestion was correct. After researches on the new error message, it became clear that native calls had probably had Something to do with the problem, although I have no idea what native Library I was using. I could solve this first part of the problem by selecting another option to handle libraries in the eclipse export feature : I chose the option "package required libraries into genereated JAR" (instead of "extract required libraries into generated JAR" before), and it make the error message go away !
Even though the message disappeared, the app would still not launch. But the problem was more obvious this time; I Simply compiled and exported the file with a java 9 JDK for eclipse, while trying to run it on a java 8 software (I think java 9 is only available in its developper version to this day). So I just downloaded java 8 JDK, configured the build path to use the latter, and it finally worked on the other computer.
Thanks to Martin who helped me get on track with the right questions !

How to Include External Jars and DLLs in Executable Jar

I've found a bunch of Stack Overflow Questions and Answers, but so far I haven't found any that work for me. I am trying to make an executable jar in Eclipse from a program which requires an extra jar and a dll. The way I got this to work properly in Eclipse is by configuring the Build Path as shown in
this image. The hierarchy of the project is this. I've tried messing with the Manifest file a bit, but I haven't made any progress with that.
I've been running the jar by using java -jar /path/to/jar and I keep getting an error saying Exception in thread "main" java.lang.NoClassDefFoundError: com/jacob/activeX/ActiveXComponent .... I've tried defining a path to the jar using -Djava.library.path=/path/to/jar and I get an error saying Error: Could not find or loaded main class outlook_dl_2.jar.
Is there any way to solve this without using a third-party application like One JAR? That's the only thing I can find that I haven't tried and I'm trying to avoid it if possible, since I feel like it really shouldn't be necessary. I'll use it as a worst-case scenario, but I would like to get this working without it.
Anyone know what I'm doing wrong here?

Coldfusion Gateway New type: Unable to find or load Gateway class

Playing around with ColdFusion gateways, I ran into a problem. Following the steps from this document, I put my jar file into the gateway\lib folder. I then restarted my server and tried to add the gateway to my list of gateways already installed. (Mind you, I did not install the gateways that were already on this system.)
I get the error:
Error creating gateway type.
Unable to find or load Gateway class [my entered class name here.]
Adding a new gateway with a classname that is already in the list, but with a new Name successfully creates a new gateway. So there's nothing (much) wrong with my administrator interface.
I've compiled the Java code with Netbeans. Added the jar file to the gateway\lib directory and restarted the server. (Even restarted the computer to be sure....)
I've verified that I compile and run with the same Java version: 1.7.0_45.
Opening my jar file with Zip and comparing it to a known working jar file shows no real differences in structure. Nor is there an major difference in the manifest. The manifest file in my project shows a newer Ant version, but that's all.
To compile the project, I imported cfusion.jar from: C:\JRun4\servers\cfusion\cfusion-ear\cfusion-war\WEB-INF\cfusion\lib\cfusion.jar
This seemed to satisfy all of the required imports in the project. I then compiled the jar file successfully. Am I missing something? Should there be other things linked in there as well?
Are there other things I can check? Log files don't really tell me anything.
Thanks in advance for any pointers you can give me..

Libgdx Exported Jar - Can't find main class - LD25

I'm doing the Ludum Dare 25, using Java Libgdx trying to export my jar.
It was working absolutely fine earlier, I exported and could run it fine. Now when I export it the exact same way, it crashes as soon as I open it. Running via CMD I get the error
'Could not find or load main class'
I've opened it up in winrar and checked the Main class is there and the manifest points to the correct place. I don't understand what's wrong.
All help appreciated, many thanks!
Solution Found
Odd issue, despite the error message had nothing to do with the class, the jar exporter made one of my image files lowercase or something, so that it was messing up it's references

Categories