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.
Related
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
Eclipse has begun terminating any program I try to run before it does anything. The only output is 4.12 printed to the console. It's not just one program with errors, I've tried running old programs that I know worked before but they all exit with 4.12 being printed out.
I tried downloading a new version of eclipse (Mars.1 Release (4.5.1)) but it hasn't really done anything. In the old version it was exiting everything with the code 4.11 so I've run out of ideas to get this to work again.
EDIT
I'm on a mac, using 64-bit.
None of my programs run, I haven't just been leaving the main() empty.
I tried running from the command line and all my programs have changed type from .java to .class
The error I got in the command line was
Class names, 'HelloWorld', are only accepted if annotation processing is explicitly requested
The program running was just hello world.
Try to run your programs via command line, outside of Eclipse, just to make sure it's not an IDE configuration. If still your programs fail, try to re-install your JRE/JDK.
if still the problem occurs - provide us with the exact command line errors.
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?
I have this C# program i made and while i can run it fine by clicking the exe file or by clicking on a batch file, I cant start up the program on a java program I made to run it. I have tried this line of code and couldn't get the software to run.
Runtime.getRuntime().exec("nameOfTheExeFile");
or set it to the batch file i made that starts the program.
Runtime.getRuntime().exec("nameOfTheBatchFile");
Now the interesting thing is when I try it with the batch file i get an error saying that the file cannot be found but when i double click the batch file it will start the exe file just fine.
I have even tried to use Process but I am not getting any luck with that process as well
List cmdAndArgs = Arrays.asList(new String[]{"cmd.exe", "/c", "ProgramName.exe"});
ProcessBuilder pb = new ProcessBuilder(cmdAndArgs);
Process p = pb.start();
Strange thing is i dont get any error at all. Even when i try unit testing i don't any error's at all. Is there a process I am missing or something ? I am lost on what to do.
Update:
When i check on the task manager i can see that the program is running but not the exe version. I see ProgramName.vshost.exe , is there a reason for this to be showing and not the exe file ?
Since your program is command line program you need to start it from cmd. I'm not sure if this is the best way to do it, but it works.
Runtime.getRuntime().exec("cmd /c start nameOfTheBatchFile");
Batch file:
start cmd.exe /k "nameOfExeFile"
exit
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