How to unmount a Linux folder from Java - 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");

Related

Permission denied for ProcessBuilder and FFMPEG in MAC

I tried to run ProcessBuilder command in Mac. I couldn't run because of permission issues. Also, I tried to copy the folder and kept in the local folder. I even tried to do chmod. It's not working. I tried to execute the following code for frame capture using FFmPeg.
ProcessBuilder builder = new ProcessBuilder(
"/Users/balaji/Documents", "-i",
"Documents/Theintouchables.mp4", "-f", "image2",
"Documents" + "/" + "Test_Image" + "_%07d.jpg");
builder.redirectErrorStream(true);
Process process = builder.start();

Add command to cmd history

I'm trying to run console program inside windows cmd terminal. It works. But program which I run don't added to history.
String[] command = {"cmd.exe", "/C", "\"start cmd.exe /K \"" + commandLine + "\"\""};
ProcessBuilder builder = new ProcessBuilder().command(command);
start = builder.start();
I want to add ability to users to easily rerun this program lately.
Is there any way to add command to cmd.exe history?

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");

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

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);

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