I'm trying to use Process and ProcessBuilder to execute a ps1 file through powershell. I'm struggling with the entire thing and cant even get the powershell.exe to run. Ive tried :
// Wont Run
// Defined specific path for powershell - trying to simply run the exe file
ProcessBuilder pb = new ProcessBuilder("C:\\Windows\\system32\\WindowsPowerShell\\v1.0\\powershell.exe");
Process p = pb.start();
I've look at Running Powershell from Java and Run a powershell on a remote machine using JAVA trying to implement methods from these examples but I dont understand why mine wont even run an instance of powershell.
I do eventually want to incorporate running ps1 files through here but I need to walk before I can run.
Also, I have checked Task Manager and it IS running as a process.
Could anyone point me in the right direction as to why this wont even run ?
Thanks in advance
Related
So I have a Docker network that has a Docker file with a bunch of information. I have a java program that is going to bring up the enviorment and then produce several commands to run within this enviorment. To be clear, the first command I need to run is NOT inside the Docker enviorment. I am having some challenges with the Process and Runtime classes.
First, say I wanted my java program to launch a new gnome terminal and then run a command to get into the docker network. I have this command,
Process process = Runtime.getRuntime().exec(new String[]{"gnome-terminal"});
Gnome terminal sucessfully comes up but any additional arguments I give in this array are just ignored. For example,
Process process = Runtime.getRuntime().exec(new String[]{"gnome-terminal","ls"});
Does not work. The command I ultimatly want to run would look something like this,
Process process = Runtime.getRuntime().exec(new String[]{"gnome-terminal","sudo","docker","exec","-it","sawtooth-shell-default", "bash"});
Second, Once I have this running, will additional commmands I run work within the Docker enviorment? I have a python file with a Stream handler that specifies the correct commands to run.
Other documentation on related issues was limited.
I made sure my code was wrapped in a runtime exception try catch and that I was running the correct .class file. Any help on this would be great!
Edit: I have also tried to run this in another linux terminal like Hyper and Tilda
I also am able to get a sudo sign in when I run the command like so,
Process process = Runtime.getRuntime().exec(new String[]{"gnome-terminal","--","sudo","docker","exec","-it","sawtooth-shell-default", "bash"});
However it closes immediatly after authorizing.
Okay this is what I was attempting to do.
https://www.atlassian.com/blog/software-teams/deploy-java-apps-with-docker-awesome
This site is outdated and I had to use this link for getting that latest version of the java PPA.
This process basically installs java into the docker contatiner so that I can run a java program that uses Runtime.
I'm trying to do something very simple here, but it isn't working.
Basically I have a program and an input file sitting at a certain directory, let's call it "programDir".
I'm writing a plugin in Eclipse that will call this program and run it on the input file.
Essentially two steps must be done: 1) cd to programDir 2) run the program by calling "idp input.txt"
I have done this manually in the cmd and it works as expected. However in Java I can't get it to work. I tried 2 approaches:
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("cmd.exe /c cd \""+programDir+"\" & \"idp input.txt\"");
Here I get the following from the errorStream: "idp output.txt" is not recognized as an intern or extern command, program or batch file
I'm not sure why. I have left out the second part of the command to ensure that I am in the correct location. When i add "start", a console window pops up and it is in the programDir folder. If I then manually type "idp input.txt" I get the expected behavior.
Second approach:
I also used ProcessBuilder after some googling on the subject. I tried this piece of simple code:
String[] command = {"CMD", "/C", "idp input.txt"};
ProcessBuilder pb = new ProcessBuilder(command);
pb.directory(new File(programDir));
Process proc = pb.start();
Now nothing happens and the program does not terminate.
Again, if I add "start" as one of the parameters in "command", a console window pops up in the right location and if I then manually type "idp input.txt", it works. So I have no idea why the code doesn't work.
One interesting thing: the idp.bat file calls a kbs.exe process. When I run the second piece of code, no kbs.exe appears in my task manager. However, as soon as I terminate the program, it briefly makes an appearance. Does that mean my code gets stuck in a loop somewhere or something?
Any help appreciated!
I'm currently developing a little software in Java and I'm facing a problem I'm not able to solve. In a few words, I am on ArchLinux and I need to run "makepkg" in a specific directory. Of course I tried with
Runtime.getRuntime().exec("cd foo && makepkg");
But I discovered that I cannot cd in directories. Someone has an idea on how to do this? Thanks anyway
A process executor is not a shell. It's done for launching a process.
A thing that can help you is to launch the process from a specified directory.
You can create a ProcessBuilder instance and set the working directory.
It is my way of doing.
ProcessBuilder pb = new ProcessBuilder("makepkg");
pb.directory(new File("foo"));
final Process process = pb.start();
// then you read the flow with process.getInputStream() for example
I've been having real problems trying to get a ruby script to run through Java. I've had all kinds of solutions proposed, and all of them are failing for some reason, so I'm trying to simplify my problem.
Let's say I have a shell script that just has this line in it:
ruby -rubygems script/test_s2t.rb
At the terminal, I can run this script using script/runruby.sh and it works as expected. Now let's say I have a Java method that does the following:
String[] cmd = {"script/runruby.sh"};
ProcessBuilder builder = new ProcessBuilder(cmd);
builder.redirectErrorStream(true);
Process process = builder.start();
This doesn't work (it throws an error back from the Ruby script, specifically, but this is a misdirect because it's really down to the script itself not working as expected). My question is not why that test_s2t.rb script doesn't work, because I think that might be distracting me from the real problem.
My question is simply what is different when I run something through ProcessBuilder as opposed to just running it via the command line. Is it a permissions thing? Path differences? There must be something screwing around with the environment the script runs in, because I can't see a problem with the script itself.
As alwyas, any suggestions appreciated. Three days and counting on this issue...
EDIT - For those curious, the exact error I receive in Java is the one described at the bottom of this question: Java receives an error executing Ruby script; Terminal doesn't
The outcome we got in that question was that I should try JRuby, but that resulted in further problems as I can't get the gems to work properly within JRuby. So I went back to asking myself why it wouldn't run normally in the first place.
The reason I think the error is a distraction is because the error is given simply because it processed a string it wasn't expecting to see. The string it expects is the normal process the script runs, which is using ffmpeg and suchlike. What this means is that the script encountered another error (which it isn't showing, which means it was probably not caused by ruby/jruby but by the processes the script launches like ffmpeg).
It's incredibly frustrating, purely because it runs so perfectly from the command line.
I've run into similar problems and there are two things that seem to be common problems:
1) The environment of the child process will be the same as environment of the current virtual machine. This includes the working directory of the launched process.
Example from: http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/ProcessBuilder.html
Map<String, String> env = pb.environment();
env.put("VAR1", "myValue");
env.remove("OTHERVAR");
env.put("VAR2", env.get("VAR1") + "suffix");
pb.directory("myDir");
Alternatively, you could set the environment inside the shell script.
2) Do you have the proper shebang #! at the beginning of the .sh file? Personally I'd make it absolutely clear and perhaps explicitly call bash or zsh or whatever with the path to the shell script as the first argument OR directly call ruby with the '-rubygems' and 'script/test_s2t.rb' as arguments.
Good Luck!
I have an executable file (ffmpeg) that I'm trying to run with a Java program on a Mac. I used the Java program to send the command chmod 777 /path/to/ffmpeg, but when I try to run ffmpeg, I get the following error:
java.io.IOException: Cannot run program "/Users/james/WalkTheHall/ffmpeg": error=13, Permission denied
But when I run chmod 777 /path/to/ffmpeg from Terminal on my own before opening the Java application, the command to ffmpeg will run just fine in the Java program.
Is there a difference between calling chmod from within the Java program and calling it on my own? Why will it not work? Thank you!
I just had the same problem in my code.
i solved this by add waitFor after exec. The "chmod" process is not finished when next command is executed. the code may look like:
p = Runtime.getRuntime.exec("chmod 777 xxx");
p.waitFor();
Runtime.getRuntime.exec("./xxx");
I'd guess that chmod is a shell command, not an executable. Try running chmod through your shell. See more details here: Want to invoke a linux shell command from Java
Yes, there is a difference. When you run the command from the terminal, it is you who is performing the action, and thus it is performed using your credentials. The Java application is running the command using the Java application's permissions. This is to prevent an application from running and then making dangerous, unwanted changes to the file system. Perhaps someone else can elaborate and give guidance to a workaround for this.
I am currently working on a project that also makes use of FFMpeg on OSX. I store FFMpeg in the JAR and extract it and set executable on use as you seem to be doing. This is what I do, and it seems to work.
public static void setExecutable(File file, boolean executable)
{
Process p = Runtime.getRuntime().exec(new String[] {
"chmod",
"u"+(executable?'+':'-')+"x",
file.getAbsolutePath(),
});
// do stuff to make sure p finishes & capture output
}
The code is GPL, so feel free to check it out. Its not the nicest codebase, and even the FFMpeg stuff is perhaps overly complex, but it works.
Source is viewable at http://korsakow.net
These two files in particular might be interesting for you
FFMpegEncoderOSX.java
FileUtil.java
Try this:
File commandFile = new File("myFile.txt");
commandFile.setExecutable(true);
Process p = Runtime.getRuntime.exec(commandFile.getAbsoluteFile());
to start an program on OSX you need this:
Runtime.getRuntime().exec("chmod 777 "+path); //in order to execute it
Runtime.getRuntime().exec(path); //execute it
Runtime.getRuntime().exec("chmod 744 "+path); //undo every change
path should be the path to the exc of the program, for example:
AppStore -> Applications/App\ Store.app/Contents/MacOS/App\ Store