Unix and dos commands from Java

How can I issue a unix command from a java program eg: setenv
help would be appreciated

setenv is a C shell builtin command. And it wouldn't make any sense to execute setenv from Java anyway, because the environment variable changes would only apply to that shell and possibly (if the variable is exported) to subprocesses.
But look at the java.lang.Runtime.exec() method and the Process class.
(http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Runtime.html)
Be careful of input and output to the commands. If the command produces output and you don't read it from the getOutputStream(), your command will hang forever when the OS stdio buffer fills up. This can cause terrible random hangs. Search the forums for more on how to handle input and output. You'll probably need multiple threads to read/write the different streams. (stderr, stdout, stdin)

Similar Messages

  • Dos command from java

    hi all
    how do i run dos commands from java??????????

    Using Runtime#exec().
    Also see http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Runtime.html

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

  • Invoking dos commands from java

    hi all,
    I just want to invoke dos prompt from java.I know it can be done using "Runtime.getRuntime()" command.But when i try to create a directory inside another directory,how can i specify the path of the destination directory.i googled as well as tried different things.But i failed to make it up.Please help me solve this problem.
    thanks in advance
    Regards

    Hai,
    Thanks for your respond.
    If I give "cmd" command only, I am getting dos command window, but I didn't get the prompt like
    c: or d:
    How can I do it?
    Expecting more helps
    Joseph

  • 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

  • Error using DOS Command from Java

    Hi,
    I am using the following code to execute the DOS command to delete a file from Java.
    cmd = "del " + fileName;
    Runtime run = Runtime.getRuntime();
    Process proc = run.exec(cmd);
    Getting the following exception during runt time.
    Exception = java.io.IOException: CreateProcess: del D:\LAWDOCS\P50074\12\3\36857.3  error=2 --
    Can some body throw light on this?.
    Thanks,
    Jeyaraman R

    "del" is not an actual executable file, but instead a command in the command interpreter. So you can't just execute del like that, you have to run the command interpreter and tell it to execute the del command.
    If you are on windows NT, 2000 or XP, replace your command with "cmd /C del " + filename.
    If you are on Windows 95, 98 or ME, use "command /C del " + filename. (I am not quite sure if the switch for this is called /C on old windows version.)
    (But why not use File.delete()?)

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

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

  • Executing a dos command from java in windows nt

    i need to execute the
    " net send"
    command from the command line in windows nt, from within my java program... is there any way that i can execute cmommands in the dos command shell? or rather, does anybody know a way i can send a message ( in windows ) from java, from one computer to another over the lan?? I cannot have a java programming running on all the other computers, so i cannot make my program serve, it must just send the messges using a lan command....
    (the net send command just sends a message in the form of a pop up box on the receiver's screeen)
    thanks a bunch guys!!!
    Thanks a bunch guys!!!!

    you can do that with exec() in Runtime.
    Runtime rt = Runtime.getRuntime();
    Process proc = rt.exec(command here);but before you do that you need to read this:
    http://www.javaworld.com/jw-12-2000/jw-1229-traps.html
    hth
    partha

  • Running DOS command from JAVA

    Hi ,
    I would appreciate if anyone could tell me how to run DOS command such as "del" using JAVA language .Thank you.

    <steps onto soapbox>
    Surely for something like 'del' we should be advocating a non-OS specific method so we don't lose sight of Java's cross platform abilities.
    If it has to run an OS specific thing fine, but please look for a non OS specific solution first.
    <steps off soapbox>

  • How can I run a dos command from java on windows 98, 95?

    The usage of cmd.exe in the java program to run a dos command on windows 98. 95 doesn't work as the equivalent command is command.exe
    But using the command.exe in the java program makes my program to hang.

    hi,
    As u mentioned, u cannot use the cmd.exe in win9x environment as cmd.exe is specific to windows NT, you can use the command.exe without any hitches.
    for eg
    java.lang.Runtime.getRuntime().exec("start command /K a.bat"); should run the batch file a.bat..
    if the problem persists, try posting the snippet of code that you are using.
    cheerz
    ynkrish

  • Use several DOS command from java program

    Using "Runtime.getRuntime().exec(toto.bat)" I have no problem to run a Command in my DOS window...
    But the problem appear when I want to call another command in this same window from myApp.java...
    In fact after executing the first command I find no way to have another access to this DOS window...
    Is there any way to set the Process created by calling exec() method as the default system??
    Hope my explications are clear...
    Does someone have the solutions...this will help me so much...
    bilbou.

    ok but I have found no forum which resolve my problem...can you give more details about what you think concerning the solution of my pb...
    bilbou.

  • Executing a dos command from java

    Hi guys.
    This line works fine:
    process = Runtime.getRuntime().exec("command.com /C c:\\test.bat");
    But this line do nothing:
    process = Runtime.getRuntime().exec("command.com /C c:\\test\\test.bat");
    I need to run a dos file in some directory in my HD. It works fine in root, but do nothing when I change to directory. I do this from an applet.
    Any ideas/suggestions?

    Any ideas/suggestions?The file isn't actually there (it isn't in the directory.)
    There is a permisions problem (which is unusual but not impossible on windows.)
    The script itself is failing - you didn't indicate "how" it doesn't work so this is possible.

  • Problems to execute "cd" DOS command from Java program

    Anyone try to exec command "cd.." or "cd directory", it doesn't have any action. No exceptions, just don't do anything.
    Runtime.getRuntime().exec("cd\\");
    Runtime.getRuntime().exec("cd hello");
    However, when I try the following, it is working fine. Any ideas???
    Runtime.getRuntime().exec("explorer");
    Runtime.getRuntime().exec("javac Test1.java");

    That's because cd is built into the command
    interpreter in Windows. Runtime.getRuntime().exec()
    can only be used to launch external programs. This
    article should get you going:And that's not the only reason why you can't do this.
    When you do
    Runtime.getRuntime().exec("cd c:\\");
    (or, heck, "cmd /c cd c:\\"): the new command that you spawn will cd to the destination specified, and then exit. The parent process (your java program, that is launching this command) won't go anywhere - the "cd" doesn't affect it.
    The only way to change your OS directory in Java is to invoke native code. And even then, the effects are undefined (in terms of how your CLASSPATH will be affected, etc.).

  • 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

Maybe you are looking for

  • Saving a file in a DB

    Hi all I need to store a file in a MS Access db; I think the best way is to use a BLOB, but I don't find anything regarding how to use in MS, only in Oracle. Anyway, here under is my code: with this code I arrive to store a binary data into a db, but

  • PO line cancellation issue

    12.0.6 there is a requisition line of 500 autocreated into a PO line of 500 300 have been receiveed and that its decided that balance 200 is not needed any more from this supplier thereby the quantity of 200 is needed back in the auto create pool to

  • LIS showing incorrect UOM

    Hi all, I have created a new info structure to show billing data. We are using two unit of measure EA and CS in sales transactions. Now the LIS report is just adding the quantity of EA and CS and doing the total for billing qty. Is there any way by w

  • Windows 7 - Large Pages

    While I was performing some benchmarks on my W520, I became aware that there is a function in Windows 7 called Large Pages. Essentially setting this policy for either a single user or a group greatly reduces the TLB overhead when translating memory a

  • View Broken Movie Files?

    Hi All I was testing a Automator workflow which eventually I want to build into a full program. Is there anyway to view Broken Quicktime Movie files which anyone knows of. I had a movie being recorded by Quicktime but for some reason or another there