我目前正在尝试从bash脚本中运行我的应用程序。当我从控制台运行java应用程序时,它工作正常。我已经编译了java应用程序,并在脚本中尝试了多个不同的命令,并且无法弄明白。我是一个相当新的脚本,所以我猜它与语法有关。我也在这里阅读了多个不同的帖子,但似乎没有任何效果。
这是我的脚本,我将其作为sh endpoint.sh
运行#!/bin/bash
CLASSPATH=json-simple-1.1.jar:log4j-1.2.17.jar:.
result=$(java -cp $CLASSPATH com/api/endpoint/MyApp $INI)
echo $result
我得到的错误如下
Exception in thread "Main Thread" java.lang.NoClassDefFoundError: com/api/endpoint/MyApp
Caused by: java.lang.ClassNotFoundException: com.api.endpoint.MyApp
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:305)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:246)
Could not find the main class: com/api/endpoint/MyApp. Program will exit.
应用程序位于com.api.endpoint包中,位于该目录中。我有一种感觉,这是一种我很容易忽视的事情,但我现在已经在这几天苦苦挣扎了。有什么想法吗?
完整脚本:
#!/bin/bash
# Set the search path for the shell to be the standard places.
PATH=setting the path here; export PATH
# Config file
INI=config.ini
# Log function
function log()
{
timestamp=`date +"%s"`
echo "$timestamp:$1">>$ERROR_LOG_FILE
}
# Read config file
if [ -f $INI ]
then
source $INI
else
exit 1 # terminate the script and return error
fi
# create error log file
echo -n "" > $ERROR_LOG_FILE
# prepare the WLST env to execute api list python script
source $WLST_SCRIPT
# call list.py
result=$(java wb.WLST $WLST_PYTHON_SCRIPT)
if [[ $result == *Exception* ]]
then
log "$result"
exit 1 # terminate the script with error code
fi
# prepare the java env to execute region inventory java app
PATH="/tmp/:$PATH"; export PATH
#echo $PATH
CLASSPATH=json-simple-1.1.jar:log4j-1.2.17.jar:.
result=$(java -cp $CLASSPATH com/api/endpoint/MyApp $INI)
echo $result
答案 0 :(得分:0)
包以句点(.
)分隔,而不是斜杠(/
),因此:
#!/bin/bash
CLASSPATH=json-simple-1.1.jar:log4j-1.2.17.jar:.
# Note the classname:
result=$(java -cp $CLASSPATH com.api.endpoint.MyApp $INI)
echo $result