简体   繁体   English

另一个不寻常的Java ClassNotFound

[英]Another unusual java ClassNotFound

I have a small programm and several .sh files to execute it. 我有一个小程序,还有几个.sh文件来执行它。 I use Raspberry-pi virtual box emulator to run it. 我使用Raspberry-pi虚拟盒模拟器运行它。 (Don't ask why... just need it.. :) ) So, my .sh file looks like: (不要问为什么...只需要它.. :))因此,我的.sh文件看起来像:

export HSQLDB_JAR=../lib/hsqldb-2.2.8.jar

java -classpath $HSQLDB_JAR org.hsqldb.Server -database.0 ../my-hsqldb/my-hsqldb -dbname.0        my-hsqldb

I have the needed jars, java is installed properly (checked several times), blah-blah, all the same: 我有需要的jar,java是否正确安装(检查了几次),等等,都一样:

rpi@RaspberryPi:/home/snb/my/apps/MyApp/bin$ sh skysql.sh
: not found2: 
Exception in thread "main" java.lang.NoClassDefFoundError: org/hsqldb/Server
Caused by: java.lang.ClassNotFoundException: org.hsqldb.Server

The most interesting thing, that when I run the script from the sh file directly from bash - it works! 最有趣的是,当我直接从bash从sh文件运行脚本时,它可以工作! But running the .sh file gives this error :( 但是运行.sh文件会出现此错误:(

Any help is very appreciated. 任何帮助都非常感谢。

EDIT 编辑

The following directories have all of the access rules, so no security issues could happen. 以下目录具有所有访问规则,因此不会发生安全问题。 They are all accessible. 它们都是可访问的。

EDIT #2 编辑#2

I have used the offered answers and comments and that's what happened: 我已经使用了提供的答案和评论,这就是发生的情况:

  1. Still same stuff :( 还是一样的东西:(

  2. My .sh file: 我的.sh文件:

     #!/bin/bash export HSQLDB_JAR="$(pwd)/hsqldb-2.2.8.jar" echo $HSQLDB_JAR java -classpath $HSQLDB_JAR org.hsqldb.Server -database.0 ../my-hsqldb/my-hsqldb -dbname.0 my-hsqldb 

The terminal output is: 终端输出为:

rpi@RaspberryPi:/home/snb/my/apps/MyApp/bin$ sh skysql.sh
: not found2: 
: not found4: 
/home/snb/my/apps/MyApp/lib/hsqldb-2.2.8.jar
: not found6: 
Exception in thread "main" java.lang.NoClassDefFoundError: org/hsqldb/Server
Caused by: java.lang.ClassNotFoundException: org.hsqldb.Server
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: org.hsqldb.Server.  Program will exit.
: not found8: 

So, as you can see, the .jar is actually found and it is in the right path. 因此,如您所见,.jar实际上已经找到并且在正确的路径中。

This is probably a bug in a part of the script that you don't show. 这可能是脚本中未显示的一部分中的错误。 Try this: 尝试这个:

  1. Make sure the first line of your script reads #!/bin/bash 确保脚本的第一行显示为#!/bin/bash

  2. Make sure you're in the folder that you think you are. 确保您位于自己认为的文件夹中。 Add

     if [ ! -e "$HSQLDB_JAR" ]; then echo "Jar not found!" ; exit 1 ; fi 

    before calling java 调用java之前

  3. To see what the shell actually executes, use -x (either add -x as parameter to the first line or activate it with the command set -x ). 要查看shell实际执行的操作,请使用-x (将-x作为参数添加到第一行,或使用命令set -x激活它)。

If you need to generate a path relative to your script, use this code: 如果需要生成相对于脚本的路径,请使用以下代码:

BASE=$( cd $( dirname "$0" ) > /dev/null && pwd )
HSQLDB_JAR="$BASE/../lib/hsqldb-2.2.8.jar"

Always quote variables which contain paths to make sure white space works properly. 始终引用包含路径的变量,以确保空格正常工作。

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

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