Run EXE from JAVA program

Does somebody know whether it is possible to run some program with extension EXE from JAVA program?

Yes sombody knows. Yes it's possible. Have a look at Runtime#exec.

Similar Messages

  • Calling C code (exe ) from Java program

    Hi all,
    kindly help in giving a solution for calling C code (exe ) from Java program, i will be very thank full to u if any body can help in sending a code example
    bye

    You might need to consume the io operations on the Process...
    An excerpt from java.lang.Process:
    All its standard io (i.e. stdin, stdout, stderr) operations will be redirected to the parent process through three streams (Process.getOutputStream(), Process.getInputStream(), Process.getErrorStream()). The parent process uses these streams to feed input to and get output from the subprocess. Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, and even deadlock.

  • Running Fop from JAVA program

    Hi all,
    this may be a rookie question....hope you can help.
    I can run Fop (0.18.0) perfectly from the command line. I created a style
    sheet (xsl file), which converts the xml file. On the command line, I input
    the xsl file, the xml file, and state the name for the pdf file.
    Now I'm trying to run Fop from within method in my JAVA program. This
    doesn't seem to work. Here's the code. Can someone tell me what I'm doing
    wrong? Is it okay to run Fop this way? My classpath is set up properly
    (it runs on the command line).
    Here's the code..............Thanks in advance for any help.
    String [] pdfParams = new String[6];
    pdfParams[0] = "-xsl";
    pdfParams[1] = xslFile;
    pdfParams[2] = "-xml";
    pdfParams[3] = xmlFile;
    pdfParams[4] = "-pdf";
    pdfParams[5] = pdfFile;
    org.apache.fop.apps.Fop.main(pdfParams);
    Regards,
    Suryakant.

    When you post code, please use[code] and [/code] tags as described in Formatting tips on the message entry page. It makes it much easier to read.
    One thing that jumps out at me is that you're calling waitFor() and then trying to read the process' stdout. But waitFor waits for the process to die, so the stream is no longer there when you try to read it.
    See this:
    http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html

  • Creating and running JARs from Java programs

    OK, I have been googling and searching, but this is something I cannot work out, so I raelly need some help.
    Basically, I am building a GUI app that would generate, compile and run java programs. And I also want to enable the users to create JARs they can later double-click if they want to run the program.
    I am using Runtime to compile programs and it works just fine. When I click on a button to create a JAR, my application creates one, but when I double-click on it, it can't find main and it exits. Now that is not so much unusual, but I am positive that my manifest file is OK, because when I run the same command from the command line, then I can click on the JAR file to run it.
    So what is the deal here? I mean, why would a JAR file created from the command line run and the one created through a GUI app using the very same command could not find main?
    Any help would be graetly appreciated.

    again, another unanswered question.
    BTW I'l keep bumping these until I get an answer that works. I have been up 3 hours now trying to get this sorted and it's something that netbeans is supposed to do on its own!?!?!?!?!
    Hello, SUN, is there anyone there???
    No, didn't think so.

  • Running mSync from java program

    hi all,
    I need to control synchronization from inside my java
    program.
    There is “msync.jar” available with Lite installation,
    I tried to use it but didn't worked.
    Any solution will be appreciated.
    Thanks in advance.
    avinash

    When you post code, please use[code] and [/code] tags as described in Formatting tips on the message entry page. It makes it much easier to read.
    One thing that jumps out at me is that you're calling waitFor() and then trying to read the process' stdout. But waitFor waits for the process to die, so the stream is no longer there when you try to read it.
    See this:
    http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html

  • Problem while running dos command from java program

    Dear friends,
    I need to terminate a running jar file from my java program which is running in the windows os.
    For that i have an dos command to find process id of java program and kill by using tskill command.
    Command to find process id is,
    wmic /output:ProcessList.txt process where "name='java.exe'" get commandline,processid
    This command gives the ProcessList.txt file and it contains the processid. I have to read this file to find the processid.
    when i execute this command in dos prompt, it gives the processid in the ProcessList.txt file. But when i execute the same command in java program it keeps running mode only.
    Code to run this command is,
    public class KillProcess {
         public static void main(String args[]) {
              KillProcess kProcess = new KillProcess();
              kProcess.getRunningProcess();
              kProcess = new KillProcess();
              kProcess.readProcessFile();
         public void getRunningProcess() {
              String cmd = "wmic /output:ProcessList.txt process where \"name='java.exe'\" get commandline,processid";
              try {
                   Runtime run = Runtime.getRuntime();
                   Process process = run.exec(cmd);
                   int i = process.waitFor();
                   String s = null;
                   if(i==0) {
                        BufferedReader stdInput = new BufferedReader(new
                               InputStreamReader(process.getInputStream()));
                        while ((s = stdInput.readLine()) != null) {
                         System.out.println("--> "+s);
                   } else {
                        BufferedReader stdError = new BufferedReader(new
                               InputStreamReader(process.getErrorStream()));
                        while ((s = stdError.readLine()) != null) {
                         System.out.println("====> "+ s);
                   System.out.println("Running process End....");
              } catch(Exception e) {
                   e.printStackTrace();
         public String readProcessFile() {
              System.out.println("Read Process File...");
              File file = null;
              FileInputStream fis = null;
              BufferedReader br = null;
              String pixieLoc = "";
              try {
                   file = new File("ProcessList.txt");
                   if (file.exists() && file.length() > 0) {
                        fis = new FileInputStream(file);
                        br = new BufferedReader(new InputStreamReader(fis, "UTF-16"));
                        String line;
                        while((line = br.readLine()) != null)  {
                             System.out.println(line);
                   } else {
                        System.out.println("No such file");
              } catch (Exception e) {
                   e.printStackTrace();
              return pixieLoc;
    }     when i remove the process.waitFor(), then while reading the ProcessList.txt file, it says "No such file".
    if i give process.waitFor(), then it's in running mode and program is not completed.
    Colud anyone please tell me how to handle this situation?
    or Is there anyother way to kill the one running process in windows from java program?
    Thanks in advance,
    Sathish

    Hi masijade,
    The modified code is,
    class StreamGobbler extends Thread
        InputStream is;
        String type;
        StreamGobbler(InputStream is, String type)
            this.is = is;
            this.type = type;
        public void run()
            try
                InputStreamReader isr = new InputStreamReader(is, "UTF-16");
                BufferedReader br = new BufferedReader(isr);
                String line=null;
                while ( (line = br.readLine()) != null)
                    System.out.println(type + ">" + line);
                } catch (IOException ioe)
                    ioe.printStackTrace(); 
    public class GoodWindowsExec
        public static void main(String args[])
            try
                String osName = System.getProperty("os.name" );
                String[] cmd = new String[3];
                 if( osName.equals( "Windows 95" ) )
                    cmd[0] = "command.com" ;
                    cmd[1] = "/C" ;
                    cmd[2] = "wmic process where \"name='java.exe'\" get commandline,processid";
                } else {
                    cmd[0] = "cmd.exe" ;
                    cmd[1] = "/C" ;
                    cmd[2] = "wmic process where \"name='java.exe'\" get commandline,processid";
                Runtime rt = Runtime.getRuntime();
                System.out.println("Execing " + cmd[0] + " " + cmd[1]
                                   + " " + cmd[2]);
                Process proc = rt.exec(cmd);
                System.out.println("Executing.......");
                // any error message?
                StreamGobbler errorGobbler = new
                    StreamGobbler(proc.getErrorStream(), "ERROR");           
                          // any output?
              StreamGobbler outputGobbler = new
                    StreamGobbler(proc.getInputStream(), "OUTPUT");
                          // kick them off
                errorGobbler.start();
                outputGobbler.start();
                // any error???
                int exitVal = proc.waitFor();
                System.out.println("ExitValue: " + exitVal);       
            } catch (Throwable t)
                t.printStackTrace();
    }when i execute the above code, i got output as,
    Execing cmd.exe /C wmic process where "name='java.exe'" get commandline,processid
    and keeps in running mode only.
    If i execute the same command in dos prompt,
    CommandLine
    ProcessId
    java -classpath ./../lib/StartApp.jar;./../lib; com.abc.middle.startapp.StartAPP  2468
    If i modify the command as,
    cmd.exe /C wmic process where "name='java.exe'" get commandline,processid  > 123.txt
    and keeps in running mode only.
    If i open the file when program in running mode, no contents in that file.
    If i terminte the program and if i open the file, then i find the processid in that file.
    Can you help me to solve this issue?

  • Facing problem with running exe from my java code

    Hello,
    I have to run the exe from java code for which I m using Runtime and Process classes. The exe runs in the following way on command line
    C:/ myexe -j [now press enter]
    input sentence followed by ! [now press enter]
    And after this exe gives the output.
    So is there any way to achieve this programmatically in Java?
    Thanks in advance.
    Regards,

    I m confused .. how can i pass input to the Process through InputStream? I think it will need OutputStream for that.
    Here is my code...
    import java.io.*;
    class CallToExe
    static BufferedReader br;
    public static void main(String[] args)
    File F = new File("C:/Chasen.exe");
    try
    if( F.exists())
         System.out.println("Exe exists");
         Runtime rt = Runtime.getRuntime();
         Process proc = rt.exec("C:/chasen.exe");
         br = new BufferedReader(new InputStreamReader(in));
         int a = in.read();
         System.out.println(a);
         String res = br.readLine();
         System.out.println(res);     
         while(!res.equals(null))
         System.out.println(res);     
         res = br.readLine();
    proc.waitFor();
         proc.destroy();
    catch(NullPointerException npe)
         npe.printStackTrace();
    catch (Exception IOEx)
    IOEx.printStackTrace();
    But it doesnt work as my chasen.exe needs arguments from stdin.
    The way exe works is as follows.
    on command prompt>>
    C:/chasen.exe -j [ Need to press ENTER here]
    input statement . [ Need to press ENTER here too]
    Then output is displayed on command prompt which can be taken from InputStream of the process. But I m not able to properly run this exe itself with all the arguments frm Stdin

  • How to run geniatagger.exe from java

    Hi,
    i'm trying to run geniatagger-1.0.exe from java on windows vista, using Process pr = Runtime.getRuntime().exec("C:/geniatagger-1.0/geniatagger-1.0.exe");
    However, i keep on getting the same error. "Cannot run *.exe CreateProcess error=2" . I tried the same code with excel.exe which is installed in C:/ProgramFiles
    and it worked. I also tried to create a path variable C:/geniatagger-1.0/geniatagger.exe;%path% and then run the same java code. Yet, nothing has changed.
    I would really apreciate any help

    dalab wrote:
    Sorry, i didn't realise that the link was already sent! I found that earlier as well. I"I..." what?
    "I ...figured it out"?
    "I ... still need help"?
    You're not a vampire named Trever are you?

  • Call c++ program .exe from java

    Hello every body
    i have a problem while calling Hello.exe from java
    c++ code
    #include "stdafx.h"
    #include <iostream>
    # include <string>
    using namespace std;
    int main(int argc, char *argv[])
         int x;
         cout<<"Just test ";
    return 0;
    }java code:
    import java.io.IOException;
    public class runtime{
        public static void main(String[] args){
        try
        Process process = Runtime.getRuntime().exec("Hello.exe");
        }catch (IOException e)
         e.printStackTrace();
    }it display nothing!!

    alexxzius wrote:
    String line="";
    BufferedReader input =
    new BufferedReader
    (new InputStreamReader(process.getInputStream()));
    while ((line = input.readLine()) != null) {
    System.out.println(line);
    There is a danger of deadlock in this code. The OP should read the 4 sections of [http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html|http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html] and implement ALL the recommendations.

  • How to connect iseries green screen from java program

    how to connect iseries green screen from java program to get the data in the DB files ,here the DB is DB2/400

    Just some Friday fun. Use the telnet program that comes with Windows and supports VT escape sequences.
    import java.io.*;
    import java.net.*;
    public class AutoTelnet {
         private static Socket s;
         public static void main(String[] args) throws Exception {
              Thread t = new Thread() {
                   @Override public void run() {
                        try {
                             s = new ServerSocket(5555).accept();
                        } catch (IOException ex) {
                             ex.printStackTrace();
              t.start();
              Process p = new ProcessBuilder("cmd", "/C", "start", "telnet", "127.0.0.1", "5555").redirectErrorStream(true).start();
              t.join();
              PrintStream ps = new PrintStream(s.getOutputStream());
              ps.println("Screen will be cleared in 5 seconds");
              ps.println("5");
              Thread.sleep(1000);
              ps.println("4");
              Thread.sleep(1000);
              ps.println("3");
              Thread.sleep(1000);
              ps.println("2");
              Thread.sleep(1000);
              ps.println("1");
              Thread.sleep(1000);
              ps.println("\u001b[2J");
              Thread.sleep(5000);
    }

  • How to run Perl from Java

    Hi,
    I have a program written in Perl. I want to run the Perl program using Java. If there is a command which can allow me to call the perl program from Java program, please advise me.
    regards

    The most easy way is to use Runtime class to call "perl" to run your perl script.
    String sCmd = "c:\\yourPerlScriptLoc\\yourPerlScript.pl";
    Process p = Runtime.getRuntime().exec(sCmd);

  • How to terminate an exe from Java

    Hi Everyone
    Can anyone let me know the code to how to terminate an exe from Java
    I run an exe using
    Runtime.getRuntime().exec(dialog); //where dialog is a string (pointing to an exe)
    Now i need to kill this exe
    How do i do this in java
    Any help is highly appreciated
    Thanks!

    getafix14 wrote:
    I run an exe using Runtime.getRuntime().exec(dialog); //where dialog is a string (pointing to an exe)
    Now i need to kill this exe
    How do i do this in java
    ejp wrote:
    Process.destroy();
    And don't forget to take a swig of magic potion first.
    Winston

  • How to call 'WordPad' from java program........

    The following code will open 'Notepad'
    Runtime runt = Runtime.getRuntime();
    Process pr = runt.exec( "notepad" );likewise, I want to open 'Wordpad'
    Runtime runt = Runtime.getRuntime();
    Process pr = runt.exec( "wordpad" );The above two line code, compiles successfully, but it doesn't give the correct result.
    If I give 'wordpad' in start->run->'wordpad', in windowXP means, the Runtime executer opens 'WordPad',
    can anyone give the solution to open WordPad from Java Programming.
    thanks
    Sarwan_Gres

    Try this:
            Runtime runt = Runtime.getRuntime();
            Process pr = runt.exec("cmd /c start wordpad");

  • Setting windows environment variables from Java program

    Is there any way to set environment variables from Java program in Windows? Any help is appreciated.
    Here is my situation:
    I need to decrypt an encrypted Oracle user password in a batch file which will be used while running a sql script with sqlplus. I was planning to have bat file which will call a Java program decrypt the password and set it as an env variable in windows which will be available while calling sqlplus.
    thanks

    Runtime.exec has a lot of overloadings. Two of them
    allows you to specify the environment variables.
    exec
    public Process exec(String[] cmdarray,
    String[] envp,
    File dir)
    throws IOExceptionExecutes the specified command and
    arguments in a separate process with the specified
    environment and working directory.
    cmdarray - array containing the command to call and
    its arguments.
    envp - array of strings, each element of which has
    environment variable settings in format name=value.
    dir - the working directory of the subprocess, or null
    if the subprocess should inherit the working directory
    of the current process.
    I had this sample program:
    public class SetVarExample {
    public static void main (String[] args) throws Exception {
         String[] cmd_env= new String[] {"password="+"ABCD","Path=C:\\Sun\\AppServer\\jdk\\bin"};
         String cmd = "cmd /c SET ";
         Runtime.getRuntime().exec(cmd,cmd_env);
    System.out.println( "Finish ...." );
    I tried it in a command prompt. But looks like when the program exits, it's a whole new process and so it does not retain the env variables set in the java program.
    Any suggestions? Am I doing it worng?
    thanks

  • How to edit the existing data in the XML file from java programming.

    Hi all
    i am able to create XML file with the sample data as below from java programming.
    i need sample code on how to edit the existing data in the XML file?
    for example
    <?xml version="1.0"?>
       <mydata>
               <data1>
                         <key1>467</key1>
                        <name1>Paul</name1>
                        <id1>123</id1>
              </data1>
              <data2>
                         <key2>467</key2>
                        <name2>Paul</name2>
                        <id2>123</id2>
              </data2>
        </mydata>
    i am able to insert the data in the XML.
    now i need sample code on how to modify the data in the above XML file from the java programming for only key2,name2,id2 tags only. the remaining tags data in the XML file i want to keep same data except for key2,name2,id2 which are i want to modify from java code
    Regards
    Sunil
    [points will be always rewardable]

    hi
    u need a parser or validate the xml file for to read the xml file from java coding u need for this
    xml4j.jar u can download this file  from here
    http://www.alphaworks.ibm.com/tech/xml4j
    or we can use the SAX(simple API for XML)
    some sample applications for this
    http://www.java-tips.org/java-se-tips/javax.xml.parsers/how-to-read-xml-file-in-java.html
    http://www.developertutorials.com/tutorials/java/read-xml-file-in-java-050611/page1.html
    http://www.xml-training-guide.com/e-xml44.html
    let me know u need any other info
    bvr

Maybe you are looking for