LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Invoking java from a bash terminal via shell script fails under Mint Debian (https://www.linuxquestions.org/questions/programming-9/invoking-java-from-a-bash-terminal-via-shell-script-fails-under-mint-debian-863039/)

rizwanjavaid 02-16-2011 07:25 AM

Invoking java from a bash terminal via shell script fails under Mint Debian
 
I am running a Java application on the command line bash terminal under Mint Debian. I have JDK1.6.0_22 installed 64-bit, and the OS is 64-bit too. I have a few JAR files in the directory and a few native LWJGL libraries. When I run the application using the command line, all works fine.
Lets assume my directory where the files are is called /home/riz/MyGame. I change to that directory and this is the command I use:

Code:

java -classpath MyGame.jar:log4j-1.2.16.jar:jme/jme-colladabinding.jar:jme-audio.jar:jme-awt.jar:jme-collada.jar:jme-editors.jar:jme-effects.jar:jme-font.jar:jme-gamestates.jar:jme-model.jar:jme-ogrexml.jar:jme-scene.jar:jme-swt.jar:jme-terrain.jar:jme.jar:jogl/gluegen-rt.jar:jogl/jogl.jar:jorbis/jorbis-0.0.17.jar:junit/junit-4.1.jar:lwjgl/jinput.jar:lwjgl/lwjgl.jar:lwjgl/lwjgl_util.jar:lwjgl/lwjgl_util_applet.jar:swt/windows/swt.jar:jbullet/jbullet-jme.jar:jbullet/asm-all-3.1.jar:jbullet/jbullet.jar:jbullet/stack-alloc.jar:jbullet/vecmath.jar:trove-2.1.0.jar:sceneMonitor/jmejtree_jme2.jar:sceneMonitor/propertytable.jar:sceneMonitor/scenemonitor_jme2.jar:sceneMonitor/sm_properties_jme2.jar -Djava.library.path="lwjgl/native/linux" -Xmx1024m -Xms768m -ea com.mygame.Main
This works fine and the application starts up as expected. LWJGL native library is loaded in and works fine as expected. Note that I run this command on the bash terminal.

The problem occurs when I try to run this command via a shell script. Here is my script (located under /home/riz/MyGame):

Code:

#!/bin/bash

# Set the minimum and maximum heap sizes
MINIMUM_HEAP_SIZE=768m
MAXIMUM_HEAP_SIZE=1024m

if [ "$MYAPP_JAVA_HOME" = "" ] ; then
    MYAPP_JAVA_HOME=$JAVA_HOME
fi

_JAVA_EXEC="java"
if [ "$MYAPP_JAVA_HOME" != "" ] ; then
    _TMP="$MYAPP_JAVA_HOME/bin/java"
    if [ -f "$_TMP" ] ; then
        if [ -x "$_TMP" ] ; then
            _JAVA_EXEC="$_TMP"
        else
            echo "Warning: $_TMP is not executable"
        fi
    else
        echo "Warning: $_TMP does not exist"
    fi
fi

if ! which "$_JAVA_EXEC" >/dev/null ; then
    echo "Error: No Java environment found"
    exit 1
fi

_MYAPP_CLASSPATH="MyGame.jar:log4j-1.2.16.jar:jme/jme-colladabinding.jar:jme-audio.jar:jme-awt.jar:jme-collada.jar:jme-editors.jar:jme-effects.jar:jme-font.jar:jme-gamestates.jar:jme-model.jar:jme-ogrexml.jar:jme-scene.jar:jme-swt.jar:jme-terrain.jar:jme.jar:jogl/gluegen-rt.jar:jogl/jogl.jar:jorbis/jorbis-0.0.17.jar:junit/junit-4.1.jar:lwjgl/jinput.jar:lwjgl/lwjgl.jar:lwjgl/lwjgl_util.jar:lwjgl/lwjgl_util_applet.jar:swt/windows/swt.jar:jbullet/jbullet-jme.jar:jbullet/asm-all-3.1.jar:jbullet/jbullet.jar:jbullet/stack-alloc.jar:jbullet/vecmath.jar:trove-2.1.0.jar:sceneMonitor/jmejtree_jme2.jar:sceneMonitor/propertytable.jar:sceneMonitor/scenemonitor_jme2.jar:sceneMonitor/sm_properties_jme2.jar"

_VM_PROPERTIES='-Djava.library.path="lwjgl/native/linux"'

_MYAPP_MAIN_CLASS="com.mygame.Main"

$_JAVA_EXEC -classpath $_MYAPP_CLASSPATH $_VM_PROPERTIES -Xmx${MAXIMUM_HEAP_SIZE} -Xms${MINIMUM_HEAP_SIZE} -ea $_MYAPP_MAIN_CLASS

The shell script is in the same directory as the JAR files (the same directory where I ran the Java command above). When I execute the shell script ( sh MyGame.sh ), I get the UnsatisfiedLinkError message:

Code:

    14-Feb-2011 19:46:28 com.wcg.game.DefaultUncaughtExceptionHandler uncaughtException
    SEVERE: Main game loop broken by uncaught exception
    java.lang.UnsatisfiedLinkError: no lwjgl in java.library.path
      at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1734)
      at java.lang.Runtime.loadLibrary0(Runtime.java:823)
      at java.lang.System.loadLibrary(System.java:1028)
      at org.lwjgl.Sys$1.run(Sys.java:73)
      at java.security.AccessController.doPrivileged(Native Method)
      at org.lwjgl.Sys.doLoadLibrary(Sys.java:66)
      at org.lwjgl.Sys.loadLibrary(Sys.java:82)
      at org.lwjgl.Sys.<clinit>(Sys.java:99)
      at org.lwjgl.opengl.Display.<clinit>(Display.java:130)
      at com.jme.system.lwjgl.LWJGLDisplaySystem.setTitle(LWJGLDisplaySystem.java:118)
      at com.wcg.game.WcgStandardGame.initSystem(WcgStandardGame.java:287)
      at com.wcg.game.WcgStandardGame.run(WcgStandardGame.java:185)
      at java.lang.Thread.run(Thread.java:662)

