Calling external programs from Java?

Hi All,
Is there a way of calling external applications from Java without using Runtime.exec(). That method seems quite messy when you are dealing with streaming data from an input file to an output file. Basically what I'm asking is there a way to run a command the same way you would type it in a command shell?
Thanks

LeWalrus wrote:
Ok, I've an external application that I want to be called inside a Java GUI. It has several input arguements, which the format looks something like:
programname inputfile > outputfile
Simple enough.
>
Works fine from a shell command line. From what I understand, this won't work using Runtime.exec() because that method will just start the application. Works fine from Runtime.exec(). Since you are'>' to write stdout to a file you need to us a shell to execute the command.
String[] command = {"sh", "-c","programname inputfile > outputfile"};
Process process = runtime.exec(command);
You need to read, digest and implement the recommendations given in http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html .
File input and output from the application has to be taken care of programatically (java.io stuff). Fine if you've only one input and output. But if you have several input files and hundreds of output files, where does this leave you. It would be much easier if you could pass a string command to the shell directly as if you typed it in the command line yoursel!

Similar Messages

  • How to call external files from java?

    How to call external files in java. For example how to call a *.pdf file to open in its default editor(say Acrobat), or a *.html file to open in the default browser or a *.txt file in a notepad etc..,
    In my program i have *.chm (Compiled Windows HTML Help) help file. how to open it in its default editor it?

    Jayarathina_Madharasan wrote:
    no one answered my questionHi what wrong did i do...basically insulted all the volunteers here who took the time to consider your question and try to offer you help. Other than that, you did nothing wrong.
    From JavaRanch :
    And even if an answer doesn't solve your problem, even if it should totally miss the point - the best thing to do to motivate others to continue trying to help you is showing respect and gratitude for the investment of time that was put into dealing with your issue.
    Edited by: Encephalopathic on Apr 14, 2008 10:01 AM

  • Error in calling 'C'  Program from Java

    Hi,
    We developed a 'C' Program and during compilation, we got a couple of Incompatible Prototype warnings. Then we made a Jni call to 'C' program from Java Program. The program returned a "Unsatisfied Link Error - unreference Symbol not found" error.
    Does Jni call fails owing to warnings by compiler?
    We are using Sun Solaris 2.8.
    Request your assistance as this a urgent issue.
    Thanks
    Srini

    Did you try running your C program through g++? It sounds like something is broken, but GCC often will allow you to compile anyways.
    -Jason Thomas.

  • Calling external programs from within a Tomcat application

    I've got a fairly complex existing Tomcat application (which is packaged and built as a .war file) which I'm trying to edit so that it calls a Perl script part way through the processing, which will generate an XML file used later on. I think I've worked out how to call external scripts from within Java, but at the moment when I try and access the application via Tomcat the application either hangs or bails out (I don't know which, as the log files unhelpfully don't give any error messages).
    The code which is causing the problem looks like this:
    System.err.println("Calling runtime...");
    Runtime runtime = Runtime.getRuntime();
    System.err.println("Executing process...");
    Process process = runtime.exec("/path/to/ysearch.pl 'News' '\"search query\"' 'file");
    System.err.println("Waiting for process...");
    int exitVal = process.waitFor();
    System.out.println("Exited with error code: " + exitVal);The code gets as far as "Executing process..", beyond that there is nothing in the log file so I presume the runtime.exec() call is where the problem is. I'm not interested in reading the output from the script (there shouldn't be any), so that's not an issue, and the permissions on it allow anyone to read or execute ysearch.pl so I don't think there's a problem in that area.
    Does anyone have any suggestions which I could try to get this to work? I've only been using Tomcat for a week (I'm picking up on someone else's code) so I might have made a beginner's mistake. I'm using Tomcat 5 on Fedora Core 7, and Java 1.5.0_01 (I can't easily change any of those).

    I'm not sure, but I thing that overhead caused by calling du cannot be big enough to matter even on older machines.
    But, when calling du from Perl script you are also invoking shell, and this can be a little bit more 'heavy'.

  • Calling PERL program from JAVA

    what is the most efficient way to call PERL scripts from JAVA class?
    please help,
    thank you,

    use of Webservices is the best answer which anyone can give you
    Alright you might have to take help of XML-RPC(by hosting perl in a remote server) to call perl routines from java Class instances.
    How you do that ?? please go ahead and try get more insight information from the below aritcle
    http://www.javaworld.com/javaworld/jw-10-2004/jw-1011-xmlrpc.html
    and if you are instrested in any other core solutions
    http://www.perl.com/pub/a/2003/11/07/java.html
    http://sunsite.ualberta.ca/Documentation/Misc/perl-5.6.1/jpl/docs/Tutorial.html
    http://search.cpan.org/~patl/Inline-Java-0.52/Java/PerlInterpreter/PerlInterpreter.pod
    http://www.perlmonks.org/index.pl?node_id=373839
    the above links might intrest you.
    Hope that might help :)
    REGARDS,
    RaHuL

  • Call other programs from Java

    There is this command line program I want to wrap with a Java class in order to make it available so that it can be transparently called.
    How can I call or execute external programs/commands from JAVA...
    is not native code that I want to run...
    Thank you

    Runtime.getRuntime().exec("your command line")... you could read more about it in the API documentation.

  • Calling external program from PL/SQL

    Does anyone know whether it's possible to call an external program living outside of Oracle from PL/SQl eg. a from a stored procedure?
    Specifically i want to call back into my java appserver (ejb server) when a trigger/stored proc. is executed.
    The call does not have to be synchronous, some sort of queue will do - eg JMS??
    Any help greatly appreciated.
    null

    Hi
    Which version of oracle you are using.
    If it 8i, you can use external procedures/libraries. REfer to ORacle PLSQL documentation.
    If you are using Oracle 7+ you can use dbms pipes to post your command string and a cron job to read the pipe and execute the string.
    An alternate ugly way is to write to a file and some process reading the file at a regular interval.
    HTH
    Arvind Balaraman

  • Calling external servlet from java stored procedure

    Hello,
    I need to call an external servlet which is in 9iAS server ( unix box) from Java Stored procedure in oracle database.
    Can anybody give me an idea? is it possible?
    Thanks,
    Viswa

    I am trying the same. Here is URL which will help u.
    http://otn.oracle.com/sample_code/tech/java/jsp/samples/wsclient/Readme.html
    Let me know when you run servlet successfully.
    Regards
    Satish

  • Calling c program from java

    Hi,
    we have written some code in c and we need to call those codes from an interface written in java. How do I do it ?
    so far as i heard, there is something call java native interface...is that true...how do i call up the c program

    java native interface...is that true...how do i call up the c program
    JNI Tutorial
    JNI Tutorial (ZIP)
    (found by the Magic of Google: JNI Tutorial)
    or Runtime.exec if you c program is a complete program.

  • Starting a external program from java

    hi iam trying to start MySQL server from java, the command in dos is as follows C:\mysql\bin\mysql -u username -p ,then the user hits return and the user is prompted to enter a password. i have got as far as entering the the C:\mysql\bin\mysql -u username -p but dont know how get the prompt back to the java program i would be greatfull if anyone could tell me where iam not doing,heres my code so far
    try{
    Runtime r=null;
    r=r.getRuntime();
    Process p=r.exec("C:/mysql/bin/mysql -u mark_r -p");
    p.waitFor();}
    catch(Exception ex){System.out.print("Error");}}

    You should try using the getOutputStream() and getInputStream() methods for the Process object. Each returns either an inputStream or OutputStream object.
    You should be careful if the mySQL password is added to the command since anyone doing a 'ps' on the machine could see the password.

  • Pocket PC -Problem executing external program from java

    Hi,
    I'm developing an application on Dell Axim x51 PDA. I am using mysaifu jvm. The application needs to execute an external program (.exe) for which I'm using the Runtime class.
    I get java.io.IOException: The system cannot find the file specified.
    I have verified that the file exists. ( i used File class to check if the file exists)
    here's the part of code :
    Runtime rt = Runtime.getRuntime ();      
    File sktScan = new File("\\My Documents\\RFID\\ScktScan.exe");
    if(sktScan.exists())
    System.out.println("FILE EXISTS");
    String command = sktScan.getAbsolutePath();
    process = rt.exec (command);
    System.out.println("Socket Scan Path is : "+command);

    You have acces to "java.lang.Runtime currentRuntime.exec()" method ? in my case NetBeans IDE dont give me for my compilation with :
              microedition.configuration     CLDC-1.1     
              microedition.profiles     MIDP-2.0

  • Calling external scrips from Java?

    Hi,
    I am writing a Java application to call an external script, to start with a unix shell script. I need the application to not only call this script but also for the script to return some sort of value or variable to the Java application.
    Is it possible for Java to get return variables from external scripts?
    Thank you,
    Paul

    The following code snippet executes a given command and returns an array containing its return code and its standard error output. Hope this helps.
            private Object [] performKshCommand(String command) {
            int     iRet=-1;
            String  sRet=null;
                    if (DEBUG1) System.out.println("performKshCommand\n" + command);
                    try     {
                            Runtime         rt=Runtime.getRuntime();
                            Process         proc=rt.exec(command);
                            InputStream is=proc.getInputStream();
                            LineNumberReader lnr=new LineNumberReader(new InputStreamReader (is));
                            sRet=lnr.readLine();
                            String line;
                            while((line=lnr.readLine()) != null) {
                                    if (DEBUG1) System.out.println(line);
                                    InputStream is2=proc.getErrorStream();
                                    LineNumberReader lnr2=new LineNumberReader(new InputStreamReader (is2));
                                    while((line=lnr2.readLine()) != null) {
                                            System.err.println(line);
                            iRet=proc.waitFor();
                    catch (Exception e) {
                            if (DEBUG) e.printStackTrace();
                            if (!DEBUG) e.printStackTrace();
                    return new Object [] { new Integer(iRet), sRet} ;

  • Controlling external programs from Java

    Hi,
    I try to invoke an external command-line program in Windows 2000 fron Java. I do this by
    Process proc = Runtime.getRuntime().exec(cmd);
    cmd is a String representing a command. For instance I try to control the gnupg program by
    "cmd /c C:\\gnupg\\gpg --list-keys"
    This works fine. But when I want to make a more advanced control, when gnupg waits for more than one input, and that input can't be put on the same line I get problems.
    When running gpg --edit-key SomeKey on the command line I have to enter
    trust - return - 5 - return - yes - return - quit
    How do I do this?

    this is how to do it! gotta listen to the output to prevent deadlock. you can pass in parameters o the initial call as well (a string array, where string[0] = command, string[...] = input argument) or write it to the output stream of the process.
    hope this helps
    public void run_command(String starting_command, String[] further_commands)
    Process proc = Runtime.getRuntime().exec(starting_command);
    Listener.make(proc.getInputStream());//these listen to the streams
    Listener.make(proc.getErrorStream());//which prevents deadlock
    PrintWriter writer = new PrintWriter(new OutputStreamWriter(proc.getOutputStream()));
    for(int i = 0;i < further_commands.length;i++)
    writer.println(further_commands);//you could put the extra stuff as the first string in this array
    import java.io.*;
    public class Listener extends Runnable
    BufferedReader reader;
    public static void make(InputStream in)
    Listener l = new Listener(in);
    Thread thread = new Thread(l);
    thread.start();
    public Listener(InputStream in)
    reader = new BufferedReader(new InputStreamReader(in));
    public void run()
    String string;
    while(true)
    string = reader.readLine();
    try
    Thread.sleep(1000);
    catch(Exception e)

  • Calling windows programs from Java

    Hello,
    I need to find how to call and pass parameters to windows programs. Specifically I need to call MS Sql server, there is call that I can do from command promt but I would like to give it some GUI interface and ease of use so I could pass some parameters to the program and execute it.
    Thanks.

    Hi thanks for all replies !
    Ok let me explain more about my problem.
    I have a SQL server which runs on MS machine and there is thousands of stored procs that my be recreated once in a while from a directorys. So we have scripts which we have to manually update every time new stored proc is written and run against the server to update all of them at one batch.
    Now I often have to browse to this directory from linux machine and I basically already written the Java program that gets all the files in the directory and passes them via JDBC to the server.
    Thats where the problem starts, when you calling and passing sp to sql without any header seting the jdbc is fine but when you pass sp with such settings for instanse as "SET QUOTED_IDENTIFIER and SET ANSI_NULLS OFF" in the header of stored proc then SQL server complains and fires exceptions on me.
    So this is my problem.

  • Problems running external programs from java

    Hello.
    I wrote a pair of perl scripts and a GUI in java to run them. The first perl script just read the files in one directory makes some changes to the names of the files and then group all this files in a set of new directories. The other perl scripts takes all this new files and calls BLAST sequence alignment program and perform some alignments among these sequences. I tested this scripts and they work fine.
    The problem comes when I try to run them for the JAVA GUI. I use RunTime and when I need to run the first perl script it all works well, but when The call to the second perl script is made the program fisishes without doing anything at all. I found out that the problem is that when running the script from the Java GUI it's not able to find BLAST program. So I guess that Java is not really starting a terminal session and it doesn't read my bash_profile to find out the path to my programs.
    So, my question is if anyone knows a method to tell Java to load all this paths in the bash_proflie file so all of my scripts work???.
    I have no idea is this can be done and how so any advice would be really wellcome.
    By the way, my java version is 1.4.2 and my OS is Mac OS X 10.3
    Thanks a lot , Julio

    Invoke /bin/sh -c and give it your program's full path with.
    (To understand what I've written, maybe reading
    http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps_p.html
    http://mindprod.com/jgloss/exec.html
    and
    http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Runtime.html
    (especially the exec(String[] cmdarray) method)
    might help)
    -T-

Maybe you are looking for

  • Process Code for Message Type LOIPRO(in ALE/IDOCS)

    Hi all,         I have to implement Production order Interface(create Production order,change Production order etc).The requirement is for eg:,I created one production order and posted it;it should get reflected in the recieving system.Does anyone kn

  • Adding a node at an xpath

    Hello, I need to build a DOM Document by adding nodes to it at certain xpaths. For example, I need to add the value "bla" in /myRoor/e1/cfgr/tete/myNode. What is the best way to do that? Thanks, Zohar.

  • FILE번호/BLOCK번호와 DBA 사이의 변환 SCRIPT

    제품 : ORACLE SERVER 작성날짜 : 1999-02-24 File 번호/Block 번호와 DBA 사이의 변환 script block dump나 block corruption 등의 해결을 위해 file 번호와 block 번호를 DBA로 변환하거나 그 반대의 경우가 필요한 경우가 종종 있다. 여기에서는 이를 수행하기 위한 간단한 script를 제공한다. 아래의 script를 각각 별도의 file로 save한 후 file을 실행하여 proc

  • JOptionPane input question

    Hi everybody, I have JOptionPane input dialog in a program to input file names, and I have a lot of text files to input, over 20. They all come from the same folder, the only difference is their names. Does anyone know, is there a way to have the JOp

  • Is it possible to add a Negative filter / Boost / Bury option to a RecordSpotlight ?

    Hi, We have a requirement in our implementation where in a spotlight should show all records that match some criteria and filter out a few from that which matches another criteria, kind of a Negative filter. This doesnt seem to be possible in the Spo