Execute System Cmd

Pls pardon my igorance ... how do I execute a system (Solaris 8) command from Java? In PERL, we can use the "system" command or grave accent "`" ... what's the equivalent in java? Thanks for your help.

How do I pass arguments into the program then? I have tried the following, but it failed. It seems that the arguments are not passed to the program. For example, I want to execute the following command "program -v uid bochap". Here's is my sample code ..
String cmd = new String(program);
String[] argv = new String[3];
argv[0] = "-v";
argv[1] = "uid";
argv[2] = "bochap";
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(cmd,argv);
Thanks for your help again.

Similar Messages

  • Execute System Cmd as another user

    Basically, I am using Runtime.exec(String cmd, String[ ] envp, File location) to execute a system command. However, only a certain user is allowed to execute that command. How can I execute that command in Java as another user? Thanks for any advice.

    I do not believe you can, since rights of that type are system level granted. You can make groups that have only execution rights to that object.

  • How to Execute a cmd file in my ps script?

    Hi All,
    I have a cmd file which calls an msi and passes paramters. I am calling this deploy.cmd file from a powershell script. How can i achive this? I am trying the following but it does not work.
    Invoke-Command {&
    "D:\deploy\ABC_Installed\ETRM
    (QA)_1.0.7.2\en-us\ABC_QA.cmd"}
    It tends to just print the contents of the cmd but does not execute it. So, is it possible to execute a cmd
    from with in PS, if so, how?
    Walk_Man... Share the joy... 'Propose as Answer' if the post helps.

    Hey mate,
    I get the following error message when i run this,
    $proc
    =
    [diagnostics.process]::Start("cmd.exe",
    "/c"D:\deploy\ABC_Installed\ETRM
    (QA)_1.0.7.2\en-us\ABC_QA.cmd")
    Missing ')' in method call.
    At :line:12 char:53
    + $proc = [diagnostics.process]::Start("cmd.exe", "/c"D <<<< :\deploy\ABC_Installed\ETRM (QA)_1.0.7.2\en-us\ABC_QA.cmd")
    Walk_Man... Share the joy... 'Propose as Answer' if the post helps.

  • How to execute SQOOP.cmd file from SSIS execute process task?

    How to execute SQOOP.cmd file from SSIS execute process task?
    What is argument need to be prefixed in the arguments property
    Executable: C:\Hadoop\sqoop-1.4.2\bin\sqoop.cmd
    Arguments: /C "sqoop import -connect "jdbc:sqlserver://mysystem:1433;database=test;username=myuser;password=mypwd;" -table emp  -target-dir /user/emptable -m 1"
    Thanks
    Durga

    Hi Durga,
    SQOOP.cmd must be no different than any other .cmd/.bat I assume, your question is more about setting the parameters up I guessed.
    So unless the arguments change dynamically you can call SQOOP.cmd from yet another .cmd that has its parameters passed to it and the former you simply set to run without the arguments in Execute Process Task.
    Otherwise, in general to set a bat/cmd file to run in SSIS one needs to do this:
    PS: It is a good idea to populate the working directory with the path to the .cmd
    Arthur My Blog

  • How to execute a .cmd file in Solaris server

    Hi all , How to execute a .cmd file in Solaris server.
    Thanks.

    Basically you should have the execution flag associated to this cmd file. Check first if this file is builded for Solaris and then you can add the "x" flag to the file permissions.
    You can too run it like this, but you should also check if this file is Solaris compatible : # sh <path/cmd file>

  • Execute a .cmd file?

    Is it possible to execute a .cmd file using a button or some other means?
    We have a .cmd file that executes sqlloader to reload a table with new data from a flat file and would like to run this .cmd file from an apex application.
    TIA.

    Hi Pixie,
    About all I know about java is that I hate it. However, this is actually one of those rare java apps that is actually useful and easy.
    Just follows the steps on that web page, with a couple of small edits, and your done. You can then create the button i n your Apex app which performs the process and executes the shell script.
    The only gotcha I've had with this was when I upgraded to ver 11 (maybe 10?). Oracle had changed the security for the database, so I needed to change how Oracle started, was owned, etc. so that I could define the proper environment variables for when the shell script executed. I did all the steps as the owner of the schema my Apex app uses, so all of the internal permissions were correct.
    Bill Ferguson

  • Executing system command in java

    I am trying to set ORACLE_SID value from java application.
    when i echo $ORACLE_SID it shows me the old sid value
    please help
    my code --->
    import java.util.*;
    import java.lang.*;
    import java.io.*;
    public class runSysCommand{
         public static void main(String args[]){
         String osName=System.getProperty("os.name");
         System.out.println("the OS name is :" +osName);
         String s=null;
         String[] cmds = {"/bin/ksh","-c","export ORACLE_SID=pd3"};
    try
    Process p = Runtime.getRuntime().exec(cmds);
    BufferedReader stdInput=new BufferedReader(new
    InputStreamReader(p.getInputStream()));
    BufferedReader stdError =new BufferedReader(new
    InputStreamReader(p.getErrorStream()));
    //read the output
    System.out.println("here is the op:\n");
    while((s=stdInput.readLine())!=null){
    System.out.println(s);
    System.out.println("here is the error if any");
    while((s=stdError.readLine())!=null){
    System.out.println(s);
    System.out.println("the OS name is :" +osName);
    System.exit(0);
    }//end try
    catch (IOException e)
    System.out.println("exception Happened" );
    e.printStackTrace();
    System.exit(-1);
    }

    When you execute "/bin/ksh," you get a new shell - with a new environment. When you close the shell, it's environment (and any changes) go away. What you will need to do is write a shell script that takes the value as a parameter - the script then sets the environment and runs whatever other program you want to specify. When the script exists, the environment will also disappear.

  • Executing System commands

    Hello,
    I have a scnerio where I have to execute a Shell command like
    "ping 192.168.0.26" but I think in Java 1.4 the System.execute() method is
    deprecated. I used the Runtime command like this
    Process p;
    p = Runtime.getRuntime.exec("ping 192.168.0.26");but it shows me nothing on the console window. I want the output to be in the console window. How can I do that???

    Now I here is what I was trying before
    import java.io.*;
    public class test{
         public static void main(String[] args){
              Process p;
              String cmd = "ping 192.168.1.1";
              OutputStream out;
                           try{
                                 p = Runtime.getRuntime().exec(cmd);
                                   catch(IOException e){
         e.printStackTrace();
    }What else should be added to it

  • How to execute system command from java program

    Hi all,
    I want to change directory path and then execute bash and other unix commands from a java program. When I execute them separately, it's working. Even in different try-catch block it's working but when I try to incorporate both of them in same try-catch block, I am not able to execute the commands. The change directory command works but it won't show me the effects of the bash and other commands.
    Suggestions??

    The code I am using is....
    try
    String str="cd D:\\Test";
    Process p=Runtime.getRuntime().exec("cmd /c cd
    "+str);your str string is already having cd in it but again you ar giving cd as part of this command also please check this,i will suggest you to remove cd from str
    Process p1=Runtime.getRuntime().exec("cmd /c mkdir
    "+str+"\\test_folder");you should say mkdir once you change your path,but here you are saying mkdir first and then cd D:\Test(this is because of str)..please check this
    Process p2=Runtime.getRuntime().exec("cmd /c bash");
    Process p3=Runtime.getRuntime().exec("cmd /c echo
    himanshu>name.txt");
    catch(IOException e)
    System.err.println("Error on exec() method");
    e.printStackTrace();
    Message was edited by:
    ragas

  • How to execute a .cmd file in java

    I can execute an .bat or a .exe file.
    But .cmd file is not getting executed in java.
    Pls tell me how to do this

    You can solve this problem by using the following method:
      private String[] getEnvironmentVariables() {
        final Map<String, String> env = System.getenv();
        final String[] result = new String[env.size()];
        final StringBuilder buf = new StringBuilder(100);
        int i = 0;
        for (Entry<String, String> e : env.entrySet()) {
          buf.setLength(0);
          result[i++] = buf.append(e.getKey()).append('=').append(e.getValue()).toString();
        return result;
      }You just have to execute your command like this now:
    Runtime.exec("cmd /C " + getMyCommand(), getEnvironmentVariables(), getMyExecDirectory());The reason why "start" didn't work is, that it is a command executed in a shell, thus you need "cmd /C" to execute start in the cmd. So you can do this as well:
    Runtime.exec("cmd /C start " + getMyCommand(), getEnvironmentVariables(), getMyExecDirectory());If you want to to execute "myCommand" in an own process.
    Kind Regards,
    Stefan Schubert

  • Executing system commands on Linux

    Hi,
    I am trying to copy a file from Java code on Linux platform using the cp command.
    Process p = Runtime.getRuntime.exec("bash -c \"cp file1 file2\"");
    The process is exiting with exit code 2 and the file is not getting copied.
    The same command when executed directly from the terminal window (command console of Linux) is working fine and the file is getting copied.
    I am using RedHat Linux 7.1. Any ideas plz...
    best wishes
    Issac

    try splitting up the args into an arary
    import java.io.*;
    public class CP {
      public static void main (String [] args) {
        try {
          String [] cmd = {"/bin/sh","-c","cp file1 file2"};
          Process p = Runtime.getRuntime().exec(cmd);
          p.waitFor();
          System.out.println("exit value " + p.exitValue());
        } catch (Exception e) {
          System.out.println("err : " + e);
    }

  • Executing system commands and capturing results

    How do I get the result of executing a system command using Runtime? For example, if I execute "cmd /c dir", how do I get to the result of the dir command from the Process that is returned?

    Process p = Runtime.getRuntime.exec("...");
    InputStream in = p.getOutputStream();
    // now read the "in" stream just as you would any
    other stream.Slight typo getRuntime(), but it's close...

  • Executing system commands like cd on Linux from Java Code

    Hi,
    I need to execute a few system commands (like cd, ls, tar, etc.,) on RedHat Linux 7.1 from Java code. Any ideas plz.
    best wishes,
    Issac

    Since this thread mentioned using the DOS START command
    from an exec(), I was wondering whether anyone else is
    seeing the odd behavior I am.
    Most of the examples showing how to read stdout and stderr
    while the process is running show simple loops that use
    getInputStream and getErrorStream and do read's until they
    get back -1. Most of these examples are insensitive to the
    state of the Process.waitFor that happens elsewhere and sort
    of rely on the -1 and the waitFor happening roughly together.
    Whenever I use this technique to launch an arbitrary DOS
    command (e.g. Runtime.exec("dir")), it works fine, and
    everything seems to terminate as expected.
    However, when my command is something like:
    Runtime.exec("cmd /c start my.bat")
    where I deliberately use the START command to fork off
    a different process, something odd happens.
    Those simple read loops get an initial burst of stdout/stderr
    bytes from the "start" part of the command. But then eventually,
    these loops do a "read" which blocks on those InputStreams. Now,
    even if my Process.waitFor returns (because the start returned
    immediately), those read loops stay blocked **until that script
    started by 'start' runs to completion**. It is like the child
    of the child is holding those streams open somehow.
    I found that by doing something like an InputStream.available() or
    a BufferedReader.ready( ) check before dropping into the read,
    and allowing the Process.waitFor completion to exit those read loops,
    them I get what I expect -- namely immediate return from the START,
    but apparently complete copies of stdout and stderr.
    Curiously, in the case where the reads block until the child process
    started by START terminates, they still don't get the child's/child's
    stdout and stderr. It is just a long duration thread block that
    yields no data.
    Can anyone explain what is happening to a
    Process.getInputStream( ).read()
    when the process immediately does a START and returns? Why would
    the read( ) stay blocked when the START finished launching a new child
    process?
    ... ron cain

  • Execute System Commands

    Hey all
    I need to write Java application that executes windows system commands. In the following example I tried to execute the command DATE /T but i got an exception. Could any one help ?
    import java.io.*;
    public class Shell {
        public static void main(String[] args) {
            String s = null;
            try {
                Process p = Runtime.getRuntime().exec("date /t");
                BufferedReader input = new BufferedReader(
                        new InputStreamReader(p.getInputStream()));
                System.out.println("Here's the standard output of the command: ");
                while ((s = input.readLine()) != null) {
                    System.out.println(s);
            } catch (IOException e) {
                e.printStackTrace();
    }

    hi,
    give command as "cmd /c date /t"
    to execute any dos commands using exec() method, you have to send those commands as parameters to the cmd.exe .
    regards,
    Mahender

  • The ForEach Loop Enumerator is Empty - SSIS Error when executing through CMD via VBA

    I am currently working on a SSIS package that is to be triggered through CMD via VBA (Access). All that the package does is read a bunch of files from a folder and upload them into a table in SQL Server. The tricky part comes is that this folder location from
    which the package is to read the files is dynamic.
    I figured out a way to make it dynamic, by using variables
    I have used the following variables:
    "Directory" against DIRECTORY in COLLECTION>>EXPRESSIONS in the ForEachfile Enumerator,
    "FilePath" (With Index 0) in the Variable Mappings
    PROBLEM : As the final link in the chain, I have used following to be triggered from VBA
    dtexec \f "C:\Desktop\SSISAppend.dtsx" /Set "\Package.Variables[User::Directory].Property[Value];C:\Desktop\AppendFiles"
    When I try to execute the package using the above command, it gives out the error
    Code : 0x8001C004
    Description: The For Each File Enumerator is empty. The For Each File enumerator did not find any files that meatched the file pattern, or the specified directory was empty.
    But, when I run the package from MS VisualStudio 2008, the package runs fine, takes the location of the file assigned to the variable "Directory". 
    All help is appreciated

    Where are you executing the package from? You're giving an aboslute path so this will work only if package as well as the folder is in the same machine from which you execute the above code. Otherwise you need to use UNC path.
    Another thing to check is what login is executing the command and you need to ensure it does have access to the directoty path as well as to the path where package resides.
    Please Mark This As Answer if it helps to solve the issue Visakh ---------------------------- http://visakhm.blogspot.com/ https://www.facebook.com/VmBlogs

Maybe you are looking for

  • Syncing 2 iPhones on the same iTunes Library

    Is it possible to sync to iPhones to the same iTunes library? So games etc get synced to both phones? BUT can both iPhones sync to the Music Library differently?

  • Do I need DiskWarrior?? Will it fix this problem?

    Hello and thank you for reading.  I have had issues with my Macbook Pro ever since I swapped out both the ram and hard drive.  I thought at first it was a RAM problem but now have been able to boot my mac from the original hard drive, mounted in an e

  • Itunes stops working when syncing

    I am changing from AT&T to Verizon iPhone 4S and everytime I plug either phone to sync with iTunes, iTunes stops working and shuts down.

  • Error in RFC Receiver Channel

    Hi Experts! In my scenarios i am using Receiver RFC for Look up. The RFC Channel is working fine in Dev server after we transport it to the QA. Now we are getting an error message :GenericRFCChannel" is not found in SXMB_moni. ID we have made the new

  • Multiple sms - please reply - help!!!!

    hello someone on the forum suggested that a text file can be sent through multiple sms and then reassembling them into a text file I'm not really sure how to do that please can you give some idea, reference or eamples... thanx Forum