I want to run the following command from a java file command : "java hello < C:\iptest\input.txt > C:\outtest\name.txt "hello" will take input from "C:\iptest\input.txt" and will produce an output file at "C:\outtest\name.txt".
codes i have done
String command[]={"java","hello","< C:\\iptest\\input.txt >","C:\outtest\name.txt"};
ProcessBuilder pb=new ProcessBuilder(command);
pb.directory(new File("E:\"));
and now how to go forward i have no idea .Please Help!!
Here is a link to the ProcessBuilder definition on Oracle, which explains how to use this class. Typically, this is used for executing non-java processes, e.g. .BAT or .EXE processes, but I suppose you could use it to execute any process you wish.
http://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html
Related
Was reading this question: Execute .jar file from a Java program and the answer tells you to use a ProcessBuilder:
ProcessBuilder pb = new ProcessBuilder("/path/to/java", "-jar", "your.jar");
How do I find out the /path/to/java using code? Of course I could look up java in my machine, but an end-user may not necessarily have Java installed in the same place as I do...
Use System.getProperty("java.home").
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().
I am trying to compile a c program from a java program on Linux platform. My snippet is.
ProcessBuilder processBuilder = new ProcessBuilder("/usr/bin/gcc",
"-c","/hipad/UserProject/example.c");
Process proc = processBuilder.start();
There is no error during compilation of java program but I am not able to get .o file. I tried to find out solutions but no one is working.
Any suggestion.....
The default working directory of a child process is what ever directory the Java process has as a working directory, which usually is where it was launched from. And by default gcc writes output files to current working directory. That's where you should find example.o.
There are two simple ways to solve this. You can give gcc -o option and full path and name of desired output file, or you can set working directory of child process, like this:
ProcessBuilder processBuilder =
new ProcessBuilder("/usr/bin/gcc", "-c","example.c"); // source in working dir
processBuilder.directory(new File ("/hipad/UserProject")); // or whatever
Process proc = processBuilder.start();
See ProcessBuilder javadoc for more info.
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();
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.