Execute DOS command inside java

I have a java program that reads each new user from an inputfile and writes that same user to an output file. The program works fine and is shown below:
import java.io.*;
public class FileStreamsTest {
public static void main(String[] args) {
try {
File inputFile = new File("newbousrs.txt");
File outputFile = new File("outagain.txt");
FileInputStream fis = new FileInputStream(inputFile);
FileOutputStream fos = new FileOutputStream(outputFile);
int c;
while ((c = fis.read()) != -1) {
fos.write(c);
fis.close();
fos.close();
} catch (FileNotFoundException e) {
System.err.println("FileStreamsTest: " + e);
} catch (IOException e) {
System.err.println("FileStreamsTest: " + e);
What I would like to do after each write is to execute a DOS command that looks something like this: supervsr.exe -USER username -PASS password -IMPORTUSERS importfile.txt
The DOS command above will add each user to the system I want to as the loop goes through each user record.
How do I execute the DOS command above inside java?

Tried what but still having a problem...here is my code:
import java.io.*;
public class ReadSource {
public static void main(String[] arguments) {
try {
FileReader file = new FileReader("newbousrs.txt");
                              //FileWriter letters = new FileWriter("outagain.txt");
                              //PrintWriter pw = new PrintWriter(new FileWriter("outagain.txt"));
BufferedReader buff = new BufferedReader(file);
                              Runtime rt = Runtime.getRuntime();
boolean eof = false;
while (!eof) {
String line = buff.readLine();
if (line == null)
eof = true;
else
System.out.println(line);
                                             Process proc = rt.exec("\\Blowfish\\droot\\Program Files\\Business Objects\\BusinessObjects 5.0\\supervsr.exe -USER xxxxx -PASS yyyy -IMPORTUSERS \\Blowfish\\droot\\accsp\\public\\newusers\\newbousrs.txt");
                                             //System.out.println(line.substring(4, 10));
                                             //System.out.println(line.substring(3,line.indexOf(',',3)));
buff.close();
} catch (IOException e) {
System.out.println("Error -- " + e.toString());
There error I get is this:
C:\jakarta-tomcat-4.0.3\webapps\webdav\WEB-INF\classes\accsp>java ReadSource
NU,Public,Steve Mcnealy,Steve Mcnealy,U,N,N,Y,N,N,PC,F,Y,N,N
Error -- java.io.IOException: CreateProcess: \Blowfish\droot\Program Files\Busin
ess Objects\BusinessObjects 5.0\supervsr.exe -USER accsp -PASS fish -IMPORTUSERS
\Blowfish\droot\accsp\public\newusers\newbousrs.txt error=3

Similar Messages

  • How to execute dos command in Java program?

    In Perl, it has System(command) to call dos command.
    Does java have this thing?
    or any other way to execute dos command in java program?
    Because i must call javacto compile a file when the user inputed a java file name.

    Look in the Runtime class, it is implemented using the Singleton design pattern so you have to use the static method getRuntime to get its only instance, on that instance you can invoke methods like
    exec(String command) that will execute that command in a dos shell.
    Regards,
    Gerrit.

  • EXECUTE DOS COMMANDS WITH JAVA

    I'm new to java and I want to execute dos commands by java
    can someone help me by by anyway?
    like tell me the packages or methods I need
    or give me links to sites?
    thanks

    No Arguments:
    try {
            // Execute a command without arguments
            String command = "ls";
            Process child = Runtime.getRuntime().exec(command);
            // Execute a command with an argument
            command = "ls /tmp";
            child = Runtime.getRuntime().exec(command);
        } catch (IOException e) {
        }With Arguments:
    try {
            // Execute a command with an argument that contains a space
            String[] commands = new String[]{"grep", "hello world", "/tmp/f.txt"};
            commands = new String[]{"grep", "hello world", "c:\\Documents and Settings\\f.txt"};
            Process child = Runtime.getRuntime().exec(commands);
        } catch (IOException e) {
        }

  • Execute dos commands through java

    Hi,
    Im trying to execute dos commands through java like
              try {
                   java.lang.Runtime.getRuntime().exec("cmd /c cls");
              }catch(IOException e){
                   System.out.println(e.getMessage());
              }Not sure if its possible? however
    open notepad would work
              try {
                   java.lang.Runtime.getRuntime().exec("notepad");
              }catch(IOException e){
                   System.out.println(e.getMessage());
              }Im trying to execute a cls commands to clear screen but without luck.

    The question is, which shell do you want to clear?
    I don't really know, but it could be that Runtime.exec executes its command in a new shell window...

  • How to execute DOS command in Java?

    I want to execute a dos command in Java,such as execute test.bat command? How to realize it?
    Thanks in advance!

    http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
    I found this article really useful in solving this. Hope it helps.
    Cheers,
    John

  • Execute DOS command from java application

    Hello,
    I want to execute a DOS command (MOVE, in order to move an image from a folder to an other) from a java application. How can I do this?
    Francesco

    Yes I have tested it and it is working but only when executing a bacth file. For instance:
    Runtime rt = Runtime.getRuntime();
    try{
         Process proc = rt.exec("move.bat");
    }catch(Exception ex){
         ex.printStackTrace();
    }and the command in move.bat is:
    move c:\\temp\\*.gif C:\\temp2
    You don't have to use double slashes in batch files, only in Java. But anyway it is working both ways.
    It is not working when you try to execute the command without the batch file:
    Process proc = rt.exec("move c:\\temp\\*.gif C:\\temp2"); -> this will not work.
    It should work. Try to execute another command to see what happens.

  • Execute DOS command from Java Code

    Hi All
    I am developing a Java application where I am launching some external Windows application on click event of a button.
    I am able to launch that application, but now I have to keep a check that if once that application is launched on clicking the button, then next time it should not launch. But if user exits that external application, then on clicking the button that application should be launched. I tried several ways but i am not able to keep track of that process which makes that application run. The major problem is that when that application is being launched and running, i don't have anything to read or write to that process's Input Stream or Output Stream. Can any one help me out in this????

    ApratimSharma wrote:
    You Might not get anything in the inputstream.read but when the external application will exit you will definitely get EOF (int -1). However Important thing here is that inputstream.read is a blocking call so better do it in a separate thread.
              Runtime runtime = Runtime.getRuntime();
              try {
                   Process process = runtime.exec("calc");
                   InputStream inputStream = process.getInputStream();
                   if (inputStream != null) {
                        System.out.println("Running");
                   while (inputStream.read() != -1);
                   System.out.println("Exited");
              } catch (Exception e) {
                   e.printStackTrace();
              }Now that is what I call 'stating the obvious' !
    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 ALL the recommendations.
    Edited by: sabre150 on Oct 8, 2009 10:00 AM

  • Help with executing windows command in java

    hi, i am trying to execute dos command in java. currently i am trying simply to create a folder in the current directory.
    here is the code snippet:
    try {
    Process p = Runtime.getRuntime().exec("md myFolder");
    } catch (IOException io) {
    io.printStackTrace();
    i have also tried using "mkdir" instead of "md". but my code just throws an exception that says:
    java.io.IOException: CreateProcess: md myFolder error=2
    what have I done wrong? thanks in advance

    try {
    Process p = Runtime.getRuntime().exec("cmd /c
    mkdir myFolder");
    catch (IOException io) {
    io.printStackTrace();hey! that code worked out perfectly. so "cmd" is needed to run dos commands? i searched through the net, but most of the examples that i were able to find did not include "cmd".
    if it wouldn't be too much trouble, what is the option "/c" for?
    i would also like to add that "mkdir" also works. The problem was the missing "cmd /c". Thanks guys for your help! I really appreciate it.

  • How to execute Dos Command 'Pause' from Java ?

    How to execute Dos Command 'Pause' from Java ?
    I have read the article in javaworld for Runtime.exec() anomalies.
    Can someone please give an insight on this?

    Thanks Buddy!
    That was very useful. Even though its a simple
    solution, I never thought about that.Bullshit! Reread reply #7 of http://forum.java.sun.com/thread.jspa?threadID=780193

  • Execute a DOS command in java

    Hi all,
    Is it possible to execute a DOS command in java? If so, how do I do that?
    Also, if it is a Linux machine, may I execute a Linux command in java?
    Thanks for your help.
    Regards,
    Bernard

    First, define 'command'.
    You can run external programs with Runtime.exec (eg. Runtime.getRuntime().exec("notepad");) but there are a few drawbacks regarding that method.
    http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html

  • Can we execute DOS Commands from a Java Application

    I just want to know whether we can execute DOS Commands from a Java Application.
    Thanks.

    hi,
    try this:
    Runtime.getRuntime().exec("cmd /c start abc.bat");
    andi

  • Execute DOS command in current window!

    All:
    Here is my code :
    import java.io.*;
    public class BuildScript {
    public static void main ( String[] args ) {
    String[] command = {"C:\\winnt\\system32\\cmd.exe", "cls"};
    try {
    Process process = Runtime.getRuntime().exec ( command );
    process.waitFor();
    } catch ( InterruptedException e ) {
    e.printStackTrace();
    System.exit(-1);
    } catch ( IOException e ) {
    e.printStackTrace();
    System.exit(-1);
    System.out.println ( "DONE!" );
    I just want to exeute some simple DOS command from Java application. And then I run this code, it hangs there for ever. I comments out "process.waitFor()", then it print out the "DONE" however it seems that it just open another console and clear that one and exit. For the main console I start my code, nothing happened.
    Can anyone help me out how to execute the dos command in current console window.
    Thanks

    Can anyone help me out how to execute the dos command in current console window.Sorry to say but there is no way to do that.
    The best way to clear the screen is to print a few hundred newlines.
    Trust me, this is a frequently asked question.

  • Run dos command from java

    May I ask how I should write if I want to execute a dos command in java? The command is :
    "C:\Program Files\Real\realproducer\rmeditor -i abc.rm -d abc.txt". Should I use array?
    Thanks!

    why an array ?
    String command = "C:\\Program Files\\Real\\realproducer\\rmeditor -i abc.rm -d abc.txt";
    Process p = Runtime.getRuntime().exec( command );what about the seach function on the left side ? ;-)
    tobias

  • DOS command from java applet

    Hi, I need to perform a DOS command from Java (using WFC if needed)...
    I have no idea, so please help!
    Thank you.

    OH OH OH WAIT! It is an Applet! i think there will be some security issues here! Sure enough you can't execute commands on the system from an applet unless you make some changes on the java.policy files on the client machine (that means, if your applet is in a web page, every machine which view your page).

  • Problem in Execution of DOS Commands with JAVA.

    I am trying to execute DOS Commands and FTP commands using Java Programming Language and Trying to get the output for further processing.Actually I want the exact output what DOS and FTP give after execution of a their corresponding commands.

    Process p = new Process("dir");
    InputStream is = p.getInputStream();

Maybe you are looking for

  • Animated .GIF in Fireworks

    Hello everyone, I'm trying to train myself with Fireworks so please bare with me.  I'm attempting to make an animated .gif and my question is: Is there a way to insert each image to automatically fit the canvas (100 pixels X 100 pixels)?  Currently I

  • Repeated and lost samples with NI-CAN

    I am using LabVIEW 2009 SP1 with NI-CAN 2.7 and a series 2 CAN card on a PXI system running LabVIEW RT. I sync'ed my DAQmx AI measurement with a NI-CAN task using the "CAN Sync Start with DAQmx". The Start is now sync'ed. As for the samples, I create

  • Need help for total of Dr and Cr. in report painter

    Hello friends,     I have created the trail balance in report painter. In table faglflext table. I created the common column of debit and credit cumulative balance and then separated the problem is I am not getting the total of all the debit and cred

  • Single click in file open dialogs in KDE4.1

    Hi, In my KDE 4.1, there's something that doesn't seem right: If in system settings, mouse&keyboard, I set the option "Double-click to open files and folders", then it doesn't actually  listen to that setting. In a file open dialog, it opens a file a

  • Faces not working with b/w photos?

    Trying to add faces/names to historical old black & white photos, but when any b/w photos selected screen goes blank. Is the dislike of b/w photos a known 'feature' of iPhone 9?