Java -jar ERROR: could not find or load main class - java

I am making a 2d game, which I have compiled into a .jar to test if it works.
When in run the jar file with java -jar command I get the following error:
Error: Could not find or load main class com.grizeldi.splatoon.Main
I know that there are many solutions here on StackOveflow, but I have tried updating java, messing with -cp... Nothing worked so far.
Code: github repo
Help anyone?
EDIT1: I have my code on a USB stick, but I have tried moving it to C: and D: but nothing worked.
EDIT2: I have added the .jar on the github.

One way is to update the Manifest with the classpath to the other jars:
Class-Path: lib.jar
An alternative is to add the contents of the jars your main jar depends on to your main jar, so you have only one jar file. Make sure you don't add the jars themselves to the main jar, as nested jars won't work.
When dealing with native libraries (.so, .dll), simply place these in the same directory as the jar. The downside of this is that you have multiple files.
In that case, it might be easiest to just add a startup script (.sh, .bat), specifying the classpath and the classname of Main, aswell as a -Djava.library.path.
There is also another way: extract the native library from the jar at runtime, save it to a temporary location, and load it explicitly. See here for more info on that.

Related

java.lang.NoClassDefFoundError with jar files

Uploaded a jar file from my computer to a server and tried to run it. When I run it I get java.lang.NoClassDefFoundError and it seems to be related to the twitter4j jar that my main method depends on.
However, I have this jar file in my libraries so shouldn't this be included when I build my code in to a jar? Here's a pic in case it helps.
is the error that I'm getting. (can't upload a pic just yet.
Not sure what this has to do with twitter, but anyway, the issue is that you do not have the correct class files. In other words, when you are running your fat JAR in the command prompt, you do not have any libraries exported with it (Or if you do, you don't have that specific one).
Sometimes such an error can be because there is an incorrect version of java, however that is not the case here since java has got no "twitter" packages or classes in it.
Using something like JarSplice would fix this.
Assuming you did not package the twitter4j classes inside your application jar, you need to tell Java where it can look for classes that are not inside your application jar. You typically use the classpath flag for that. In your case, it should look something like
java -cp /tmp/twitter4j.jar -jar /tmp/myapp.jar
An alternative would be to package all twitter4j's classes inside your application jar. This is called a 'fat' jar. How to make one depends on how you build your application jar.
The JAR file that you are trying to use needs to be in the classpath. This can be done by using the -cp attribute from the command line. However, when using java -jar, you cannot use the -cp attribute.
To get around this, you can do the following:
java -cp /tmp/myapp.jar;\path\to\external.jar com.example.package.MyClass
where MyClass has the main() method defined.
Alternately, you can specify jar files on the classpath using the manifest.mf file. See http://docs.oracle.com/javase/tutorial/deployment/jar/manifestindex.html for details.

java.lang.NoClassDefFoundError. Class not found during runtime.

I am working on eclipse, and I have the need to use external library's. For example Jsoup and JXL.
Now what I have done so far is: First created a "lib" folder in my project folder. Afterwards in eclipse, click on project properties, Libraries tab, add external jar and added the jar in the lib folder.
So this solve my compilation issue. Now, when I run the program (I go to project/bin and in the console execute: java ProgramName ; I get
java.lang.NoClassDefFoundError:
Now to testing, I added the Jar file to the folder where Main.java is and Now, I have been able to run the program doing the following:
javac -classpath ./path/to/jar Main.java
java -classpath ./path/to/jar:. Main
And this works.
So the first thing that comes to mind is that I have to tell java where to find the respective libraries. If this is correct? How do I do it?
java -cp ???(dont know what to put here)
But moreover. I have another issue. I am writing this program in a computer, but I am going to use it in other which probably don't have those libraries. How do I solve this issue?
I like to use something like the following:
java -cp myjar.jar;lib/*.jar com.foo.bar.MyClass
This adds not only my jar to the classpath but those in the lib directory as well.
If you want to run your jar on another computer, you will need those jars as well, you cant just have your jar. Why not just also package your lib directory along with it?
To get your program to run you have two paths to worry about
The path to the jar files that are your applications dependencies (like jsoup.jar) (lets call this lib)
The path to the directory containing the classes of your app (lets call this classes)
The general form of the command line you need is:
java -cp lib/jsoup.jar:classes Main
If you have more libs
java -cp lib/jsoup.jar:lib/jxl.jar:classes Main
A general note on packaging your app for release to other computers. You might want to consider making a jar of your own app, probably best done using http://ant.apache.org/manual/Tasks/jar.html
Another option is to produce a "one jar", which makes one large jar, bundling in all the classes you need from your libs and all the classes in your app. You can then make the jar executable for a nice out of the box solution. Have a look at http://one-jar.sourceforge.net/ and https://code.google.com/p/jarjar/
if you have this structure:
project folder
... code
... libs
then from the code folder:
javac -cp .;../libs/*.jar yourmainclass.java
java -cp .;../libs/*.jar yourmainclass
When you need to compile and run this project, take all the folder and do the same in other machine.

Exported Runnable Jar File - I know what's wrong but not how to fix

How to export to runnable java file with eclipse using slick and lwjgl (Light Weight Java Graphics Library)?
So, I have done some research into this and believe I have found the root of the problem and what needs fixing, now I need to figure out how to fix it.
I have a fairly large project that use slick and lwjgl. These are both provided as .jar files that I have added to the java build path in my IDE (Eclipse INDIGO, in case you where wondering).
My goal is to export the piece into a single runnable jar file. Eclipse gives me three options for this:
Extract required libraries into generated JAR
Package required libraries into generated JAR
Copy required into a sub-folder next to the generated JAR
I have been gunning for the second options and it seems the easiest to deal with logically. Anyway when I select this option I press the FINISH option (the only one available to me other than BACK and CANCEL) I get the generation progress popping up for a sec and all seem well. However, if I run the resulting file I get an Unresolved Linker Error as it cannot find lwjgl.
Seeing as both slick.jar and lwgjl.jar are inside the generated JAR when viewed with winRar, I assume that some information is being lost about the java build path when everything goes from the IDE to the runnable JAR.
My research so far seems to suggest I can point the generated JAR to the enclosed files it needs through something in the manifest.txt file but this is were I have been unable to find anything more concrete.
Can anyone please advise me where to go from here?
P.S: I have tried almost every permutation of the export option and environmental options for the build path but when I run the generated JAR it get various differing errors that still all mean "I cannot find this here external".
P.P.S: I forgot to add, eclipse itself is mentioned in the manifest file as follows:
Manifest-Version: 1.0
Rsrc-Class-Path: ./ lwjgl.jar slick.jar
Class-Path: . lwjgl.jar slick.jar
Rsrc-Main-Class: Game.Game
Main-Class: org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader
Every bit of common sense tells me that needs to be changed as it refers to the .jars' location but again, not sure what to or where to begin.
You could use JarSplice, just export the jar from Eclipse, with your classes and resources (don't include any libraries at this point), then use JarSplice to add any libraries and natives (like lwjgl and slick) and make the jar runnable.

How to run examples of an API (which is already having workspace) using eclipse IDE

I am new to Java and I am not sure how apt is my question title. Please suggest any better title.
I have got a Java API which has many executable modules which do various things related to parsing MS Outlook's .pst files. And my problem is that I am getting this exception when I run the execute command to execute a .class file of one module of the API(java -classpath /opt/Java/libs/JPST/lib/jpst.jar /opt/Java/libs/JPST/examples/GetInboxMessages/bin/Example)-
Exception in thread "main" java.lang.NoClassDefFoundError: /opt/Java/libs/JPST/examples/GetInboxMessages/bin/Example
Caused by: java.lang.ClassNotFoundException: .opt.Java.libs.JPST.examples.GetInboxMessages.bin.Example
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: /opt/Java/libs/JPST/examples/GetInboxMessages/bin/Example. Program will exit.
So far ...
You can check my previous question where I concluded that I was correctly running the compile and execute commands. But I am still getting the above mentioned `exception.
I asked the API's developer who says that I should get Eclipse or Netbeans IDE. He said that the
examples folder inside the API's
folder structure is entire Eclipse
workspace.
and also said that
It is easier to compile and run all
examples.
And now ...
So, I have setup Eclipse Helios for Java. Can anyone tell me how is it easy to compile and run all the examples (I think the modules inside the examples folder) using the IDE. I have earlier worked with Eclipse for PHP but never for Java.
Folder structure of the API
I need to execute a .class file of one of those modules (many module folders are present inside examples directory). There are many such modules present inside the examples folder of the API. Each of these modules folders contain a bin folder (containing .class file) and src folder (containing .java file) . I need to execute one such .class file.
There is a lib directory containing a .jar file (which is needed to be referenced by my target .class file). The lib directory is at the same level as the examples directory.
Solution
I have verified that the solution given by Jonathan works (verified only without-using-elipse case). To make it more clear the classpath needs to include all the class locations in both compile and execute commands. So, if you need to compile and execute an Example.java file with a dependency Parent.jar do this:-
Compile
javac -classpath /path/to/Parent.jar/file/Parent.jar:/path/to/Example.java/file/ Example.java
Execute
java -classpath /path/to/Parent.jar/file/Parent.jar:/path/to/Example.class/file Example
And note the separator - should be : for linux and ; for windows.
Thanks,
Sandeepan
When Eclipse starts it should ask you which workspace to use. Select the examples folder and, if it really is an eclipse workspace, you should see all the projects already set up (it may take some time to build everything depending on the size of the projects and the speed of your machine). Then simply find the class you want and run it. If everything is set up correctly it should just work. If not you may have to go back to the developer for more details.
But I don't think you need Eclipse to run that one class. Looking at the stacktrace you provided at the beginning of your question it looks like it failed to run because you haven't set your classpath properly. Try:
java -classpath /opt/Java/libs/JPST/lib/jpst.jar:/opt/Java/libs/JPST/examples/GetInboxMessages/bin Example
To very briefly explain: The classpath lists the places where Java looks for the classes it needs. You specify the class to run relative to the classpath, not as a file path. Since the class you are trying to run in in the folder "/opt/Java/libs/JPST/examples/GetInboxMessages/bin" this folder must be on the classpath (note, I am assuming a unix based system, if you are using windows use ';' to separate entries not ':'). Then simply specify the class to run by itself relative to the classpath (which in this case is just the class name by itself).
Please be aware this is a very basic overview, things get a more complex if you start using packages (folders that would appear below the bin folder). This is one advantage of using a program like Eclipse since it will handle a lot of this for you.
Sounds like a classpath issue.
Right click on your project in eclipse.
Go to buildPath > configure build path
Make sure the jar file is referenced in the libraries tab and if it isn't add it.

where to place jar in order to run a program from command-line?

this will sound silly but i am executing my code from command prompt and have to use a jar in my class.
I have placed my jar in lib folder of JDK..
but i am still getting error of file not found
any explanation??
EDITED : guys tried all but still not working
EDIT 2 :i am trying to work as was told by this link i am using js-1.6R5.jar
Edit 3 : i undestand all the things you ppl have told but nothing working for me.. pls give me a link to upload my example that i can share with you all.
Edit 4 : i am fed up by setting classpaths but its not working... i have SDK installed in my system, do i need an extra JDK to run my programs from command prompt??
You need to add the jar to the class path by doing the following...
java -classpath D:\myprogram;D:\myprogram\lib\supportLib.jar org.mypackage.HelloWorld
Please see Wikipedia - Classpath_(Java)
You can place it anywhere, as long is you include it in your classpath. See Setting the Class Path for how to include jars in the classpath.
Have in mind that adding something in the JDK lib is almost never a good idea.
You can make a lib folder in your application's directory and put jar files there, then make your application find them by adding lib to your application's classpath.
And, don't put your jar files in JDK's lib folder. It's not good practise.
You need to let Java know that you want to include the jar in your classpath (the list of folders and jars it checks for classes). One way to do this is with the -cp command line argument, something like
java -cp ".;pathToMyJar\myJar.jar" MyClass
Another is to edit the CLASSPATH environment variable in your OS to include your jar.
A simple solution will be to place the jar fiel inside the windows folder if you are doing it in a Windows machine.
Unfortunately your question contains a lot of question signs and few information.
If you are using java.io.File to open jar as a regular file this jar should not be in lib directory. You just have to provide correct path in file system.
If however you are just trying to use jar as a part of your application it should be in classpath. Use either command line java -cp myjar.jar MyMainClassor put full path to this jar to global environment variableCLASSPATH`.

Categories