Florian Pellegrin
Florian Pellegrin

Reputation: 59

Command java works in interactive shell but not in shell script

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)

Upvotes: 0

Views: 200

Answers (1)

H.Tibat
H.Tibat

Reputation: 373

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>

Upvotes: 1

Related Questions