I'm trying to open a PDF file in Linux with the xdg-open command in java.
String[] command = {"xdg-open","\""+path+"\""}
Process p = Runtime.getRuntime().exec(command,null);
p.waitFor();
When I run the code in terminal nothing happens even tho if I type it in terminal:
xdg-open path
it opens the PDF.
Any ideas whats wrong?
You should not escape the path: if the program was called, it was with an invalid path ("path" and not path).
String[] command = {"xdg-open", path}
The Runtime.getRuntime().exec(command,null); will use ProcessBuilder internally which, in the case of Linux, should invoke the system command execve.
Related
I am trying to write a java program that can open terminal and input a command to it. The command is "wrspice" which can start an application. I have added "wrspice" PATH in .bashrc and it works fine. But when I write a java code using ProcessBuilder to execute, it pop up "bash: wrspice: command not found" error. Here is my java code:
final String[] wrappedCommand;
wrappedCommand = new String[]{ "xterm", "-hold", "-e", "wrspice"};
Process process = new
ProcessBuilder(wrappedCommand).redirectErrorStream(true).start();
This code works fine when I replace "wrspice" with "ls" to print the file list. But when I try "wrspice" it gives me error for "command not found".
Here is how I add "wrspice" in .bashrc:
export PATH=/usr/local/xictools/bin:$PATH
Anyone have any ideas? Thanks in advance.
Java isn't an interactive shell. Specify the full-path to your command instead. And you don't need new String[]. Like,
wrappedCommand = { "xterm", "-hold", "-e", "/usr/local/xictools/bin/wrspice"};
You should probably specify the path to xterm as well. Double check on your system.
wrappedCommand = { "/usr/bin/xterm", "-hold", "-e", "/usr/local/xictools/bin/wrspice"};
I'm trying to run cmder.exe from a java application. I'm using the java ProcessBuilder class and it works great with cmd.exe, but never with cmder.exe.
This is my code:
command = "cd C:\\Users\\fxxx\\Documents\\plt 3.0\\git\\xxx-estatic";
ProcessBuilder builder = new ProcessBuilder("C:\\PLT 3.0\\abc\\cmder_mini\\cmder.exe", "/start", command);
I want to open the cmder command prompt and run a "cd" command. Cmder.exe is actually launched, but I get the "file name, directory name or volume label syntax is incorrect" error message and my command (cd...) is never executed.
Again, things work perfectly if I use cmd.exe.
Does someone know how to effectively run cmder.exe from a java application?
Thanks a lot.
RTFM!
Assuming that you use that cmder.exe, the doc states:
Cmder.exe Command Line Arguments
Argument Description
/C [user_root_path] Individual user Cmder root folder. Example: %userprofile%\cmder_config
/SINGLE Start Cmder is single mode.
/START [start_path] Folder path to start in.
/TASK [task_name] Task to start after launch.
That means that your code should be:
path = "cd C:\\Users\\fxxx\\Documents\\plt 3.0\\git\\xxx-estatic";
ProcessBuilder builder = new ProcessBuilder("C:\\PLT 3.0\\abc\\cmder_mini\\cmder.exe", "/start", path);
I'm a little curious about the behaviour of Runtime's exec() method when I run mysqldump. I'm running the following command:
mysqldump --user=root --hex-blob [database name] -r [path to sql file]
What I'm wondering is, where does Runtime search for the program mysqldump.exe?
I see that some people supply the whole file path to mysqldump.exe when executing it using Runtime. Why is this?
The reason why I'm curious is because I have two scenarios:
On one windows machine, if I open run and type "cmd" it will open a command window with the default location C:/. Running the mysqldump command on this machine works.
On another windows machine, if I open run and type "cmd" it will open a command window with the default location H:/. Running the mysqldump command on this machine fails. Java's Runtime cannot find the file mysqldump.exe.
Is it possible that the two windows machines have different default drives and if I don't supply the full path to mysqldump.exe, the system will look in the default driver?
Thanks in advance!
As mentioned in the documentation:
Starting an operating system process is highly system-dependent. Among the many things that can go wrong are:
The operating system program file was not found.
Access to the program file was denied
The working directory does not exist.
What I would suggest is starting with a ProcessBuilder, something like:
ProcessBuilder pb = new ProcessBuilder("mysqldump ...");
Map<String, String> env = pb.environment();
env.put("PATH", env.get("PATH") + ";Path/to/mysqldump");
try {
Process process = pb.start();
//some code
} catch(IOException e){
}
This way you ensure the environment variable is correctly set.
I need to execute linux commands from JSP.
It is working fine.
But i need to start some sh file in a particular directory in linux through JSP. say /home/username/something/start.sh
try{
String command= "cd /home/username/something";
Runtime.getRuntime().exec(command);
Runtime.getRuntime().exec("./start.sh")
out.println("Child");
}
catch(Exception e)
{ out.println("Error");
}
It says FIle or Directory not found.
I tried Runtime.getRuntime().exec("pwd"), It is showing something like "java.lang.UNIXProcess#fc9d2b" !! :O
I need to change the pwd and execute some commands through jsp. How can i do that??
Any help would be appreciated.
You shouldn't (and actually, it seems you can't) set a working directory like that. Each Process object given by Runtime.exec() will have its own working directory.
As answered in How to use “cd” command using java runtime?, you should be using the three argument version of Runtime.exec(), in which you provide a File that will be the working directory. From its javadoc:
Executes the specified command and arguments in a separate process with the specified environment and working directory.
Or even better, use ProcessBuilder along with ProcessBuilder.directory() instead:
ProcessBuilder pb = new ProcessBuilder("start.sh");
pb.directory(new File("/home/username/something"));
Process p = pb.start();
Is there a way to make Java spawn a bash script on Windows? I have cygwin installed, and I've associated the .sh file extension with cygwin bash. The following code, which works on Linux, isn't working:
String[] cmdArray = { "scriptName.sh", "-force", categoryName};
Process proc = Runtime.getRuntime().exec(cmdArray, null, directory);
Try adding either bash or start as the first parameter.
I think you'll have to provide the executable itself in order to do this. The Process.exec method only seems to execute exe files.