0

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.

grizeldi
  • 180
  • 2
  • 12

1 Answers1

1

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.

Community
  • 1
  • 1
Kenney
  • 9,003
  • 15
  • 21
  • I had all jars in one folder (libs + splatoon2D.jar). Should that work? – grizeldi Nov 04 '15 at 18:44
  • -jar documentation says that when it is used, -cp is ignored I think – grizeldi Nov 04 '15 at 18:45
  • I don't think so; If I run `java` it says `... or java [-options] -jar jarfile`. If you want everything in your jar file, you'll have to unzip the libraries and add the contents to your jar. – Kenney Nov 04 '15 at 18:47
  • Hope that this one works ;-) Sorry about the confusion! – Kenney Nov 04 '15 at 18:55
  • it's ok ;). Is it ok if i just add lib jars to my splatoon jar? Because that didn't work. – grizeldi Nov 04 '15 at 18:56
  • No, nested jars won't work without custom code. You really have to unpack them first, and take care that your manifest won't be overwritten. If you use Maven to build, there's some tasks that can do all this automatically. – Kenney Nov 04 '15 at 18:59
  • I'm using Intellij Idea's default jar creating thingy. I have tried adding all paths to classpath, but it again didn't work. – grizeldi Nov 04 '15 at 18:59
  • I think that the paths need to be relative to the jar that the manifest is in. Maybe Idea puts `lib/lib.jar` instead of `lib.jar` in the manifest? What's the contents of the manifest now, exactly? – Kenney Nov 04 '15 at 19:02
  • I added all extracted libs to my jar and it kind of worked. Now it wants natives. Where should I put them? – grizeldi Nov 04 '15 at 19:03
  • TADA! I figured it out. Had to place natives in the same folder as my jar. Now if you edit your answer to something like "add all extracted libs to your jar", I'll accept it. – grizeldi Nov 04 '15 at 19:06