简体   繁体   English

从CLI运行Java可以正常工作,但从SH执行相同操作不会使Java找到该类

[英]Running Java from CLI works fine, but executing the same from SH won't make Java find the class

I am trying to run a Java server on Linux, which works fine when I run it from the CLI. 我正在尝试在Linux上运行Java服务器,当我从CLI运行它时,它可以正常工作。

The following code works if I execute it from the right folder (requires some files, so I can't execute it from another folder at this point): 如果从正确的文件夹执行以下代码,则该代码有效(需要一些文件,因此目前无法从另一个文件夹执行该代码):

java -classpath "/var/server/dist/*" net.world.WorldServer

As soon as I make an SH file with this exact same code in the folder, and I execute it with the following command: 一旦我在文件夹中使用完全相同的代码制作了一个SH文件,并使用以下命令执行该文件:

./launch_server.sh OR bash launch_server.sh

I get the following error: 我收到以下错误:

Error: Could not find or load main class net.world.WorldServer

I have been searching Google for people who have a similar problem, but could not find an answer that would help me with it. 我一直在Google搜索中寻找类似问题的人,但是找不到可以帮助我的答案。

The following I have tried did not help; 我尝试过的以下内容没有帮助;

  1. Set CLASSPATH before execution (in the SH file) [export CLASSPATH="/var/server/dist/*"] 在执行之前设置CLASSPATH(在SH文件中) [export CLASSPATH =“ / var / server / dist / *”]
  2. Set CLASSPATH as a server environment variable [export CLASSPATH="/var/server/dist/*"] 将CLASSPATH设置为服务器环境变量[export CLASSPATH =“ / var / server / dist / *”]

My Java version: 我的Java版本:

Java(TM) SE Runtime Environment (build 1.8.0_20-b26)
Java HotSpot(TM) 64-Bit Server VM (build 25.20-b23, mixed mode)

CentOS version: CentOS版本:

CentOS release 6.4 (Final) [64-Bit]

I am executing this all as "root" and I have full access to the server. 我以“ root”身份执行所有操作,并且拥有对服务器的完全访问权限。

If you need more information to solve this, please let me know in a comment, and I will get the information as fast as possible and edit the question. 如果您需要更多信息来解决此问题,请在评论中让我知道,我将尽快获得信息并编辑问题。

Thanks in advance! 提前致谢! ^_^ ^ _ ^

When running directly on a terminal, "/var/server/dist/*" is getting expanded by bash before passing the string to java , so it ends up being a valid classpath ( /var/server/dist/some.jar:/var/server/dist/other.jar... so on). 当直接在终端上运行时, "/var/server/dist/*"在将字符串传递给java之前被bash扩展,因此它最终成为有效的类路径( /var/server/dist/some.jar:/var/server/dist/other.jar...等)。

To make it work inside a shell script you need to build the classpath string, something like 要使其在Shell脚本中运行,您需要构建classpath字符串,例如

classpath=""
for i in `ls /var/server/dist/*jar`
do 
   classpath=$i:$classpath
done

java -classpath $classpath

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

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