简体   繁体   中英

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)

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM