0

Been looking for around 2 hours for a solution to my problem, tried countless solutions and still the problem remains unsolved.

I have a game project with few packages; geometry, levels, decoration, and game. Inside the game package resides a gameRun.java

package game;
public class gameRun {

/**
 * Program entry point.
 * @param args the arguments for the levels
 */
public static void main(String args[]) {
    ... some code
}
}

and I have the following manifest file :

Main-Class: game.gameRun
Class-Path: someExternalGraphicTool.jar
//newline here

I compile my whole game using the following command : (the bin folder already exists).

  javac -d bin src/geometry/*.java src/decoration/*.java src/levels/*.java src/game/*.java

I then turn my compiled classes into an executable .jar file using the command :

jar cvfm t.jar manifest.mf bin/geometry/*.class bin/decoration/*.class bin/levels/*.class bin/game/*.class 

I then try to run my t.jar using :

java -jar t.jar 
//or 
java -cp .:t.jar game.gameRun

which both produce the error, "Error: Could not find or load main class game.gameRun" However, when I unzip the jar file, inside the bin/game directory I can see a gameRun.class file and inside the META-INF\MANIFEST.MF file I can still see that the Main-Class is set to game.gameRun.

Note that I am working on a remote linux server, on a command line, and the jar file doesn't work even if I download it to my windows 10 machine.

What did I do wrong during this process ? Thanks for any help.

Eliran Ziv
  • 320
  • 1
  • 2
  • 13
  • If your class is called `gameRun`, then your manifest should read `Main-Class: gameRun`, I think. By the way, Java naming convention is that class names always begin with an uppercase letter. You should follow conventions. – Tim Biegeleisen May 22 '18 at 11:46
  • The gameRun class is inside the game package, so by what I read I should type game.gameRun. I just tried making the jar with what you suggested and it yields the same error just regarding gameRun instead of game.gameRun. – Eliran Ziv May 22 '18 at 11:48
  • Sorry...I assumed you didn't have a package. – Tim Biegeleisen May 22 '18 at 11:49
  • Edited the code to show that it's inside a package now. – Eliran Ziv May 22 '18 at 11:50
  • Is the manifest something you are creating or is it created automically? Would it be possible to include the entire manifest? – npinti May 22 '18 at 11:52
  • I create the manifest, I'll include the whole manifest. – Eliran Ziv May 22 '18 at 11:53
  • you can try **start javaw -Xmx200m -jar C:\path\abc.jar** – JAMSHAID May 22 '18 at 11:54
  • @JAMSHAIDIQBAL it does not produce the error, but it doesn't work either. – Eliran Ziv May 22 '18 at 11:57
  • You shouldn’t have a bin package inside your jar, that will completely confuse the JVM. – vandench May 22 '18 at 11:59
  • @vandench Well, I think the bin folder inside t.jar is created automatically, as a part of the -jar command. Or if it's not automatic, how can I prevent it from happening ? – Eliran Ziv May 22 '18 at 12:01
  • are you using the command line interface to create jar file? – JAMSHAID May 22 '18 at 12:01
  • @JAMSHAIDIQBAL yes. that's my only option because I'm working on a remote linux server that only provides me a command line. – Eliran Ziv May 22 '18 at 12:02
  • [link](https://docs.oracle.com/javase/tutorial/deployment/jar/build.html) you should try this official tutorial for more info as i am not much familiar with the command line interface of java. i hope it may help – JAMSHAID May 22 '18 at 12:04
  • @JAMSHAIDIQBAL I've already read that, thanks. – Eliran Ziv May 22 '18 at 12:05
  • [question](https://stackoverflow.com/questions/14821715/java-use-command-prompt-to-create-jar-file-from-multiple-class-files?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa) maybe this question might help – JAMSHAID May 22 '18 at 12:07
  • @JAMSHAIDIQBAL Thanks you for you help, but unfortunately I've already seen the question and tried removing the manifest file and only specifying an entry point using -e flag, but it didn't work. aswell – Eliran Ziv May 22 '18 at 12:11
  • @EliranZiv the bin folder appears to be an artifact of the javac command. Since the compiled classes don’t list it in their package and the manifest doesn’t see it for the main class it will cause problems. – vandench May 22 '18 at 12:15

1 Answers1

1

Your classes are in a wrong structure inside a jar file, because of the bin folder. My suggestion: pack it all into a jar starting in a bin folder.

Now when you extract your jar you will see 2 folders: META-INF and bin.

If you make the jar from bin folder you will see: META-INF and game, and it will work.

It doesn't work for you simply, because it can't find the main class, since it is inside the bin/game/YourClass.class, not in game/YourClass.class.

...\bin> jar cvfm t.jar manifest.mf geometry/*.class decoration/*.class levels/*.class game/*.class

And then just:

...\bin> java -jar t.jar

Shadov
  • 5,421
  • 2
  • 19
  • 38