Run Java from Java on Linux - java

I am writing a program that updates it's self. I get all the updating working, I just need the program to restart it's self. Here is my current code:
String javaPath = System.getProperty("java.home");
Process process = new ProcessBuilder(javaPath, "-jar", ClientPortal.class.getProtectionDomain().getCodeSource().getLocation().getPath()).start();
I get an error though:
java.io.IOException: Cannot run program "/usr/lib/jvm/java-8-oracle/jre": error=13, Permission denied
I tried running this in my own console, and it is denied. The reason I am using the path of Java instead of just using the command java, is because, like me, people might have multiple Java instances installed and the wrong one is run. The command might simply be not available.
How can I run the same JVM in this fashion?

Related

Trying to run swiftlint through Java subprocess fails (works normally through command line)

I have a java program to run different kinds of linters, calling the linters as a process using ProcessBuilder. So far, other linters have worked for the most part but with swiftlint, i'm facing a strange issue
When I run it normally from the command line, with
swiftlint --enable-all-rules
It works perfectly, but executing the same through my Java subprocess utility
ProcessBuilder processBuilder = new ProcessBuilder("swfitlint", "--enable-all-rules");
Process process = processBuilder.start();
It fails with the following error message
SourceKittenFramework/library_wrapper.swift:31: Fatal error: Loading libsourcekitdInProc.so failed
At first glance it seems like for some reason certain libraries are not available to the java program which are available to when firing a command through bash, but just to be
sure, I tried running the following command through my subprocess util
swiftlint --version
swift --version
Both of which worked, meaning Java does have access to swift binaries installed on my machine. Any help would be appreciated.
I'm not still not entirely sure why this happens but i believe it's due to some kind of environment issue on my machine itself.
I tried running the same code inside of a docker container that uses the image built from the following base images
FROM openjdk:17-slim as java_base
FROM ghcr.io/realm/swiftlint:latest
COPY --from=java_base /usr/local/openjdk-17 /usr/local/openjdk-17
and it just worked

Diffrence of command line typing and Runtime.getRuntime().exec() on Windows

I want to try to e2fsck on Windows with Cygwin exe and dll files.
Fortunately, it works fine when I try to execute it with Windows command line(such as "e2fsck.exe -f test.img").
However, I failed to execute it with JAVA API, Runtime.getRuntime().exec("e2fsck.exe -f test.img").
Exit value of process is 8, and it means operational error of e2fsck. The exit value seems to be returned when e2fsck can not access target file, or target file is invalid.
I found the below things :
It's not caused by diffrence of string handling way of cmd.exe and exec().
Any other cmds on API call, such as ...exec("echo test! >> test.img"), work fine.
It means that exec() API put the right file path to e2fsck or any other processes.
It's not caused by permission deny. As I mented above, test.img is writable by exec().
ProcessBuilder could not solve this problem.
I couldn't find actual reason of my problem.
Did I miss somthing else?

Error when starting Datomic shell: java.lang.NoClassDefFoundError: jline/ConsoleRunner

I followed the instructions on the Datomic site: http://docs.datomic.com/getting-started.html, but I'm getting this error when trying to start up the datomic shell prompt. I'm using a windows machine. Any suggestions? I tried the same thing on my linux box and did not get this error.
Edit: moved to a different windows machine and it's working. If I have time to troubleshoot this problem and I find a solution I'll report back
I noticed that you cannot run the shell.cmd from within the bin directory, you need to call it with bin\shell.cmd from the parent directory... hope that helps.
In case you are using cygwin/bash and call bin/shell :
The java runtime on windows does not understand classpath with a ":"
but this is what you get from bin/classpath.
Either correct this or use DOS-CMD shell and call bin/shell.cmd inside.
Regards
Some tips for running datomic on Windows (7 at least):
Do not download datomic into Program Files. On startup, it creates logging directories and temp files into its own directories, so unless you run the command prompt as Administrator, you're gonna have screens full of Unable to write to file... errors.
You need to run datomic as such (assuming you extracted the download to C:)
c:\datomic-free-0.x.xxxx>bin\shell.cmd
Note the backslash. Tripped me up forever coming from *nix world.
After that, return to your regularly scheduled datomic tutorials.

Starting an external Java program from a Java program

This question has been asked before but with no real answer.
I wan't to start a Java Progam form another Java Program.
In my case I wan't to start the same program(2) and then exit the original program(1) while the clone is still open.
Unfortunatly I can't get this to work with ProcessBuilder
ProcessBuilder processBuilder = new ProcessBuilder("java","Programm");
Process process = processBuilder.start();
No error message, nothing happens.
I also can't run the program from the CMD in windows.
javac shows several errors during compilation:
symbol not found, ...
I can start the program normally from Eclipse.
You probably aren't seeing the errors from your Process because they are sent to its own error stream. You can access the error stream using process.getErrorStream().
I suspect the problem is that your classpath isn't properly set when invoking the java executable and it is failing to find your class or its dependencies, but it is hard to tell without seeing the error.

Powershell process hanging when called from Java application

I am trying to write a simple application that takes in a command line arguement (which will be a Powershell ps1 file) and then run it. So I have experemented with a number of different approaches and seem to be running into a problem. If I attempt to invoke powershell from within java, the windows process is started and is visible via process explorer, however powershell never returns, it hangs in some sort of loop by the looks of it. The command I am using is:
String command = "powershell -noprofile -noninteractive \"&C:\\new\\tst.ps1\"";
The command is then executed using:
Runtime systemRuntime = Runtime.getRuntime();
Process proc = systemRuntime.exec(command);
At the moment I am hard coding the location to the ps1 file as I was trying to rule this out as an issue. Using a process explorer I can see the hanging powershell process and the command that was passed to it was :
powershell -noprofile -noninteractive "&C:\new\tst.ps1"
which when copied into a cmd window, works to launch the tst.ps1 file. The file itself is incredibly simple in this example and I think I can rule it out being the cause of the freeze as I have tried to launch other ps1 files the same behaviour can be seen.
To further add to the confusion, if I use the java code posted above and pass in powershell commands instead of a file name then it successfully runs.
I've scoured the web and see lots of people experiencing the same issue but no one seems to have posted there solution, I hope its a simple oversight on my part and can be easily fixed.
Any hints/tips are appreciated :D
Alan
You have to close OutputStream in order for Powershell to exit.
Runtime systemRuntime = Runtime.getRuntime();
Process proc = systemRuntime.exec(command);
proc.getOutputStream().close();
Is your external program writing to the standard outputs (err and out)?
If yes, it can hang waiting for you to consume them from the java parent process.
You can get those as InputStreams by calling
Process.getInputStream()
and
Process.getErrorStream()
There's more details here:
Javadoc for Process

Categories