I want to run some unix/shell commands in Java and process the output. I used getRuntime.exec(), but it is not giving correct output for some commands like ls directory_path/*.tar.gz . For that command my program is not giving any output but it is giving error saying No such file or directory. But the same command is giving correct output on command prompt.
Is there any other way to execute commands which include wildcards in Java?
Is there any other way to execute commands which include wildcards in Java?
Yes there is.
You have three potential problems in your code:
Assuming you want to see the output of the external command you need to take care of the output it produces, and print it yourself.
Follow the example I've provided over here:
How to make a java program to print both out.println() and err.println() statements?
The shell expansion of * doesn't work if you naively go through Runtime.exec.You have to go through /bin/bash explicitly.
If you try to list the content of directory_path/*.tar.gz you should know that (if you're not starting with a /) directory_path will be resolved relative to the current working directory. Try at first to give the absolute path to make sure it works, and then try to figure out why it can't find the relative path.
Here's a complete example that should work:
Process proc = new ProcessBuilder("/bin/bash", "-c",
"ls /.../directory_path/*.tar.gz").start();
Reader reader = new InputStreamReader(proc.getInputStream());
int ch;
while ((ch = reader.read()) != -1)
System.out.print((char) ch);
reader.close();
That is because you specified a relative path to the directory and your application is running in a working directory. The path you specified is relative to that working directory.
To find out what the absolute path is, you can check out the working directory.
System.out.println("Working directory: " + System.getProperty("user.dir"));
"ls directory_path/*.tar.gz" works relative to CURRENT directory.
Related
I am using a .jar file, but unfortunatley as a black box, i.e. I do not know what exactly is in there nor how it all works.
I am sending commands to the Mac terminal from a Python script. I enter the following command:
java -jar jarfile.jar req_data /abs_path/to/required/data input path/to_my_/input/file.txt
This does what I need: analyses input using the 'black box' and creates and new file with analysis output. This new file is created in the folder where jarfile.jar is located.
I want to have this file put somewhere else upon creation.
I have tried using the > operator, specifying a path, e.g.:
java -jar jarfile.jar req_data /abs_path/to/required/data input path/to_my_/input/file.txt > /output/path/
this created a file in my desired location, but it was simply the message from Terminal, saying "The operation was carried out successfully" - the analysis results file was created in the same folder as before.
I tried %*> too, but it threw an error.
As a poor workaround I now have a function, which retrospectively finds and moves all the newly created files (analysis output) to my desired folder.
Is there a way to control the output files with the command line within the original command? Or is it something that is specified somewhere in my jar file? My problem is that editing it is not allowed.
I'm new to python. However, I may suggest to try few things, if they can work for you. Apology me, if does not work! I believe that you have already done the following step:
import subprocess
subprocess.call(['java', '-jar', 'Blender.jar'])
Like, if you have a properly configured jar path, then can run jar directly.
Secondly, look at the cwd parameter (is used for executable). Include a cwd param as x
def run_command(command, **x):
with subprocess.Popen(command,...., **x) as p:
for run_command specify the path of either the working directory (possibly it should be) or the full system path. I'm not sure, just try both.
for outputline in run_command(r'java -jar jarfilepath', cwd=r'workingdirpath', universal_newlines=True):
print(outputline, end='')
Alternatively, you can try to run command from the directory in which you wish to store output file. Try: run the popen as
subprocess.Popen(r'directory of running command', cwd=r'workingdir')
where workingdir could be your current directory path.
If it does not work, try without r'. If still does not work, try doubling slash in the path like (C:\\ abc\\def)
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 .
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 try to run batch file in java, but file performed not completely. Code look like:
Runtime.getRuntime().exec("cmd /c call "+path);
When I try to run it manually, without java, batch works correct.
Batch file contains next code:
cd C:\Downloads\
if not exist Documents mkdir Documents
move *.odt Documents
move *.doc Documents
if not exist Archives mkdir Archives
move *.tar Archives
move *.gz Archives
if not exist Music mkdir Music
move *.mp3 Music
Java complete batch only to fifth line. Has anyone faced this?
Hm, with batch, it stoped again here. And Java code: link. If add to somename.ogg to downloads folder, script goes to next line. All folders (Archives, Documents etc) are already exists, and downloads folder doesn't contain *.zip, *.tar, *.docx, *pdf files, but these lines passed without problems. Why it stoped precisely at ogg-line?
I think the problem is that there is a bug in your batch script. The MSDN documentation for the if command states this:
You cannot use the if command to test directly for a directory, but the null (NUL) device does exist in every directory. As a result, you can test for the null device to determine whether a directory exists. The following example tests for the existence of a directory:
if exist c:\mydir\nul goto process
Now the way you are using it (if not exist directory mkdir directory) is likely to succeed if the directory doesn't exist ... but attempt to create the directory a second time if it does exist. Ooops ...
Now, your Java application read and printed out the contents of the error stream for the process, you'd most likely see an error message telling you that the Archives directory already existed. Ignoring the output is asking for trouble, IMO.
I bet path in your Java program contains spaces or something similar. Are you quoting that properly?
Btw: Runtime.exec() has been superseeded with ProcessBuilder which can handle arguments much better than Runtime.exec()
Try this:
ProcessBuilder builder = new ProcessBuilder("cmd.exe", "/c", "call \"" + path "\"");
builder.start();
OMG, GUYS!
I don't understand why, but:
ProcessBuilder builder = new ProcessBuilder("cmd.exe", "/c", "call", "\"" + "sort.bat" + "\"");
builder.start();
doesn't works, BUT:
ProcessBuilder builder = new ProcessBuilder("cmd.exe", "/c", "call", "\"" + "sort.bat" + "\"");
BufferedReader br = new BufferedReader(new InputStreamReader(builder.start().getInputStream()));
String line;
while ((line=br.readLine())!=null) {
System.out.println(line);
}
works correctly! %)
If anybody understands this, please, explain this.
If you're not careful with your stdout and stderr streams it's very easy to deadlock ProcessBuilder or Runtime.exec(). The article When Runtime.exec() won't over at JavaWorld contains a lot of useful information on this and a couple of other pitfalls.
If possible, I would suggest using Commons Exec because all the required stream pumping is done for you.
I have a specific need to unrar files found in different subdirectories during execution of my program on Os x. I do this by calling the freeware command line tool unrar, which works very well.
However, unrar will always unrar files in the current working directory, which means that I have to specify a working directory for the executed process or get every unpacked file in my .jar home folder.
This is easy enough to do using either the processBuilder.directory(dir) or runTime.getRuntime().exec(args,null,dir) for example where dir is a File.
This works excellent but not when the working directory contains a space. As a short example:
File dir=new File("/Users/chargedPeptide/rar 2");
String[] cmd = { "/Users/chargedPeptide/rar/unrar", "e", "-o+","/Users/chargedPeptide/rar", "2/weather.rar"};
Process pr = Runtime.getRuntime().exec(cmd,null,dir);
int exitValue=pr.waitFor();
Will not work, while using: Process pr = Runtime.getRuntime().exec(cmd); instead will launch the command successfully but leave me with all of the files in the jars working directory.
Using processbuilder instead and using processbuilder.directory(dir); to set the directory
exits with the message:
Exception: Cannot run program "/Users/chargedPeptide/rar/unrar" (in directory "/Users/chargedPeptide/rar 2"): error=2, No such file or directory
Help? How do I handle the spaces? I've tried adding backslashes before the spaces to make them literal no help since the File object treats them like actual part of the path.
Edit: To make the whole thing a bit more clear:
1. I have a separate method that feeds the execute method a command and a directory to processbuilder, all directories are found by the previous method and exist. This works except when the dir contains a space.
2.I need to set the working dir or the command will execute in the wrong place.
3.Parsing the file object by:
dir=new File(dir.tostring.replace(" ","\ ");
to put a backslash in front of all spaces does not work since the java File object then looks for a path containing actual backslashes. No luck.
4.Using rt.exec instead of processbuilder dosen't help either.
Any ideas most welcome.
How about:
dir.mkdirs();
before launching the process.
This creates the missing directory.
Edit:
This looks strange.
String[] cmd = { "/Users/chargedPeptide/rar/unrar", "e", "-o+","/Users/chargedPeptide/rar", "2/weather.rar"};
If this was a shell you'd write:
unrar e -o+ "/Users/chargedPeptide/rar 2/weather.rar"
You actually have to put the .rar file in quotes, since otherwise it will be interpreted as 2 arguments to the process.
The way you've split 'cmd' will do exactly that, break the rar argument in two. Try:
String[] cmd = { "/Users/chargedPeptide/rar/unrar", "e", "-o+","/Users/chargedPeptide/rar 2/weather.rar"};
Not sure if it will work, but can you try putting a / at the end of the path.
i.e.,
File dir=new File("/Users/chargedPeptide/rar 2/");