Java runtime exec - java

I am trying to convert various xls files into csv. when I execute the following command in the terminal it works fine
libreoffice --headless --convert-to csv --outdir
/Data/edennis/ /Data/edennis/2013-10/*.xls
but when I try with runtime exec it does not.
Research I've done:
According to this thread Java Runtime exec() behavior cannot execute system commands like echo, but libreoffice is not a system command, isn't it an executable program ?
Java runtime execThis thread recommends to use processBuilder, but not sure if this is what I would need to do in my case.
According to the Java Doc:
EXEC:
Executes the specified string command in a separate process with the
specified environment.

First, there is no reason why Runtime.exec should not be able to run /bin/echo (if available).
Second, yes, use ProcessBuilder.
Third, your problems stem from using shell syntax for file patterns like *.xls. Runtime.exec calls the program you specify, not a shell that would do filename expansion. If you need to do filename expansion, run a shell like:
"sh -c libreoffice --blabla *.xls"

Related

Shell command in Java with Runtime.getRuntime().exec();

Is this a simple and good way to execute a Shell command via Java?
Runtime.getRuntime().exec( some command );
Or is this bad practice?
It depends.
The original purpose and basic functionality of a Unix shell is to let you run programs, optionally passing them arguments. For example the command ls runs the ls program, and the command grep foo bar runs the grep program with the arguments foo and bar. If your command only runs a (fixed) program with fixed if any arguments, Runtime.exec can do it. There are two subcases:
the overloads taking a String parse the line into 'words' (program name and arguments) using any whitespace; this is essentially the same as the default parsing (with no quoting) done by standard shells.
if you need any different parsing, for example if your command would use any quoting in shell, you must do that parsing yourself and pass the results to one of the overloads taking a String[].
But note that when you run a program from an interactive shell -- one using a terminal or equivalent (sometimes called a console) for input and output -- the program's input and output default to that terminal. The I/O for a program run by Runtime.exec is always pipes from and to the Java process, and some programs behave differently when their input and/or output is/are pipe(s) -- or file(s) -- instead of a terminal. Plus you must write code to send (write) any desired input and receive (read) any output. Of course, shells can be and sometimes are run without a terminal too.
However, shells can be and routinely are used to do much much more than the basics:
shell can execute commands with contents different from the input by variable (formally parameter) substitution (possibly with modification/editing), command substitution, process substitution, special notations like squiggle and bang, and filename expansion aka 'globbing' (so called because in the early versions of Unix it was done by a separate program named glob). Runtime.exec doesn't do these, although you can write Java code to produce the same resulting command execution by very different means.
shell executes some commands directly in the shell rather than by running a program, because these commands affect the shell process itself,
like cd umask ulimit exec source/. eval exit alias/unalias, or variables in the shell like set shift unset export local readonly declare typeset let read readarray/mapfile,
or child process like jobs fg bg, or special parsing like [[ ]] and (( )) (in some shells). These are called 'builtin' and Runtime.exec can't do them,
with two partial exceptions: it can run a program with a different working directory and/or env var settings, equivalent to having previously executed cd or export or equivalent.
Shell also often has builtins that duplicate, or modify, a 'normal' program; these commonly include test/[ echo printf kill time. Runtime.exec can only do the program version, not the builtin version.
shell has control structures (compound commands) like if/then/else/elif/fi and while/for/do/done and trap && || ( ) { }. Runtime.exec can't do these, although in some cases you could use Java logic to produce the same results.
shell can also have user-defined functions and aliases that can be used as commands; Runtime.exec does not.
shell can redirect the I/O of programs it runs, including forming pipes. Runtime.exec can't do these, but see below.
Since 1.5, Java also has ProcessBuilder, which provides the same functionality and more, in a more flexible and arguably clearer API, and thus is generally recommended instead. ProcessBuilder does support redirecting I/O for the program it runs, including using the terminal/console if the JVM was run on/from one (which is not always the case), and since 9 it can build a pipeline. It does not have the word-splitting functionality of Runtime.exec(String) but you can easily get the same result with string.split("[ \t]+") or in most cases just " +".
Note shell is itself a program, so you can use either Runtime.exec or ProcessBuilder to run a shell and pass it a command, either as an argument using option -c (on standard shells at least) or as input, and unsurprisingly this shell command can do anything a shell command can do.
But this can be a portability issue because different systems may have different shells, although any system claiming Unix certification or POSIX conformance must have a shell named sh that meets certain minimum requirements.
The actual shell used on different systems might be any of bash dash ksh ash or even more. OTOH this is true for other programs as well; some programs that typically differ significantly on different systems are awk sed grep and anything to do with administration like netstat.
A few of the existing Qs that show shell commands that don't work in Runtime.exec at least as-is:
a command for sherlock.py is interpreted differently from linux command line and java process api
Execute shell script multiple commands in one line using Process Builder in Java (Unix)
Check in Java if a certain application is in focus
Problem in executing command on AIX through Java
ProcessBuilder doesn't recognise embedded command
File not Found when executing a python scipt from java
Java system command to load sqlite3 db from file fails
Curl To Download Image In JAVA
Keytool command does not work when invoked with Java
use javap from within a java program on all the files
Using SSMTP and ProcessBuilder
Process Builder Arguments
Whitespace in bash path with java
java.lang.Runtime exception "Cannot run program"
Why does Runtime.exec(String) work for some but not all commands?
How to save Top command output in a text or csv file in java?
Execute bash-command in Java won't give a return
Using Java's Runtime.getRuntime().exec I get error with some commands, success with others -- how can this be determined?
Java and exec command - pipe multiple commands
Java exec() does not return expected result of pipes' connected commands
How to make pipes work with Runtime.exec()?
How to use Pipe Symbol through exec in Java
In Runtime.getRuntime().exec() getting error: /bin/bash: No such file or directory
Java exec linux command
How to use pipes in a java Runtime.exec
Java Runtime.getRuntime().exec and the vertical bar
Whenever I execute terminal command from code it gives "cannot run program" error=2 No such file or directory
Command line proccess read linux in java
Java Command line system call does not work properly

Perl Process can't find command svn when called from Java

I need to automatically execute some perl scripts from Java. I use Runtime.getRuntime().exec to start the perl process. I generate a start-parameter-string and pass it as an argument to exec. The String looks somewhat like this:
cmd /C start /wait perl "path\to\perl\script" -p1 scriptparameter1 -p2 scriptparameter2
I also tried
perl "path\to\perl\script" -p1 scriptparameter1 -p2 scriptparameter2.
If I copy that String and execute it via Windows+R everything works, but via exec the Perl-scripts can't find the svn-command. Why does it make a difference if I execute the perl process from java instead of directly from windows?
I figured it out. The reason was that I had just installed svn and the PATH envirment variable took effect for cmd but not for other processes (for whatevery reason). After a computer restart everything works as expected.

How to excute a Java program on a Unix server?

I have to create a Unix shell script to execute Java code which takes a input file and creates an output file. This is how I do it on Windows:
C:\Work\MCDExcelParserJE\bin>java -classpath .\;.\jxl.jar medicaid.Test PROCESS
How can I run this on Unix?
First thing is that you need to use colon(:) instead of semi-colon(;) in your classapth:
java -classpath .\;.\jxl.jar medicaid.Test PROCESS
should be updated to
java -classpath ./:./jxl.jar medicaid.Test PROCESS
Make sure you have jdk/bin directory is added to path in order to run the java commands.

Executing bash script with tilde in path

I am trying to execute in linux:
command[0] = "~/test/bin/runScript_sh";
Runtime.getRuntime().exec(command);
But get an exception
java.io.IOException: Cannot run program
error=2, No such file or directory
Probably because it can not evaluate tilde.
What can be done?
I would replace it myself.
if(path.s.substring(0,1).contains("~"))
path = path.replaceFirst("~",System.getProperty("user.home"));
Which gets you the string you want.
You can get the user's home directory with System.getProperty:
command[0] = System.getProperty("user.home") + "/test/bin/runScript_sh";
When you run a command at the shell command prompt, things like ~ expansion, quote handling, globbing, $variable expansion, input/output redirection and piping and son on are all handled by the shell ... before it asks the operating system to run the program(s) for you.
When you run a command using Runtime.exec, you have three choices:
write the command without any shell "funky stuff"
replicate what the shell would do in Java; e.g. replace leading tildes with the appropriate stuff1, or
use exec to launch a child shell to run the command; e.g.
Runtime.getRuntime().exec("/bin/sh", "-c", "~/test/bin/runScript_sh");
That is possibly overkill in a simple case like this. But if you are trying to do more complicated things then a child shell can really simplify things.
1 - In fact fully shell compatible handing of tildes is fairly complicated.
Items such as ~ and $HOME are shell expansions
You have to expand these items in your program and then replace them (hint: get them from the os properties, see this page)

Does getRuntime.exec() in java understands * or not?

I want a list of .tar.gz files from a specified directory. For this I am running "ls directory_path/*.tar.gz" using getRuntime.exec() in Java. But it is not giving any output rather it is saying No such file or directory..
But the same command is running on command prompt..
I am running java in unix.
Thanks in advance
This is because exec won't launch a shell just to run your program. It just starts a process. On Unix-like systems the shell is responsible for expanding wildcards to lists of files. So you would need to run ls through a shell to get the desired behaviour.
However, why do you use ls at all? This answer shows how to get a list of files with Java.
1. You will be able to fire processes using the exec() not the command promt..
2. Better use list() to get the names of all the files in a directory, and then use FileFilter to get the .tar.gz files.

Categories

Resources