0

I wrote a really simple script with only one command and this one is working in the interactive shell but not in a script.

#!/bin/sh
echo "--- Running game ---"
java -cp lib/java-json.jar:bin:. com.theaigames.game.warlight2.Warlight2 "../example-map.txt" "java -cp bot.BotStarter" "java -cp bot.BotStarter" 2>err.txt 1>out.txt

When I display the content of err.txt after executing the script I get this :

Error: Could not find or load main class com.theaigames.game.warlight2.Warlight2

But when I use it in an interactive shell the program is executed :

Exception in thread "main" java.lang.NoClassDefFoundError: org/json/JSONException
at com.theaigames.game.warlight2.Warlight2.setupGame(Warlight2.java:91)
at com.theaigames.engine.Engine.start(Engine.java:89)
at com.theaigames.game.warlight2.Warlight2.main(Warlight2.java:293)
Caused by: java.lang.ClassNotFoundException: org.json.JSONException
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 3 more

(The java error isn't the problem here)

2
  • Check the environment for differences, like the class path or the current working directory. Oh, btw, the "linux" tag was misapplied (see its description!) and the subject is also a bit misleading, the java program works fine in both situations, it's what is going on inside which isn't. Commented Mar 18, 2018 at 11:15
  • @UlrichEckhardt Indeed as answered below it's a problem about the current directory. I still don't understand why the output is different while I'm in the same directory when executing the script or the command. Commented Mar 18, 2018 at 11:42

1 Answer 1

1

It might cause by current working directory path. You can make sure by using -Duser.dir JVM argument.

Or, you can change the path before executing java.

#!/bin/sh
cd <your dir>
java <some arguments>
Sign up to request clarification or add additional context in comments.

1 Comment

I've just added this before executing. if [! -d "bin" ]; then cd $HOME/Documents/IA/Project/warlight2-engine-master/bin fi In my project there are three directories (bin, src, lib). The script is bin. Now when I execute the program while I'm in the project folder it works but if I'm not it's not working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.