Interact with extern process by Runtime.exec()

Hi,
I want interact with a extern process by Runtime.exec(), but I don't Know the way for introduce the params required for the extern process.
So, Is posible, interact with the extern process from Java?
Thanks

Exactly, I would like to know how exec can pass a PIN
number when the proccess already has been launched. Sounds like you're doing it through the process' stdin. Read that article I linked.
You'll have to call Process.getOutputStream() or whatever that method is. To you it's an OutputStream, to the process, it's input. You'll write the stuff to that stream that you'd type to the command line. If you want to get that from the user interactively, you'll do it like you would any interactive user input in any Java program--get it by reading System.in or from some GUI element, and then take that and write it to the process' stream.
If you're not familiar with I/O in Java, look here:
http://java.sun.com/docs/books/tutorial/essential/io/index.html

Similar Messages

  • Interacting with external processes

    What are the options in the latest AIR regarding interacting with external processes running on the same machine?
    Thanks

    You will need to user AIR 2.0.
    Here is a tutorial:
    http://www.adobe.com/devnet/air/ajax/quickstart/interacting_with_native_process_print.html

  • How to interact with external database in CQ5

    Hi,
    I need to interact with external database like SQL or Oracle to store some/fetch some data in CQ5. Can someone pls provide some help regarding the same. I guess I need to do it through JDBC. Please guide me step by step how to do this.
    Thanks

    Hi,
    Bellow you find some references to the documentation related to CQ and DB configuration and developments:
    http://dev.day.com/docs/en/cq/current/developing/jdbc.html
    https://helpx.adobe.com/cq/kb/HowToConfigureSlingDatasource.html
    Regards,
    kasq

  • Spawn a java process using runtime.exec() method

    Hi,
    This is my first post in this forum. I have a small problem. I am trying to spawn a java process using Runtime.getRuntime().exec() method in Solaris. However, there is no result in this check the follwoing program.
    /* Program Starts here */
    import java.io.*;
    public class Test {
    public static void main(String args[]) {
    String cmd[] = {"java", "-version"};
    Runtime runtime = Runtime.getRuntime();
    try{
    Process proc = runtime.exec(cmd);
    }catch(Exception ioException){
    ioException.printStackTrace();
    /* Program ends here */
    There is neither any exception nor any result.
    The result I am expecting is it should print the following:
    java version "1.4.0"
    Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-b92)
    Java HotSpot(TM) Client VM (build 1.4.0-b92, mixed mode)
    Please help me out in this regard
    Thanks in advance
    Chotu.

    Yes your right. It is proc.getInputStream() or proc.getErrorStream(). That is what I get for trying to use my memory instead of looking it up. Though hopefully the OP would have seen the return type of the other methods and figured it out.

  • Start a new java process using Runtime.Exec() seems to ignore the -Xmx

    I am working with a process that requires a minimum of 1.5 GB to run and works better if more is available.
    So I am determining how much memory is available at startup and restarting the jre by calling
    Runtime.exec("java -Dcom.sun.management.jmxremote=true -Xmx1500M -jar XXX.jar")
    which reinvokes the same process with a new max memory size.
    The initial call to the process is
    java -Dcom.sun.management.jmxremote=true -Xmx3500M -jar XXX.jar
    The initial call returns 3262251008 from Runtime.maxmemory()
    When reinvoked through Runtime.exec() as above
    Runtime.maxmemory() still returns 3262251008
    Is there a way to separate the new process from the size specified by the parent process?

    That is strange. Here is a program I wrote which calls itself recursively.
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.List;
    import java.util.ArrayList;
    import static java.util.Arrays.asList;
    public class MemorySize {
        public static void main(String... args) throws IOException, InterruptedException {
            System.out.println("Maximum memory size= "+Runtime.getRuntime().maxMemory());
            if (args.length == 0) return;
            List<String> cmd = new ArrayList<String>();
            cmd.add("java");
            cmd.add("-cp");
            cmd.add(System.getProperty("java.class.path"));
            cmd.add("-Xmx"+args[0]+'m');
            cmd.add("MemorySize");
            cmd.addAll(asList(args).subList(1,args.length));
            Process p = new ProcessBuilder(cmd).start();
            readin(p.getErrorStream());
            readin(p.getInputStream());
        private static void readin(final InputStream in) {
            new Thread(new Runnable() {
                public void run() {
                    try {
                        byte[] bytes = new byte[1024];
                        int len;
                        while((len = in.read(bytes))>0)
                            System.out.write(bytes, 0, len);
                        in.close();
                    } catch (IOException e) {
                        e.printStackTrace();
            }).start();
    }If you run this with the args 128 96 33 222 it prints out
    Maximum memory size= 66650112
    Maximum memory size= 133234688
    Maximum memory size= 99942400
    Maximum memory size= 35389440
    Maximum memory size= 231014400

  • Trying to run external script using Runtime.exec

    Hey,
    I am trying to use Runtime.exec(cmd, evnp, dir) to execute a fortran program and get back its output, however it seems to always be hanging. Here is my code snippet :
                Process process = Runtime.getRuntime().exec(
                      "./fortranCodeName > inputFile.txt" , null, new File("/home/myRunDir/"));
                InputStream stream = new InputStream(process.getInputStream());
                InputStream error = new InputStreamr(process.getErrorStream());
                BufferedReader stdoutReader = new BufferedReader(new InputStreamReader(stream));
                BufferedReader erroutReader = new BufferedReader(new InputStreamReader(error));
                System.out.println(stream.available());  //returns 0
                System.out.println(error.available());     //returns 0
                while (true) {
                    String line1 = stdoutReader.readLine();  //hangs here
                    String line2 = erroutReader.readLine();
                    if (line1 == null) {
                        break;
                    System.out.println(line1);
                    System.out.println(line2);
                }I know for a fact that this fortran code prints out stuff when run it in terminal, but I don't know if I have even set up my Runtime.exec statement properly. I think I am clearing out my error and input streams with the whole reader.readLine bit I have above, but I am not sure. If you replace the command with something like "echo helloWorld" or "pwd", it prints out everything properly. I also am fairly confident that I have no environmental variables that are used in the fortran code, as I received it from another computer and haven't set up any in my bash profile.
    Any Ideas?

    Okay, so I implemented the changes from that website (thanks by the way for that link, it helps me understand this a little better). However, my problem is still occuring. Here is my new code:
                class StreamThread extends Thread {
                InputStream is;
                String type;
                StreamThread(InputStream is, String type)
                    this.is = is;
                    this.type = type;
                public void run()
                    try
                        InputStreamReader isr = new InputStreamReader(is); //never gets called
                        BufferedReader br = new BufferedReader(isr);
                        String line=null;
                        while ( (line = br.readLine()) != null)
                            System.out.println(type  +">"+  line);
                        } catch (IOException ioe)
                            ioe.printStackTrace();
            try {
                Process process = Runtime.getRuntime().exec(
                      "./fortranCodeName" , null, new File("/home/myRunDir/"));
                StreamThread stream = new StreamThread(process.getInputStream(), "OUTPUT");
                StreamThread errorStream = new StreamThread(process.getInputStream(), "ERROR");
                stream.start();
                errorStream.start();
                int exitVal = process.waitFor(); //hangs here
                System.out.println("ExitValue: " + exitVal);

  • BPM - Interact with external system

    Hi Experts,
    We are implementing a project with BPM Suite Version 1.1.1.6 (PS5). A requirement is the following things:
      - The BPM process begins through a human task.
      - Upon completion of the human task, the flow must interact asynchronously with an external system through a webservice. The BPM system sends data to external system.
      - The user interacts with the external system and runs a couple of tasks.
      - After completing the activities in the external system, the BPM should be receiving an event to continue with the main flow was in wait.
    The question is....as an external system can reactivate a BPM process that is in wait hoping the event.
    Thank you very much.

    Investigate the use of the Call activity. If the external process is long-running, you might use a Service activity in conjunction with correlations.

  • Process proc=runtime.exec("sh","-c","//home//usr//mkdir abcd") not working

    my servlet calling the above line is not executing to make a directory
    "abcd" at specified location. i am using Mandrake Linux and Tomcat... can anybody expalin the error.

    Hi raghutv,
    I also have this error
    I use Tomcat 4.0 and Window Me
    Do u think that the problem is "Window OS" or Tomcat Setting
    I have a problem. I want to complie the other Java Program "hi.java" using servlet.
    I am compiling the servlet code succesfully, but the web page can't display anything.
    The real path of "hi.java" is in the
    D:\Program Files\Apache Tomcat 4.0\FYP\WEB-INF\classes\hi.java
    This is my servlet code..............pls help.............thx!
    /*********************** Hello.java *********************/
    import java.io.*;
    import java.util.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    public class Hello extends HttpServlet
    public void service(HttpServletRequest req,
    HttpServletResponse res)
    throws ServletException, IOException
    try {
    String path = getServletContext().getRealPath("\\WEB-INF\\classes\\hi.java");
    Runtime runtime = Runtime.getRuntime();
    Process process = runtime.exec("javac.exe " + path);
    BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
    PrintWriter com_out = res.getWriter();
    com_out.println("<pre>");
    String line = null;
    while((line = br.readLine()) != null){
    com_out.println(line);
    //String[] cmd = { "ping.exe", "time.nist.gov" }; // Command and argument
    //String[] cmd = { "javac.exe","hi.java"}; // Command and argument
    //Process process = Runtime.getRuntime().exec(cmd); // Execute command
    //Process process = Runtime.exec(cmd); // Execute command
    //process.waitFor(); // Wait until process has terminated
    catch (Exception e) {
    System.err.println(e);

  • Socket stays open after java process exits, Runtime.exec()

    I have a program that does the following:
    opens a socket
    Does a runtime.exec() of another program
    then the main program exits.
    what i am seeing is, as long as the exec'd program is running, the socket remains open.
    What can i do to get the socket to close?
    I even tried to explicity call close() on it, and that didn't work. Any ideas would be great.
    I am running this on WindowsXP using netstat to monitor the port utilization.
    here is some sample code
    import java.io.*;
    import java.net.*;
    public class ForkTest
        public static void main(String[] args)
            try
                DatagramSocket s = new DatagramSocket(2006);
                Process p = Runtime.getRuntime().exec("notepad.exe");
                System.out.println("Press any key to exit");
                System.in.read();
            catch (IOException ex)
                ex.printStackTrace();
    }

    java.net.BindException: Address already in use: Cannot bind
            at java.net.PlainDatagramSocketImpl.bind(Native Method)
            at java.net.DatagramSocket.bind(DatagramSocket.java:368)
            at java.net.DatagramSocket.<init>(DatagramSocket.java:210)
            at java.net.DatagramSocket.<init>(DatagramSocket.java:261)
            at java.net.DatagramSocket.<init>(DatagramSocket.java:234)
            at ForkTest.main(ForkTest.java:11)

  • External commands in runtime exec

    Im trying to run commands from the Sleuthkit in a java GUI. These commands have no problem running from the konsole as all the paths etc. have been set up, but when I try to run them with the java runtime exec() they wont work. Similarly,i run standard linux commands in this gui aswell and they run without problem.
    Any ideas whats weong?
    Thanks.

    You didn't tell us exactly what you're doing.Its a forensic GUI that runs konsole based commands
    and displays the ouput in a JText areaThat tells us nothing useful. By "exactly what you're doing," I mean the Java code and the command it's exec()ing.
    >
    You didn't tell us what "doesn't work" means.The 'basic' linux commands work fine in the gui, its
    just the sleuthkit ones that wont run or return
    anything when executed using the runtime exec(). Even
    though running these commands in a konsole works
    fine. THis means all paths have been set.
    Any ideas on a different way to run them or should I
    include more than just the command used on the
    konsole?That still doesn't tell us what "doesn't work" means.
    My previous response still applies.

  • Interact with another process

    Does anyone know how can I make my program send/read text from another program? Let's say that my active window is Notepad. I would like to be able to insert text and also read what has been typed (i.e. it would copy & paste).
    Any macro program can do that, but I want to treat the data with Java.
    Thanks.

    If what you want is just a copy&paste tool wich can interact with other programs, you can try the api clipboard from awt:
    import java.awt.datatransfer.Clipboard;
    import java.awt.datatransfer.ClipboardOwner;
    import java.awt.datatransfer.Transferable;
    import java.awt.datatransfer.StringSelection;
    import java.awt.datatransfer.DataFlavor;
    import java.awt.datatransfer.UnsupportedFlavorException;
    import java.awt.Toolkit;
    it can be useful:
    // clipboard
    public void getClipboardContents() throws Exception
         BufferedReader in;
         Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
         Transferable contents = clipboard.getContents(null);
         boolean hasTransferableText =(contents != null) && contents.isDataFlavorSupported(DataFlavor.stringFlavor);
         if ( hasTransferableText )
              in = new BufferedReader (new StringReader((String)contents.getTransferData(DataFlavor.stringFlavor)));
              String linea = in.readLine();
              while (linea!=null)
                   linea = in.readLine();
              in.close();
    public void setClipboardContents( String aString )
         StringSelection stringSelection = new StringSelection( aString );
         Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
         clipboard.setContents( stringSelection, this );
    the clipboard can be fullfilled from any app and read from java because it's a sysyem pool, but you cannot receive events from windows apps like notepad, you can copy notepad's text and paste it to your java app.

  • Problems with Runtime.exec() and certain Unix processes

    Certain Unix processes don't behave correctly when run from Java using Runtime.exec(). This can be seen by running /bin/sh and trying to interact with it interactively. The issue appears to be that /bin/sh (and many other Unix tools) are checking the file handles on the spawned process to see if they are associated with a TTY.
    Is there any way to associate a process spawned by Runtime.exec() with a terminal or alternatively, is there a JNI library available that would setup the process correctly and still provide access to the input and output streams?
    Our objective is to have the flexibility of expect in being able to run and interact with spawned processes. A bug was opened at one point but closed back in 1997 as being a fault in the spawned process, not Java.
    Bug ID: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4058689

    #include <stdio.h>
    void doit() {
    int c;
            while((c=getc(stdin)) != EOF) {
                    printf("%c",c);
    int main() {
            freopen("/dev/tty", "r", stdin);
            doit();
    }This program reopens its standard input against /dev/tty and catenates it to stdout. And voila, it takes its standard input from the terminal, even though it was started with stdin against /dev/null..
    $ gcc b.c -o b
    $./b < /dev/null
    buon giorno
    buon giorno

  • Increasing memory used by java process when calling Runtime.exec()

    Hi everyone!
    I'm running a servlet Oracle iAS 9i, Jserv. It receives an image posted by a user and does a System.exec() which performs a call to convert (an image processing utility included in ImageMagick).
    After a few hours, java process start taking more and more RAM, until it crashes with an error like this:
    Memory: 5394008 free of 134217728 total. (I log this myself)
    Exception: java.io.IOException: Not enough space
    java.lang.UNIXProcess.forkAndExec(), line -2.
    java.lang.UNIXProcess.<init>(), line 54.
    java.lang.Runtime.execInternal(), line -2.
    java.lang.Runtime.exec(), line 553.
    ImageProcessor.convert(), line 235.
    ImageProcessor.createItemPics(), line 124.
    Uploader.doPost(), line 69.
    As you can see, this is not an OutOfMemoryError, but something else in connection with O/S.
    This is not related to hard disk space, since there is a lot of free space in every filesystems
    After restarting the webserver, everything goes back to normal and start increasing RAM again.
    Is there anything I can do? Any help will be gratly appreciated... Thanks!!!
    Dani.-

    Have you had any solutions to this pal?
    I am also suffering. I tried closing the processes by:
    Runtime runtime = Runtime.getRuntime();
    Process proc = runtime.exec("/opt/sta_rpms.sh " + sFileName);
    proc.waitFor();
    proc.getInputStream().close();
    proc.getOutputStream().close();
    proc.getErrorStream().close();
    proc.destroy();
    proc = null;
    but still after like a day...Though our codes are not alike, it sounds like the same reason.
    Cheers!

  • How to exec command with parameters through runtime.exec()

    im trying to run a command prompt passing in parameters. Right now im creating a .bat file like this
    c:
    cd C:\SOMEDIR
    ant deploy moreparameters
    and then passing in the file name into the exec command. It is running fine but im trying to use the runtime.destroy() command and it doesnt close the ant command, it only closes the bat file which was already closed. Is there a command I could pass into the exec() method to run the ant command with the parameters without using a bat file?

    im getting a CreateProcess: ant deploy -Dswasm=swasm error=2
    When i try to do
    cmds[0] = "ant";
    cmds[1] = "deploy";
    cmds[2] = "-Dswasm=all-bs";
    Process p = runTime.exec(cmds, null, new File("C:\\Program Files\\apache-ant-1.6.1\\bin"));
    its like it cant find ant. But it finds java command fine. What should i pass into the second parameter, it says "If envp is null, the subprocess inherits the environment settings of the current process. " Does that mean it will take the window system enviroment variables? if not what string would i pass in to set where ant is?

  • Launch Command Line Scanner with Runtime.exec()

    Hi im trying to develop all the process of a common Antivirus ,
    I have a command line anti-virus. But when i try to scan a single file mi code doesnt work.
    I made the following code:
    Runtime runtime = Runtime.getRuntime();
    commands = new String[] {"cmd.exe", "/c","C:\\KAVCLineScanner\\kavecscan.exe","C:\\anfed\\a.txt"};
    Process p =runtime.exec(commands);
    BufferedReader b =new BufferedReader(new
    InputStreamReader(p.getInputStream()));
    String line=null;
    while( (line=b.readLine())!=null){
    System.out.println(line);
    I think the problem is my parameter where i send the file to scan "C:\\anfed\\a.txt", because if i execute this instruction from MSDOS, the process doesnt have problems.
    I also made a .bat file with the instructions but i have the same problem :(
    Thanks for any advise

    I think java naming is from programmer's point of view, so the output stream of the process would be the inputstream to the programmer, hence I am trying to write to my outputstream, which is actually the process' inputstream.

Maybe you are looking for

  • ICal Uses Obsolete Email Address for Reminders

    Hey Everybody, I transferred all my data from my old mac to my new laptop. In doing so, I got a new email address and iCal won't stop sending reminders to my old email address. I know it's doing this because I get mailer-daemon messages back about it

  • Sender JMS adapter configuration for IBM MQSeries

    Dear All, i am doing a IBM MQSeries 5.2.x Message ->sender JMS adapter -> XI server -> receiver RFC adapter -> R/3. i have deployed the JMS adapter with the required jar files of IBM MQSeries 5.2.x in XI server. in Sender JMS adapter configuration fo

  • Stock Ageing Analysis

    Dear all, We are planning to Golive by September1st,2007.Customer is having some stock pretaining to some ten years back where some are  unused in the mean period of time(say from 1995 to 2007).They want to upload that stock with the relevant dates s

  • Change screen resolution script.

    I'm trying to make a script to change the resolution of my screen from the current to a different one and back to the first. I need this to make some tests in my new monitor. I've looked everywhere, but apparently it's not as simple as I thought! To

  • Billing without invoice verification

    Hello Experts, In 3rd party sales, there is MIRO transaction used for invoice verification. What happens if Reseller or customer  is billed without invoice  verification? Is there a process to handle this? regards M Russo