简体   繁体   English

命令Java在交互式Shell中有效,但在Shell脚本中无效

[英]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 : 当我在执行脚本后显示err.txt的内容时,我得到了:

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) (Java错误不是这里的问题)

It might cause by current working directory path. 它可能是由当前工作目录路径引起的。 You can make sure by using -Duser.dir JVM argument. 您可以使用-Duser.dir JVM参数来确保。

Or, you can change the path before executing java. 或者,您可以在执行Java之前更改路径。

#!/bin/sh
cd <your dir>
java <some arguments>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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