I don't understand what I am doing wrong. I am executing the exact same command via a shell script and it is not working. I have echoed the Java command I build in the shell script and it is identical to the one I type in on the bash terminal that is working. Any ideas, solutions, most welcome. Is there a special way to load libraries when using a shell for Java?

I am running Linux Mint Debian 201012, Linux mint 2.6.32-5-amd64 #1 SMP Thu Nov 25 18:02:11 UTC 2010 x86_64 GNU/Linux. JDK is 1.6.0_22 64-bit. I have 64-bit .so files in the correct place too as the Java command works. Also, file permissions are correct.

I have also tried the absolute file path: -Djava.library.path="/home/riz/MyGame/lwjgl/native/linux" and this does not work either, same error trace.

There must be something obvious I am missing. I call the shell script from bash like this:

Code:

sh MyGame.sh
No need for root privilege.

Any tips, ideas most welcome.

Thanks
Riz

pgroover 02-16-2011 08:12 AM

Have you tried changing to the directory it's located at when the script starts? It's been awhile since I've worked in Linux, but I believe a shell started from a script starts off in the user's home directory. That may be the problem, sorry if I'm wrong but like I said, it's been awhile and sometimes it seems like I'm starting all over.

knudfl 02-16-2011 08:30 AM

I'd guess, the shell script should start with :

Line 1 : #!/bin/bash
Line 2 : cd /home/riz/MyGame

rizwanjavaid 02-16-2011 01:36 PM

Thanks for all your suggestions. I have solved the problem. The line:

Code:

_VM_PROPERTIES='-Djava.library.path="lwjgl/native/linux"'
should be:

Code:

_VM_PROPERTIES=-Djava.library.path="lwjgl/native/linux"
(no single quotes) as later on when I invoke the command, it does not deal with the quotes in the same way and fails even though echoing the command looks right.

Thanks again
Riz


All times are GMT -5. The time now is 06:25 AM.