How to cd and execute an executable in ubuntu through java program - java

Hello and excuse me for being new to java coding, but what I am trying to do is a java program that calls an executable program with some given parameters in ubuntu. I've found the code above in another stackoverflow question:
ProcessBuilder pb = new ProcessBuilder();
pb.command("bash", "-c", "./runCalculator.sh");
Process process = pb.start();
int retValue = process.waitFor();
But how can I cd to the executable file first and then execute the program, displaying its output, through java?
Thank you.

You have 2 options:
Use absolute path
pb.command("bash", "-c", "/path/to/runCalculator.sh");
Use ProcessBuilder directory method:
pb.directory(new File("/path/to"));

You don't have to cd anywhere, just specify the absolute path.
String path = "/home/Omen/runCalculator.sh";
pb.command("bash", "-c", path);

Related

Starting a java GUI program via .cmd file from java code

I'm trying to start a java GUI program in windows via java code as below :
ProcessBuilder builder = new ProcessBuilder(Arrays.asList(new String[] {"cmd.exe", "/C",
"C:\\path\\to\\program\\program.cmd"}));
try {
builder.start();
} catch (IOException e) {
e.printStackTrace();
}
The cmd file starts the program with a "start javaw .." command with ... -cp program.jar -jar program.jar . When using the java code above, it throws an error that the program.jar is not found :
I also tried with the below, using cd first :
{"cmd.exe", "/C", "cd C:\\path\\to\\program\\ && program.cmd"}));
But the above does nothing at all.
Content of .cmd :
setlocal
SET JAVAHOME=..\java
SET PATH=%JAVAHOME%\jre\bin;%JAVAHOME%\jre\bin\client;%JAVAHOME%\bin;%PATH%
SET PATH=%PATH%;bin\
SET POLICY=java.policy
SET JAR_BOOT=program.jar
SET CONFIG_FILE=program.xml
IF EXIST jar\%JAR_BOOT% copy jar\%JAR_BOOT% . >NUL
start javaw -Xbootclasspath/p:jar/xercesImpl-2.9.1.jar;jar/xml-apis-1.3.04.jar;jar/xalan-2.7.1m1.jar;jar/serializer-2.7.1m.jar -Xmx1024M -XX:MaxPermSize=200M -cp %JAR_BOOT% -Dsun.java2d.noddraw=true -DJINTEGRA_NATIVE_MODE -Djava.security.policy=%POLICY% -jar %JAR_BOOT%
title Command Prompt
endlocal
So what's the proper way to do this ?
Using the suggestion from #JMax to use the directory() method of the ProcessBuilder to set the working directory has fixed the issue

how to use logcat commands within java code?

I want to use "adb logcat -d > C:\Users\lenovo 01\Documents\android\sdk\platform-tools" command line command within my java code. this works directly in command prompt but it doesn't work within java code.
for example:
pb = new ProcessBuilder("adb", "logcat", "-d", ">", "C:\\android\\cellograf.txt");
pc = pb.start();
pc.waitFor();
System.out.println("written");
when I execute this, nothing happens. It writes only "written" but the file is empty. When I run this command in command prompt, it works correctly and writes all logcat output to that file. What am I doing wrong?
Redirecting output to a file is a feature of the command interpeter; it's not something that can be performed by the process itself. Try appending cmd /c to the beginning of your command:
pb = new ProcessBuilder("cmd", "/c", "adb", "logcat", "-d", ">", "C:\\android\\cellograf.txt");

Java ProcessBuilder to start execute multiple commands sequentially in Linux

