Run External Program in Java

I need to run a VB batch in a class Java.
If someone can help me because I don't know the method.

It's OK but if I don't export the result on the standard output, the run doesn't work.
So I added this code :
DataInputStream dis = new DataInputStream( newProcess.getInputStream() );
String line;
while( (line = dis.readLine()) != null )
System.out.println(line);
Have you got another solution to do it or an explication on this ?
Thanks

Similar Messages

  • Running external program using java

    hi
    i am trying to run an external program using the runtime.exec() method. my problem is that the external program only runs when i press ctrl-c to exit my program. does anyone know how i can execute the external program while my program is still running without having to quit the program?should i be using threads?
    thanks

    As per the api doc exec will be executed as a seperate process
    Process exec(String command) ------Executes the specified string command in a separate process.
    Can you able to share that code what you have written ?

  • 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-

  • Help with running external programs within java

    Hi,
    I'm new here, and not sure whether i'm in the right place for this, but i thought i'd give it a shot anyway!
    Basically i have a fortran program already written, that takes in the name of a text file as an input. I'm wanting to create a user interface to create this text file, and then i would have a button or something to enable me to then run the fortran program and input the file all from the user interface, and get rid of the need to manually run the fortran program.
    Firstly, is this even possible?! Is it difficult to program and get to work?! Does anyone have any suggestions or guidance on this.
    Thanks

    @Op.
    ..and here are two links related to that.
    http://java.sun.com/developer/JDCTechTips/2003/tt0304.html
    http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html

  • How to execute external program in java?

    My question is how to execute an external program in java.
    I need to call a unix command in java.
    Thanks.

    it depends on what you are trying to do. Following are the two methods
    1. Runtime.exec() : this method allows you just to call an external program as a seperate process
    2. JNI (Native Interface) :- As of right now only C and C++ are supported by this method. This method allows you to directly call the C/C++ methods from JAVA

  • How to run native program with Java program?

    Hello
    I've got following problem. I'd like to write file browser which would work for Linux and
    Windows. Most of this program would be independent of the system but running programs not. How to run Linux program from Java program (or applet) and how to do it in Windows?.
    Cheers

    Try this:
    Runtime rt = Runtime.getRuntime();
    Process proc = rt.exec("ls -l");
    InputStream stream = proc.getInputStream();
    InputStreamReader isr = new InputStreamReader(stream);
    BufferedReader br = new BufferedReader(isr);
    String line = null;
    while ( (line = br.readLine()) != null) .....
    "if the program you launch produces output or expects input, ensure that you process the input and output streams" (http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html)

  • Run external programs

    Hi there..
    I need a example to run others programs from Java, for example lunch the explorer.. Is possible do that?
    Thanks you

    Have a look at the Runtime class and especially the exec method. You should be able to do what you want using the following code:
    Runtime.getRuntime().exec("explorer");

  • How to call a external program in java?

    Help!!
    Is there any method that can a java program can call a external program? For example execute a exe file.
    Thanks.

    Yes.
    Runtime.getRuntime().exec("exactly what you would type at the command line");
    But be aware that this is operating-system-specific and full of gotchas. When you run into one of them, come back to the forum and do a search, this is a frequent topic of discussion.

  • Run external programs using runtime class

    Okay, I'm experiencing a really annoying problem with java.lang.runtime
    I'm building a GUI that needs to run some external programs, via a button say. These generally produce a text file or something, so I don't need to stream the output or anything (at least I'm assuming I don't?). Should be very simple...
    So at the terminal (bash) I would type ./programName , and everything will run hunkey dorey.
    In my code then, natrurally, I write
    String cmd = "./programName";
    Process p = runtime.getRuntime().exec(cmd);
    But low and behold...nothing happens. What is going on here, and how do I get around it! ??
    (On windows incidentally, it's no problem at all and works absolutely fine. But when I go over to mac, which is what I need to use, I'm screwed - only adding to the annoyance!)
    Any help would be much appreciated as I have a deadline looming!!

    You need to 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 the recommendations. Failure to implement all the recommendations will cause you grief.
    P.S. The fragment of code you have posted shows that you have fallen for at least 4 of the traps.

  • Running External Program

    Hi All
    I really hope that some one can help ,e with my problem as i have no idea. OK i am calling an external program that outputs documents. This program uses control files and for each job there are 5 control files. Thus i call this program 5 times to get all my output documents. When i run this program from the command line then it works fine, however when i call it from my code i can see that it is working because my outputs are processed and i can see the program in the task manager. But it stops and does not continue to the next call, its like after this program does the processing then it just hangs. I dont know what is wrong and i cant get any errors in the logs either. I think that the problem either is with my java instalation as i had problems on the server and had to re-install java because someone installed multiple versions or that it is a jvm problem. Can some one please give some ideas of what could be the problem. My code looks as follow:
    Process p = Runtime.getRuntime().exec("d://cep//nt_engine_010_62 prodengine -controlfile=d://cep//document1.txt");
    p.waitFor();
    System.out.println(p.exitValue());

    You may want to give this code a try:
    ProcessBuilder pb = new ProcessBuilder( whatever_command );
          pb.redirectErrorStream();
          Process p = pb.start();
          InputStream is = p.getInputStream();
          BufferedReader br= new BufferedReader( new InputStreamReader( is ) );
          for ( String line = br.readLine(); line != null; line = br.readLine() )
            System.out.println( line );
          p.waitFor();

  • How to compile and run package programs in Java

    Hi,
    I want to know how to compile and run the package programs in Java using -d. instead of creating the package folder manually.
    eg:
    package Test;
    class test1
    public void disp()
    //Any code;
    I want to compile this without creating the folder ' Test ' manually. that is if we use -d with javac the Test folder will be created automatically. I need the format of -d
    could anyone please help me.
    Thanks in Advance ,
    Ambika

    My program Test.java in F:\Tomcat5\webapps\Ambika\WEB-INF\Classes. I compiled in the format below. I got like this. What should I do for this? But yesterday I compiled like this only, It compiled and the folder com\cert\Test.class is created. Today again I compiled the pgm after deleting the already created folder 'com\cert', I got the error like this.
    I've given my pgm and the thing I've got when I compiled it.
    Test.java
    package com.cert;
    public class Test
         public void display()
              System.out.println("Hai");
    F:\Tomcat5\webapps\Ambika\WEB-INF\Classes>javac -d F:\Tomcat5\webapps\Ambika\WEB-INF\Classes\Test.java
    javac: no source files
    Usage: javac <options> <source files>
    where possible options include:
    -g Generate all debugging info
    -g:none Generate no debugging info
    -g:{lines,vars,source} Generate only some debugging info
    -nowarn Generate no warnings
    -verbose Output messages about what the compiler is doing
    -deprecation Output source locations where deprecated APIs are u
    sed
    -classpath <path> Specify where to find user class files
    -cp <path> Specify where to find user class files
    -sourcepath <path> Specify where to find input source files
    -bootclasspath <path> Override location of bootstrap class files
    -extdirs <dirs> Override location of installed extensions
    -endorseddirs <dirs> Override location of endorsed standards path
    -d <directory> Specify where to place generated class files
    -encoding <encoding> Specify character encoding used by source files
    -source <release> Provide source compatibility with specified release
    -target <release> Generate class files for specific VM version
    -version Version information
    -help Print a synopsis of standard options
    -X Print a synopsis of nonstandard options
    -J<flag> Pass <flag> directly to the runtime system
    F:\Tomcat5\webapps\Ambika\WEB-INF\Classes>
    Plz help me.
    thanks in advance
    Ambika

  • Why i get error msg when i run my program? (java.lang.NoClassDefFoundError)

    i have compile and run my program. First time the program can run. But after that when i run, come out error messege as below:
    java.lang.NoClassDefFoundError: FormPoster (wrong name: search/FormPoster)
    at java.lang.ClassLoader.defineClass0(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:537)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:123)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:251)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:55)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:194)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:187)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:289)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:274)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:235)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:302)
    Exception in thread "main"
    what can i do? thank very much

    Hello,
    i have compile and run my program. First time theprogram can run.
    But after that when i run, come out error messegeas below:
    java.lang.NoClassDefFoundError: FormPoster (wrongname: search/FormPoster)
    Check your FormPoster class: the classname has to be
    the same as
    the file name and the class should be stored in its
    correct directory
    (the sub directory structure should reflect the
    package name).
    kind regards,
    JosYou should also check your clsspath if you are using packages.

  • Running perl programs through java.

    How do i run a perl program through java?
    arg.pl :
    while(<STDIN>)
    print;
    java program that I tried:
    import java.io.*;
    class runtime
        static Process p;
        static BufferedReader stdOutput;
        static BufferedWriter stdInput;
        public static void main(String args[]) throws IOException
            int i = 0;
            try{
                String cmd[] = new String[] {"perl","arg.pl"};
                p = Runtime.getRuntime().exec(cmd,null,new File("./"));
                stdOutput = new BufferedReader(new InputStreamReader(p.getInputStream()));
                stdInput = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
            catch(Exception ex){
                System.out.println("Unable to initiate Process : " + ex);
            stdInput.write("abcd\n");
            stdInput.write("1234\n");
            stdInput.write("efgh\n");
            stdInput.write("5678\n");
            stdInput.close();
            System.out.println(stdOutput.readLine());
            System.out.println(stdOutput.readLine());
            System.out.println(stdOutput.readLine());
            System.out.println(stdOutput.readLine());
    }This works well. But I need to read the output line by line as and when i give the standard input line by line to it.
    I tried the following :
    import java.io.*;
    class runtime
        static Process p;
        static BufferedReader stdOutput;
        static BufferedWriter stdInput;
        public static void main(String args[]) throws IOException
            int i = 0;
            try{
                String cmd[] = new String[] {"perl","arg.pl"};
                p = Runtime.getRuntime().exec(cmd,null,new File("./"));
                stdOutput = new BufferedReader(new InputStreamReader(p.getInputStream()));
                stdInput = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
            catch(Exception ex){
                System.out.println("Unable to initiate Process : " + ex);
            stdInput.write("abcd\n");
            stdInput.flush();
            System.out.println(stdOutput.readLine());
            stdInput.write("1234\n");
            stdInput.flush();
            System.out.println(stdOutput.readLine());
            stdInput.write("efgh\n");
            stdInput.flush();
            System.out.println(stdOutput.readLine());
            stdInput.write("5678\n");
            stdInput.flush();
            System.out.println(stdOutput.readLine());
            stdInput.close();
    }This doesn't work well. It just halts without any output.
    Can anyone help me?
    Edited by: Vijayakrishna on Dec 5, 2007 8:30 AM

    Let me be more specific.
    I want to use a class which will look something like this.
    import java.io.*;
    public class FindAnswer{
        Process p;
        BufferedReader stdOutput;
        BufferedWriter stdInput;
        public FindAnswer()
            try{
                String cmd[] = new String[] {"perl","arg.pl"};
                p = Runtime.getRuntime().exec(cmd,null,new File("./"));
                stdOutput = new BufferedReader(new InputStreamReader(p.getInputStream()));
                stdInput = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
            catch(Exception ex){
                System.out.println("Unable to initiate Process. Error: " + ex);
        public String getAnswer(String question)
            String answer = question;
            try{
                stdInput.write(question + System.getProperty("line.separator"));
                stdInput.flush();
                answer = stdOutput.readLine();
            catch(Exception ex){
                System.out.println("Exception. Error: " + ex);
            return answer;
        public void close()
            if (p != null) {
                try {
                    if (p.getInputStream() != null)
                        p.getInputStream().close();
                    if (p.getOutputStream() != null)
                        p.getOutputStream().close();
                    if (p.getErrorStream() != null)
                        p.getErrorStream().close();
                catch (Exception e) {
                    System.out.println("Exception in Closing Streams - " + e);
    }The perl program takes lot of initialization time. I dont want to run the program foreach question i have. I'll just create an object to this class and find and answer by passing question to the object's getAnswer method. Here, I dont know all the questions in the beginning itself. I'll know one by one only and I should find answer without terminating the process.
    Kindly help me in this.

  • AIR 2.6: Run external programs sequential (1 by 1)?

    Hi,
    I managed to run an external program in Adobe AIR (2.6). I'm using the NativeProcess for this. I would like to perform the following actions.
    Pseudo-code:
    execute program.exe
    this will return a port number
    set the port number as an environment variable (in windows)
    execute program.exe with several queries
    stop progam.exe
    The corresponding executables:
    C:\my_proj\program.exe start
    var portnumber:String = process.standardOutput.readUTFBytes(process.standardOutput.bytesAvailable);
    C:\WINDOWS\system32\cmd /c set port=portnumber
    C:\my_proj\program.exe query test1
    C:\my_proj\program.exe query test2
    C:\my_proj\program.exe stop
    My problem is the bold text in my source code underneath. I'm trying to wait till the first native process is finished and had returned its output. Once I have this output (=the portnumber) I can continue with the following action. And this is needed for all steps ... to execute them one by one.
    But "do {} while (process.running);" is not working. My AIR applications is getting stuck in this loop ... as if process.running is always true.
    Any idea how I can execute external programs sequential?
    This is my source code:
                var file:File = File.applicationDirectory;
                file = file.resolvePath("NativeApps");
                if (Capabilities.os.toLowerCase().indexOf("win") > -1)
                    file = file.resolvePath("C:\\my_proj\\program.exe");
                var nativeProcessStartupInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo();
                nativeProcessStartupInfo.executable = file;
                var processArgs:Vector.<String> = new Vector.<String>();
                processArgs[0] = "start";
                nativeProcessStartupInfo.arguments = processArgs;
                nativeProcessStartupInfo.workingDirectory = File.documentsDirectory;
                var process:NativeProcess = new NativeProcess();
                process.addEventListener(ProgressEvent.STANDARD_OUTPUT_DATA, onOutputData);
                process.addEventListener(ProgressEvent.STANDARD_ERROR_DATA, onErrorData);
                process.addEventListener(NativeProcessExitEvent.EXIT, onExit);
                process.addEventListener(IOErrorEvent.STANDARD_OUTPUT_IO_ERROR, onIOError);
                process.addEventListener(IOErrorEvent.STANDARD_ERROR_IO_ERROR, onIOError);
                process.start(nativeProcessStartupInfo);
                do {} while (process.running);
                file = file.resolvePath("NativeApps");
                file = file.resolvePath("C:\\WINDOWS\system32\\cmd");
                var nativeProcessStartupInfo2:NativeProcessStartupInfo = new NativeProcessStartupInfo();
                nativeProcessStartupInfo2.executable = file;
                processArgs[0] = "/c";
                processArgs[1] = "set";
                processArgs[2] = "port="+portnumber;
    public function onOutputData(event:ProgressEvent):void
                var process:NativeProcess = event.target as NativeProcess;
                var portnumber:String = process.standardOutput.readUTFBytes(process.standardOutput.bytesAvailable);
    public function onExit(event:NativeProcessExitEvent):void
                process.exit();

    Hello,
    The  "do {} while (process.running);" may take almost all of the resource, so other code was blocked, then the program seems no response.
    You can add the following code in the onOutputData, like:
    public function onOutputData(event:ProgressEvent):void
        var process:NativeProcess = event.target as NativeProcess;
        var portnumber:String = process.standardOutput.readUTFBytes(process.standardOutput.bytesAvail able);
        file = file.resolvePath("NativeApps");
        file = file.resolvePath("C:\\WINDOWS\system32\\cmd");
        var nativeProcessStartupInfo2:NativeProcessStartupInfo = new NativeProcessStartupInfo();
        nativeProcessStartupInfo2.executable = file;
        processArgs[0] = "/c";
        processArgs[1] = "set";
        processArgs[2] = "port="+portnumber;

  • Running another program from Java on OSX

    I have an application that allows the user to specify an external program to run using the Process class, they just have to xpecify the path.
    On OSX most of the apps are in .app packages with the contents hidden to the filechooser. What command do I need to run to start (for example ITunes) an application

    Thanks, this is very wierd but compard to other OSes seems to work

Maybe you are looking for

  • Unable to edit personnel number in bp transaction

    Hi experts, I have a employee which is created by an idoc from another system with number starting from 0H. When i go to this employee in BP transaction, if i go to identification tab and try to edit the personnel number, it is not editable. My requi

  • How do I revive my Ipod Touch 4g?

    Last time I used my ipod was yesterday at about 5pm, working completely normally. I went out from then and had the ipod in my pocket. I got home at 12 and noticed the Ipod was dead. I connected it to a dock incase it was just out of battery, left it

  • Automatic generation of PDF file from briefing book in OBIEE11g

    Hi all, Could anyone tell me if exist a way to generate PDF files from briefing books automatically in OBIEE11g? Thanks Annalisa

  • Can't create .jar file?

    Am trying to create a .jar file, but without any luck? My classpath is set like this: C:\j2sdk1.4.2\bin; And am trying like this: cd C:\Documents and Settings\aslan1\Skrivebord\retroguard-v1.1\retroguard-v1.1\Regnskab1.5.1\ jar cvf Aslan.jar *.class

  • Adobe X goes into "Flattening" when printing and gets stuck at 6%, why?

    When attempting to print some PDF's Acrobat X goes into "Progress" and sticks on 6% "Flattening" and does not print. when clicking on the programme it says "Not Responding". Can anyone help with this please.