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();
Related
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 have written a shell script file which extracts the files, Please see below.
File Name: unzip.sh
#/bin/sh
cd /home/zip;
UNZIPDIR=/home/unzip/;
for i in *.zip; do
unzip "$i" -d "$UNZIPDIR"
rm "$i";
done;
This shell script executes sucessfuly on putty,
$> ./zip.sh
As i wanted to execute this script from my java class while i have tried several ways to invoke/execute the shell script file but it's not executing. Please see below java code.
//First try
File executorDirectory = new File("/home/zip");
ProcessBuilder processBuilder = new ProcessBuilder("./unzip.sh");
processBuilder.directory(executorDirectory);
Process p = processBuilder.start();
p.waitFor()
//Second try
Process p = Runtime.getRuntime().exec("/home/zip/unzip.sh");
The problem is that you fail to account for the process' standard output/error (as mentioned by #Yazan in the comments). You need to .get{Output,Error}Stream() from the created process and read from them (even if it is only to discard it).
The real problem however is that you use an external shell script for something which is entirely doable in Java itself. See this page which gives an example of how to extract a zip file entirely with Java code; to delete a file, use Files.delete().
From my application I have to execute an external jar of which I do not have the source.
Given an input file, it processes it, creates an "output" directory and puts in it an mxml output file. Problem is: it creates said directory in tomcat/bin instead of inside the directory of the original file.
Here's what I've tried so far.
Initially
Process p = new ProcessBuilder("java -jar "+newfile.getParent()+"\\converter.jar "+newfile.getPath()+" -mxml").start();
Then, seeing how from console the "output" directory was created in the directory the command was called from, I tried:
String startSim[] = {"cd "+newfile.getParent()+"\\" , "java -jar converter.jar "+newfile.getName()+" -mxml"};
try {
Runtime.getRuntime().exec(startSim).waitFor();
} catch (Exception e) {
e.printStackTrace();
System.out.println("Log non creato.");
}
But with this I get the "file not found" exception for the first instruction. Does anyone know how to possibly solve this problem? I'd like to avoid having to reach for my output file all the way in my tomcat/bin directory.
Thanks for any suggestion!
Paolo
P.s.: by the way, before trying all this I tried simply calling the method I need from the library, but had the same exact problem. So I resolved to execute the jar, instead. And here we are. :)
You can set working directory using ProcessBuilder.directory() method:
ProcessBuilder pb = new ProcessBuilder();
pb.directory(new File("mydirectory"));
pb.command(......);
etc
This does not work for you when you are using Runtime.exec() because cd command is a functionality of shell. You could solve it using this technique but you have to create platform specific command with prefix like cmd /c on windows or /bin/sh on Linux. This way is definitely not recommended.
But in your specific case you do not neither first nor second solution. Actually you are starting one java process from another. Why? you can easily invoke the main() method of the second process directly.
Take a look on META-INF/MANIFEST.mf file from converter.jar. Field Main-Class contains the fully qualified name of main class. Let's say it is com.converters.Main (just for example). In this case you can invoke
com.converters.Main.main(new String[] {newFile.getPath(), "-mxml"});
directly from your code. Just add the jar to your classpath.
Concerning to changing working directory in this case. Check again whether you really need this or your converters.jar supports parameter that does this.
A lazy approach to this may be going to the root directory and descending from there to your tomcat bin directory .
TO run a hadoop program the command used is
hadoop/bin/hadoop jar hadoop/Anagram.jar com.hadoop.examples.anagrams.AnagramJob /user/hadoop-user/testdir /user/hadoop-user/outputdir
From PWD.
I need to run this command from a JSP page hosted in APACHE TOMCAT 7.0.
TO run linux command in JSP.
out.println(Runtime.getRuntime().exec("mkdir /tmp/testdirectory"));
Its working fine. But i need to run this hadoop command.
In reference to this question, I have done like this
<%
try{
out.println(Runtime.getRuntime().exec("start-all.sh"));
ProcessBuilder pb = new ProcessBuilder("hadoop jar hadoop/Anagram.jar com.hadoop.examples.anagrams.AnagramJob /user/hadoop-user/testdir /user/hadoop-user/outputdir5677");
pb.directory(new File("/home/hadoop-user/hadoop/bin/"));
Process p = pb.start();
}
catch(Exception e)
{ out.println("Error"+e);
}
%>
But it is throwing the exception
java.lang.UNIXProcess#5a8a7e Errorjava.io.IOException: Cannot run
program "hadoop jar hadoop/Anagram.jar
com.hadoop.examples.anagrams.AnagramJob /user/hadoop-user/testdir
/user/hadoop-user/outputdir5677" (in directory
"/home/hadoop-user/hadoop/bin"): java.io.IOException: error=2, No such
file or directory
When i use like this
How can i resolve my problem. I need to run this command. but this problem is related to path. :(
Any help would be appreciated!!
Try: ./hadoop jar hadoop/Anagram.jar ...
The PATH used is likely not containing the ".", or current directory.
This will make it search the bin directory that you're in.
ProcessBuilder takes comma separated arguments, not the entire command like in post. Search for ProcessBuilder examples
I need to install a .reg file (INTRANET) by using Java. How do i get my goal ?
Cheers,
You could use System.exec to launch regedit yourfile.reg
Here is how to do it :
String[] cmd = {"regedit", "yourfile.reg"};
Process p = Runtime.exec(cmd);
p.waitFor();
Last line is optional, it only allows you to wait until the operation is over.
If you're already on Java 1.6, just grab java.awt.Desktop:
Desktop.getDesktop().open(new File("c:/yourfile.reg"));
It will launch the file using the default application associated with it, as if you're doubleclicking the particular file in Windows explorer.
This can achieved through Process Builder in JAVA. Please consider the following example for this:
ProcessBuilder processBuilder = new ProcessBuilder("regedit", "reg_file_to_run.reg");
Process processToExecute = processBuilder.start();
And then you can optionally wait for the completion of process execution with this line:
processToExecute.waitFor();
Note: If command in your registry file asks for confirmation prompts while making changes in registry entries, you can perform it silently as well with '/s' option. Like this:
ProcessBuilder processBuilder = new ProcessBuilder("regedit", "/s", "reg_file_to_run.reg");
With this command would be executed silently without any confirmation prompt.