B4J Question Shell not running jar file

aaronk

Well-Known Member
Licensed User
Longtime User
Hi,

I am using an non-UI app and trying to run a .jar file with shell.

When I use command line and I use the following it works:

/opt/jdk-14/bin/java -jar /opt/CloudServer/tool.jar Args1 Args2 Args3 Args4

However, when I use the following code in my B4J app it comes up with an error..
(not 100% sure if this is the way in doing this)

B4X:
    Dim shl As Shell    'jShell Lib v1.52
    Dim Args As List
    Args.Initialize
    Args.Add("-jar")
    Args.Add("tool.jar")
    Args.Add("Args1")
    Args.Add("Args2")
    Args.Add("Args3")
    Args.Add("Args4")
    
    shl.InitializeDoNotHandleQuotes("shl","/opt/jdk-14/bin/java", Args)
    shl.WorkingDirectory = "/opt/CloudServer"
    shl.Run(10000)

Wait for shl_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
    Log(Success & "; ExitCode = " & ExitCode)
    Log("StdOut = " &  StdOut)
    Log("StdErr = " &  StdErr)

The error it shows is:

false; ExitCode = -559038737
StdOut =
StdErr = org.apache.commons.exec.ExecuteException: Execution failed (Exit value: -559038737. Caused by java.io.IOException: Cannot run program "/opt/jdk-14/bin/java" (in directory "/opt/CloudServer"): error=13, Permission denied)

I can't work out why it's not working in B4J when it works fine in command line.

Any one know why this doesn't work ?
 

aeric

Expert
Licensed User
Longtime User
Maybe try with array instead of a list.

The error shows permission error. Maybe you need to add sudo to your command.

I think better move or copy the tool.jar to /home/yourusername/YourDirectory
 
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Maybe try with array instead of a list.
the error tells that he has no permission to run /opt/jdk-14/bin/java

Caused by java.io.IOException: Cannot run program "/opt/jdk-14/bin/java" (in directory "/opt/CloudServer"): error=13, Permission denied)

I´m not a linux pro but it must have to do with userrights maybe.
 
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime User
Maybe try with array instead of a list.
I tried that, but had the same result. I also added sudo as well.


the error tells that he has no permission to run /opt/jdk-14/bin/java
Yeah, that is what I thought but I can't work out why since it works fine when running it in command line. I even ran my B4J app as sudo so it should give it full access from what I can work out.

Perhaps you already have an instance of the same project active in that directory?
I changed the package name to something else but still got the same result.

If I run it on my Windows machine (as per below) it works, but can't get it to work on Linux (Ubuntu).

B4X:
Dim shl As Shell    'jShell Lib v1.52
        Dim Args As List
        Args.Initialize
        Args.Add("-jar")
        Args.Add("tool.jar")
        Args.Add("Args1")
        Args.Add("Args2")
        Args.Add("Args3")
        Args.Add("Args4")
        
        shl.InitializeDoNotHandleQuotes("shl", "java", Args)
        shl.WorkingDirectory = File.DirApp
        shl.Run(10000)
        
    Wait for shl_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)

    Log(Success & "; ExitCode = " & ExitCode)
    Log("StdOut = " &  StdOut)
    Log("StdErr = " &  StdErr)
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Are you using some libraries like jSerial?

 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Are you trying to write a file in the /opt/xxxx directory?
Unless you use "sudo", you may not have write permission.
Use /home directory and you don't need sudo permission.

VirtualBox_Debian 12_09_05_2024_17_00_16.png
VirtualBox_Debian 12_09_05_2024_17_01_22.png
 

Attachments

  • CloudServer.zip
    1.1 KB · Views: 9
  • tool.zip
    747 bytes · Views: 14
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime User
Have you tried this?
Yes, I have tried that.

The CloudServer.jar file runs, but when it tries to run the code to call the other jar file that is when this error shows.

/opt/jdk-14/bin/java -jar CloudServer.jar

Error Shows:
false; ExitCode = -559038737
StdOut =
StdErr = org.apache.commons.exec.ExecuteException: Execution failed (Exit value: -559038737. Caused by java.io.IOException: Cannot run program "java" (in directory "/home/CloudServer"): error=13, Permission denied)
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
How do you install Jdk 14?

Here is how I install:

B4X:
wget https://www.b4x.com/b4j/files/java/linux_jdk-14.0.1.zip
sudo mkdir /usr/java
sudo cp linux_jdk-14.0.1.zip /usr/java
cd /usr/java/
sudo unzip linux_jdk-14.0.1.zip
sudo update-alternatives --install /usr/bin/java java /usr/java/jdk-14.0.1/bin/java 0
sudo update-alternatives --install /usr/bin/javac javac /usr/java/jdk-14.0.1/bin/javac 0
 
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime User
It thinks I have a different version of Java installed.

I used to use Java8 but I upgraded to Java14 a year or so ago, and I copied Java14 into the directory and now I need to call it with the file path.

java -jar CloudServer.jar
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.UnsupportedClassVersionError: b4j/cloud/server/main has been compiled by a more recent version of the Java Runtime (class file version 53.0), this version of the Java Runtime only recognizes class file versions up to 52.0
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:756)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:473)
at java.net.URLClassLoader.access$100(URLClassLoader.java:74)
at java.net.URLClassLoader$1.run(URLClassLoader.java:369)
at java.net.URLClassLoader$1.run(URLClassLoader.java:363)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:362)
at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:621)

java -version
openjdk version "1.8.0_362"
OpenJDK Runtime Environment (build 1.8.0_362-8u372-ga~us1-0ubuntu1~18.04-b09)
OpenJDK 64-Bit Server VM (build 25.362-b09, mixed mode)
 
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime User
It only shows I have one version installed..

sudo update-alternatives --config java
There is only one alternative in link group java (providing /usr/bin/java): /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java
Nothing to configure.
I manually copied the Java14 folder from my computer to /opt/jdk-14 on the linux server using SSH.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
It only shows I have one version installed..


I manually copied the Java14 folder from my computer to /opt/jdk-14 on the linux server using SSH.
You need to install using the commands:
B4X:
sudo update-alternatives --install /usr/bin/java java /opt/jdk-14/bin/java 0
sudo update-alternatives --install /usr/bin/javac javac /opt/jdk-14/bin/javac 0
 
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime User
I just ran those 2 commands.
I then ran: sudo update-alternatives --config java
Set my Java version.

Now when I check the Java version it shows the correct Java version.
java -version
openjdk version "14" 2020-03-17
OpenJDK Runtime Environment (build 14+36-1461)
OpenJDK 64-Bit Server VM (build 14+36-1461, mixed mode, sharing)

But still got the Permission denied issue. (I tried running it as sudo but got the same result)
/home/CloudServer# java -jar CloudServer.jar
Calling tool.jar...
false; ExitCode = -559038737
StdOut =
StdErr = org.apache.commons.exec.ExecuteException: Execution failed (Exit value: -559038737. Caused by java.io.IOException: Cannot run program "java" (in directory "/home/CloudServer"): error=13, Permission denied)
 
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime User
I just worked it out.

Looks like it might have been a permission thing.

I ran the following command and it fixed the issue.

chmod -R o+rx /opt/

Since Java was in that directory, it looks like I had to set the permissions for the whole directory and I must have not had it set in the past.

Thanks heaps for your help!
 
Upvote 0
Top