Help! How to call Unix system command?

In my java application.there are some code as following:
try{
String cmd = "ls -a > temp.txt";
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(cmd);
int rc = process.exitValue();
if (rc == 0){
System.out.println("OK!");
else{
System.out.println("Failed!");
catch(Exception ex){
ex.printStackTrace();
But when i run the application, it always throws IllegalTreadStateException,
return code(rc) is 2, exception's content is as below:
java.lang.IllegalThreadStateException: process hasn't exited
at java.lang.UNIXProcess.exitValue(UNIXProcess.java:118)
at MyApplication.main(...)
If someone know what happened, please help me, my E-mail address is:
[email protected]

import java.io.*;
public class RunCommand {
public static void main(String args[]) {
String s = null;
try {
     // run the Unix "ps -ef" command
Process p = Runtime.getRuntime().exec("ps -ef");
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new
InputStreamReader(p.getErrorStream()));
// read the output from the command
System.out.println("Here is the standard output of the command:\n");
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
// read any errors from the attempted command
System.out.println("Here is the standard error of the command (if any):\n");
while ((s = stdError.readLine()) != null) {
System.out.println(s);
System.exit(0);
catch (IOException e) {
System.out.println("exception happened - here's what I know: ");
e.printStackTrace();
System.exit(-1);
http://www.devdaily.com/java/edu/pj/pj010016/index.shtml
Cheers,
Bala

Similar Messages

  • How to call Operating System commands / external programs from within APEX

    Hi,
    Can someone please suggest how to call Operating Systems commands / external programs from within APEX?
    E.g. say I need to run a SQL script on a particular database. SQL script, database name, userid & password everything is available in a table in Oracle. I want to build a utility in APEX where by when I click a button APEX should run the following
    c:\oracle\bin\sqlplusw.exe userud/password@database @script_name.sql
    Any pointers will be greatly appreciated.
    Thanks & Regards,

    Hi Guys,
    I have reviewed the option of using scheduler and javascript and they do satisfy my requirements PARTIALLY. Any calls to operating system commands through these features will be made on the server where APEX is installed.
    However, here what I am looking at is to call operating systems programs on client machine. For example in my APEX application I have constructed the following strings of commands that needs to be run to execute a change request.
    sqlplusw.exe user/password@database @script1.sql
    sqlplusw.exe user/password@database @script2.sql
    sqlplusw.exe user/password@database @script3.sql
    sqlplusw.exe user/password@database @script4.sql
    What I want is to have a button/link on the APEX screen along with these lines so that when I click that link/button this entire line of command gets executed in the same way it would get executed if I copy and paste this command in the command window of windows.
    Believe me, if I am able to achieve what I intend to do, it is going to save a lot of our DBAs time and effort.
    Any help will be greatly appreciated.
    Thanks & Regards,

  • How to call unix script/command in ODI

    Hello Gurus,
    Please let me know how to call unix script/command in ODI?
    Thanks
    Shridhar

    you can call shell script using the OS command found in Package.
    Step 1. Drag the OS Command in the Packgae
    Step 2. In the Text mentiond call the script say for ex sh /opt/path/script.sh
    Step 3. Execute.
    Note : Make sure the User through which ODI is triggering does have the required permission to execute shellscript , also always provide the full path , since scripts are execute from the oracldi/bin folder so its necessary to provide the complete path of the script location.
    Hope this helps.

  • How to call operating system commands?

    Can we call OS commands from JAVA?
    I am trying to call lpr/lp commands from JAVA.
    Can anybody guide me where to search for this information.
    Thanks in advance
    Srini

    Thanks for you all for this information. I will try this....Your information greatly helped me to go forward...

  • Calling the system commands in Java

    Hi All,
    How to call the system commands in java other than using
    exec() function.For eg, in my linux machine i have used p.exec("exit") to exit from a cshell to the prompt but it is not working.Is there any other solution for this one. Pls. do provide a solution for this.It is quite Urgent.
    Thanks,
    m.ananthu

    Why not SEARCH THE F*ING FORUMS!! This has been answered SO MANY TIMES

  • How to execute a system command with call executable in teststand 4.2.1?

    Hi All,
          how to execute a system command with "call executable" step in teststand 4.2.1?
          example as i want to quit a application.using "taskkill /f /im xxx.exe".And execute other system command(DOS).
    BR
    Johnny

    Hi, 
    I want to run netstat -an | find "8080" command from command prompt using call executable in test stand. after that i would like take the std output to local variable. 
    Attachments:
    callsettings.jpg ‏404 KB

  • Is it possible to call operating system command after file receive?

    Hi expert,
    I'm trying to call operating system command after file receive comm.channel receive a txt file.
    operating system command uses txt file name as parameter.
    Is it possible? How?
    Thanks.

    Hi,
    It should be possible, actually we have two options that are
    1.RUN OS command before message processing
    2.RUN OS command after message processing.
    So you can give the commands in the RUN OS command after message processing.
    Check this links for more info
    http://wiki.sdn.sap.com/wiki/display/XI/SAP%20XI%20File%20Adapter%20OS%20Command%20Line%20Feature
    http://help.sap.com/saphelp_nwpi71/helpdata/en/44/556cb799c93677e10000000a114a6b/content.htm
    http://www.sdn.sap.com/irj/scn/weblogs;jsessionid=(J2EE3417200)ID1690747750DB11724434094963199241End?blog=/pub/wlg/10392
    Regards
    Ramesh

  • Calling Operating System Commands from PL/SQL using java

    Calling Operating System Commands from PL/SQL - The Java Way
    wlth help of given thread link,
    Calling OS Commands from Plsql
    but i had user privilege problem
    Declare
    x Varchar2(2000);
    Begin
    x := OSCommand_Run('/tmp/sri/GROUP_ho.sh');
    DBMS_OUTPUT.Put_Line(x);
    End;
    o/p;
    can't exec: /tmp/sri/GROUP_ho.sh lacks user privilege
    i done the grant privillage also
    part
    dbms_java.grant_permission('abcd', 'SYS:java.lang.RuntimePermission', 'writeFileDescriptor', '');
    dbms_java.grant_permission('abcd', 'SYS:java.lang.RuntimePermission', 'readFileDescriptor', '');
    dbms_java.grant_permission('
    abcd', 'SYS:java.io.FilePermission','<<ALL FILES>>', 'execute');
    again
    get
    can't exec: /tmp/sri/GROUP_ho.sh lacks user privilege
    same error;

    Process management at the OS level should prevent execution continuing in the calling code until the command has completed (either successfully or with error).
    If the low level java code were to spawn child process threads then execution could continue, but I'm guessing the Java function your talking about doesn't do that and just calls the operating system to execute the command and waits for the returning code to come back.

  • How to execute Operating System commands from Stored procedure.

    Any help on how to execute Operating System commands from stored procedures will be appreciated.
    Nanditha.

    Search the forums for 'External Procedure' and you will find example code that has been posted before.

  • How to call legacy system screen in web dynpro for java?

    Now I'm using Netweaver CE for building BPM (Business Process Management).
    In BPM system, I wana call legacy system screen(ERP, C/S , Web, etc..) in web dynpro.
    I'v already known how to call legacy system method and data.( using RMI, SOAP and RFC )
    But I don't konw how to call legacy system screen.
    If there is no way calling legacy system screen, I will have to build every screen using web dynpro.
    It's so terrible.
    So I think there is a way calling legacy system screen.
    It's just my opinion.
    If It can call legacy system screen in web dynpro, please let me know how to call it.
    Or, If there is no way calling legacy system, how solve I this problem?
    Thank you reading my question.

    I assume that with "legacy system screen" you mean ABAP Dynp sceens. Of cause it would be attractive to integrate into new BPM processes. However there is a conceptional issue: You could easily integrate them e.g. into a WebDypro using a URL to to the so-called WebGUI (the ITS that makes Dynp screens Web enabled).
    However the Dynp screen store all there state in the ABAP backend. This information is not available to to the process engine, e.g. to set the Task to completed or to use the data in Output Mapping.
    In fact you need a way to wrap the how UI transaction into a WebDynpro screen including data flow.

  • How to use Operating System Commands while using define in sqlplus

    How to use Operating System Commands while using define in sqlplus . The Host OS is Linux
    define report_name=PROD_${host date "+%b"}.html
    The above is not working.
    The output should be 
    define report_name=PROD_JAN.html
    Regards,
    DB

    of course I was talking nonsense: the problem here is the mixing of bind variables and DEFINE variables.
    With the following changes the script should work (the conditions for begin_interval_time are perhaps not what you want):
    variable snap1 number
    variable snap2 number
    variable rptname varchar2(20)
    begin
    SELECT min(snap_id) into :snap1 FROM dba_hist_snapshot WHERE CAST(begin_interval_time AS DATE) >= SYSDATE-1;
    SELECT max(snap_id) into :snap2 FROM dba_hist_snapshot WHERE CAST(begin_interval_time AS DATE) >= SYSDATE-1;
    end;
    col file_name_value new_value report_name noprint
    select 'PROD_'|| to_char(sysdate,'MON') || '_' ||  to_char(sysdate,'DD') file_name_value  from dual;
    define  report_type  = 'html'
    define num_days=2
    define begin_snap=:snap1
    define end_snap=:snap2
    @@?/rdbms/admin/awrrpt.sql

  • How can I pass system commands from a java program? Urgent!

    hi,
    I have been trying this out since a long time. How do I send system commands to command.com or cmd.exe from a java program and an output of the executed command back to the java application.
    If u have any idea, or have any information about the kind os application reply back.
    Thanks
    Deepa Datar

    This is the code which I tried, but it displays only the title of MS-DOS, something like " Microsoft Corp...etc ". But doesn't take any input, and the subprocess(cmd.exe) hangs.
    import java.io.*;
    public class cmddemo2
    public static void main(String arg[])
    try
    System.out.println("cmd");
    Process p=Runtime.getRuntime().exec("cmd.exe");
    DataInputStream din=new DataInputStream(p.getInputStream());
    DataOutputStream dout=new DataOutputStream(p.getOutputStream());
    System.out.println("after streams");
    String s;
    dout.writeChars("type cmddemo2.java");
    while((s=din.readLine())!="\n")
    System.out.println(s);
    dout.writeChars("dir");
    String s1;
    while((s1=din.readLine())!="\n")
    System.out.println(s1);
    System.out.println("over");
    catch(Exception e)
    { System.out.println("Exception : "+e);

  • How to execute a system command on a remote UNIX server?

    I would like to execute a system command (for example "df") on a remote UNIX machine.
    A simple example would be welcome...
    Thanks

    I would like to execute a system command (for example
    "df") on a remote UNIX machine.
    A simple example would be welcome...None will be forthcoming.
    Executing a process is relatively simple. However doing this on a remot system is another matter.
    See http://sourceforge.net/projects/sshtools/
    and/or http://javassh.org/space/start
    Other resources to be found with Google.

  • How stop call to System.exit but allow them to install Securitymanager?

    I have an application that allows you to start a java application from inside it (it then gives you monitoring capabilities). I want this to work with pretty much any desktop application, even those that require their own security manager. However, i don't want their application to be able to shutdown the jvm. How can I do this?
    As far as I can tell, there's no way to allow them to add the securitymanager, "wrapped" by mine is there?
    And I thought of using AccessController.doPriviledgedAction(...) and giving them a context where they can add a security manager but not call system.exit, but then if they do add a security manager, won't it have the final say on system.exit calls?
    Is there a solution here?

    6tr6tr wrote:
    Thanks for the reply!
    Hmmm, the problem with that is I need the users to be able to use my application without access to that app's source code. So they need to be able to run that program (as 3rd party black-boxed code) inside my app and still have this work properly. Is there another way?
    Source code?
    You don't need any source code for what I suggested.
    If a frame/jframe tries to exit on close, I can grab and stop that. my problem is if some random code tries to call system.exit(). Is there any other way to intercept it?
    UPDATE: the only solution i could come up with is to use bytecode engineering + classloading.
    Which I doubt will help if they call it with reflection.
    You could use the bootstrap command line options and replace System with your own version. Add some functionality to how it sets up the security manager and/or make System.exit() do nothing, but provide another name for yourself.

  • How to call operating system(windows) functions like sutdown,refresh,et

    Hello,
    I am new in Oracle D2k.I want to know that how to call windows XP/7 shell commands(operating system functions) or windows function key(like F5 for system refresh in windows xp/7) in an Oracle Form.
    Actually I have to make a trigger which can fire at pressing a particular key and execute appropriate action of Windows7 like shutdown,F5(function key of windows to system refresh).
    thank you
    regards
    aaditya.

    Just for future reference, regardless if you are posting in the Forms area of the forum or any other, terms like Forms 11g, Oracle 11g, etc are parts of product names. They are not version numbers although they do offer a slight hint. It is very important that when posting you include the complete version number of any products related to the problem or question you have. For example, you said you are using Oracle Forms 11g. Look at how many possible versions "Forms 11g" could possibly be:
    11.1.1.1
    11.1.1.2
    11.1.1.3
    11.1.1.4
    11.1.1.6
    11.1.2.0
    11.1.2.1
    With each new patch release and major release, a large number of bug fixes are included. In the case of new major releases, in addition to bug fixes, many new features are added. So knowing the version number is important.
    Beyond that, it would appear that you have many questions:
    1. How to copy and paste
    2. How to execute a system shutdown
    3. How to force the browser into full screen mode
    4. Refresh - this one is unclear. What do you mean by "refresh"?
    Let me answer those that I can:
    <blockquote>o Functions like cut/copy/paste are available directly from your Forms code. For these I would recommend referring to the Builder's Online Help. Look for CUT_REGION, COPY_REGION, PASTE_REGION. It might also be helpful to understand SELECT_ALL. For example you may want to code a Copy button like this:
    GO_ITEM('some_text_field);
    SELECT_ALL;
    COPY_REGION;o To execute a system shutdown on the client machine, you would first need to WebUtil enable your form. Refer to the Forms Builder Online Help for information about how to use WebUtil. You can also find lots of information in this forum about WebUtil. Once enabled, you can execute a Windows shutdown in various ways. Here one option:
    Declare
         my_cmd varchar2(255);
    Begin
         my_cmd := 'cmd /c start "" "' || '"shutdown -s -f -t 1"' || '"';
         WEBUTIL_HOST.HOST (my_cmd);
    End;Refer to the Microsoft documentation for more information: http://technet.microsoft.com/en-us/library/bb491003.aspx
    o I'm not sure what you are attempting to do with the F11. I understand that this function key sets the browser to full screen, but the form will not exactly adjust for this change in browser size. You can adjust the applet size dynamically, but not the form. For that you would need to code the behavior yourself. So, before I can offer something specific, it would be helpful to understand what you expectation is and if what you want is to set the browser to full screen, why not just allow the end user to do it? You could easily have a java script popup displayed before the form starts which might say something like "To maximize your browser, press F11"
    o Finally, "refresh". I don't know what you mean here. Refresh what? The Windows desktop, browser, Oracle Forms.....
    </blockquote>

Maybe you are looking for

  • Acrobat 9 disappeared

    Hi, I had an "authentication code required" so open photoshop was the message whenever I tried to open Acrobat 9. So I re-installed the Design premium suite and Acrobat X Pro is there but not Acrobat 9 which is what I need to build a form. Please hel

  • Attribute tag in xml file

    Hi what is the use of attribute tag in repository xml inside <property > tag example <property name="xyz" column-name="xyx" data-type="string"> <attribute name="abc" value=/abc/xyz/componentName/> </property> here what is use of attribute tag and als

  • I can't remove event handler functions

    Hi guys! Please help to remove event handler functions of a FLVPlaback. I add some functions for an FLVPlaback instance and that's why doesn't work the control panel (play button, pause button and so on) Here is a link: http://sexaid.fw.hu/vg/vg.html

  • Can i back up more then one imac in home to 2T time capsule

    Can i back up 2 imacs to 2T time capsule.  all my music is on the mac in a room away from the time capsule

  • WE19 (iDoc Test Tool) and case sensitive content

    Hi experts, I am not able to send case sensitive content via WE19. When I change a value of an existing iDoc (via WE19) all values of the segment are converted to uppercase (independent of the domain). To reproduce: 1) Open transaction WE19 2) Use an