I would like to execute 2 or more commands sequentially through my Java Application using ProcessBuilder class. I Have tried multiple options as suggested in other responses/forums but no luck.
Here are the things I have tried:
ProcessBuilder processBuilder = new ProcessBuilder("ls", ";", "pwd");
Gives me following error :
Errors : ls: ;: No such file or directory
Errors : ls: pwd: No such file or directory
ProcessBuilder processBuilder = new ProcessBuilder("ls", "&&", "pwd");
Gives me similar error:
Errors : ls: &&: No such file or directory
Errors : ls: pwd: No such file or directory
List<String> command = new ArrayList<String>();
command.add("ls");
command.add(";");
command.add("pwd");
ProcessBuilder processBuilder = new ProcessBuilder(command);
Gives me following error:
Errors : ls: ;: No such file or directory
Errors : ls: pwd: No such file or directory
My OS is Linux/Mac-OSX.
Your approaches are equivalent to calling ls with the specified arguments. In Bash notation, what you're running is:
ls ';' pwd
ls '&&' pwd
If you want ls and pwd to be run as separate commands, you can use Bash (or another shell language) to wrap them into a single command:
bash -c 'ls ; pwd'
which you can call this way:
ProcessBuilder processBuilder = new ProcessBuilder("bash", "-c", "ls ; pwd");
I'm using ProcessBuilder to compile java program like this and it works for me:
ProcessBuilder b = new ProcessBuilder("cmd.exe","/c","cd " + dir,
" & javac " + mapClassName + ".java -cp " + pathToProjectClasses);
cmd.exe : it's start the command prompt.
\c : not sure what its doing but its important, you can see this link for more information (\? cmd commands)
cd + dir : is the first command and its change the directory to a certain path which is dir.
& : its mean start the second command after you finish the first one
javac : this word and the rest of the string is the second command
-cp : path to external class used by the class you want to compile.
So I have 2 commands, first one is cd command and second one is javac command and i execute them sequentially using &.
Sorry for my bad writing skills, if I haven't explained my code well please ask me about anything you want to know.
You could get the Process from ProcessBuilder.start() from the first command, wait using waitFor() and then launch the second one.

How to unmount a Linux folder from Java

I tried:
final ProcessBuilder pb = new ProcessBuilder("umount", "foldername");
final Process p = pb.start();
Throws
umount: /home/user/foldername is not in the fstab (and you are not
root)
I tried
final ProcessBuilder pb = new ProcessBuilder("sudo","umount", "foldername");
final Process p = pb.start();
Throws
sudo: sorry, you must have a tty to run sudo
I got the root password, but can't provide it to the ProcessBuilder. Also I cannot edit fstab (or whatever is needed to be edited), because it is remote virutal machine started on a remote server from saved OS image.
I just want to run the command as root.
You have a couple of options:
Make the controlling terminal available for sudo so that the user can type the password there.
pb = new ProcessBuilder("sh", "-c", "sudo umount foldername </dev/tty");
Process p = pb.start();
p.waitFor();
Execute the program with gksudo rather than sudo. Systems that use GTK+ often come with the gksu package as a graphical interface for su and sudo.
pb = new ProcessBuilder("gksudo","umount", "foldername");
Open a terminal emulator window for sudo:
pb = new ProcessBuilder("xterm","-e","sudo","umount","foldername");

Start a Java process at low priority using Runtime.exec / ProcessBuilder.start?

I'm trying to start an external process via Java using the ProcessBuilder class, and that much works. Currently running using the command:
new ProcessBuilder("java", "-jar", jarfile, args);
What I would like to do is just this, but to start the process with low priority. My program is currently only running on Windows, so a window-specific solution is fine by me. Some research suggests I use the "start" command, but when I try doing this from Java it gives an exception saying it is an unrecognized command (the same command works from cmd.exe).
Does anyone know how to launch a process from Java (Windows-specific if need be), with belownormal priority?
Use start command. It is windows dependent but does what you need. I have read there is no cross platform way for this.
ProcessBuilder pb = new ProcessBuilder("cmd", "/C start /B /belownormal javaws -version");
System.out.println("Before start");
Process start = pb.start();
It is even possible to read Input end Error streams.
To wait:
ProcessBuilder pb = new ProcessBuilder("cmd", "/C start /B /belownormal /WAIT javaws -sdasd");
System.out.println("Before start");
Process start = pb.start();
start.waitFor();
System.out.println("Done");
For premature destroy:
ProcessBuilder pb = new ProcessBuilder("cmd", "/C start /B /belownormal /WAIT javaws -sdasd");
System.out.println("Before start");
Process start = pb.start();
start.destroy();
start.waitFor();
System.out.println("Done");

Categories

Resources