运行shell脚本时出现NoClassDefFoundError

时间:2010-08-22 05:14:43

标签: java shell nstask

这是错误:

Exception in thread "main" java.lang.NoClassDefFoundError: org/alicebot/server/net/AliceServer
Caused by: java.lang.ClassNotFoundException: org.alicebot.server.net.AliceServer
    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:307)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:248)

这是server.sh文件:

#!/bin/sh
echo Starting Jarvis Program D.
cd "`dirname \"$0\"`"
export ALICE_HOME=.
export SERVLET_LIB=lib/servlet.jar
export ALICE_LIB=lib/aliceserver.jar
export JS_LIB=lib/js.jar

# Set SQL_LIB to the location of your database driver.
export SQL_LIB=lib/mysql_comp.jar

# These are for Jetty; you will want to change these if you are using a different http server.
export HTTP_SERVER_LIBS=lib/org.mortbay.jetty.jar

export PROGRAMD_CLASSPATH=$SERVLET_LIB:$ALICE_LIB:$JS_LIB:$SQL_LIB:$HTTP_SERVER_LIBS
exec java -classpath $PROGRAMD_CLASSPATH -Xms64m -Xmx128m org.alicebot.server.net.AliceServer $1

这是NSTask代码:

NSString *shstring = [NSString stringWithFormat:
                          @"%@ %@ %@", 
                          @"source", 
                          @"server.sh", 
                          @"| /usr/bin/sed '/Jarvis>/q'"
                          ];

NSTask *sh = [NSTask new];
    NSPipe *outputPipe = [NSPipe new];

    [sh setLaunchPath:@"/bin/sh"];
    [sh setArguments:[NSArray arrayWithObjects: @"-c", shstring, nil ]];
    [sh setCurrentDirectoryPath:@"/applications/jarvis/brain"];
    [sh setStandardInput:[NSPipe new]];
    [sh setStandardError:outputPipe];
    [sh setStandardOutput:outputPipe];
    [sh launch];

    NSMutableString *outputString = [NSMutableString string];
    while ([outputString rangeOfString:@"Jarvis>"].location == NSNotFound) {
        [outputString appendString:[[[NSString alloc] initWithData:[[outputPipe fileHandleForReading] readDataToEndOfFile] encoding:NSUTF8StringEncoding] autorelease]];
        NSRunAlertPanel(@"", outputString, @"", @"", @"");
    }

当我通过NSTask运行此脚本并检查错误管道时,会出现该错误并仅打印回显。

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

看起来工作目录并不像你期望的那样。您正在classpath参数中定义相对路径。它们与当前工作目录相关。可能当前的工作目录不知道lib/aliceserver.jar文件。执行pwd以了解当前的工作目录。

答案 1 :(得分:0)

用server.sh中的绝对路径替换相对,然后使用完整路径运行server.sh(如果server.sh有shebang行,请不要在NSTask代码中获取server.sh,并始终检查cd命令的退出代码)。

这是我的server.sh文件:

#!/bin/sh
# cat ~/Desktop/charliebot/server.sh

# code from: http://sourceforge.net/projects/charliebot/

# also adapt the following files if necessary:
# ~/Desktop/charliebot/console.sh
# ~/Desktop/charliebot/targeting.sh
# ~/Desktop/charliebot/tester.sh

echo Starting Alicebot Program D.
ALICE_HOME="${HOME}/Desktop/charliebot"

cd "${ALICE_HOME}" || { echo "Could not cd to: ${ALICE_HOME}"; exit 1; }

SERVLET_LIB="${ALICE_HOME}/lib/servlet.jar"
ALICE_LIB="${ALICE_HOME}/lib/aliceserver.jar"
JS_LIB="${ALICE_HOME}/lib/js.jar"

# Set SQL_LIB to the location of your database driver.
SQL_LIB="${ALICE_HOME}/lib/mysql_comp.jar"

# These are for Jetty; you will want to change these if you are using a different http server.
HTTP_SERVER_LIBS="${ALICE_HOME}/lib/org.mortbay.jetty.jar"

PROGRAMD_CLASSPATH="${SERVLET_LIB}:${ALICE_LIB}:${JS_LIB}:${SQL_LIB}:${HTTP_SERVER_LIBS}"

export ALICE_HOME SERVLET_LIB ALICE_LIB JS_LIB HTTP_SERVER_LIBS PROGRAMD_CLASSPATH

#printf "\n\n\nPROGRAMD_CLASSPATH\n%s\n\n\n"  "$PROGRAMD_CLASSPATH" | tr ':' '\n'

exec java -classpath "$PROGRAMD_CLASSPATH" -Xms64m -Xmx128m org.alicebot.server.net.AliceServer $1

这对我来说没有丝毫的打嗝。

# in Terminal.app
~/Desktop/charliebot/server.sh

我认为对server.sh的参数$ 1是强制性的。如果是这种情况,server.sh(即exec java -classpath ... $ 1)会抱怨它。但是,很高兴知道什么样的参数可以作为$ 1传递给server.sh(...阅读man 1 java ...)!

当然,所有这些都不会在循环死锁(NSNotFound)时删除Objective-C。您必须实现异步stdout管道读取机制而不是while循环。 (例如,参见Apple关于这种方法的异步操作的“Dashboard Programming Topics”。)