Runtime.exec with Unix script

I want to execute unix script xxx.sh that I write it for running something and I will execute that script via java class I use
Runtime runTime = Runtime.getRuntime();
Process procRun = runTime.exec("/xxx/yyyy/testing.sh");
xxx and yyyy is path in my unix server and testing.sh is unix script
when I run this class it return true result but that unix script doesn't working or execute following my program in java class
please help me to solve this problem
Regards,
sobig

when I run this class it return true result but that unix script doesn't
working or execute following my program in java classwhat do you mean it returns true....
and how do you know that it does not execute your script.... its probably going to be started as a bg process.... try doing something like creating a directory or something in the script .sh to be sure that it ran.... (just for testing).... im quite sure that it will execute a .sh file like that....

Similar Messages

  • Runtime.exec() with .bat-files in a jar

    Hi All,
    I've written a java-Program, which calls .bat-files per Runtime.exec() for some OS-specific tasks.
    Everything works fine, but if I want to pack the whole code and the .bat-files into several jars, I can't get the bat-files to run. I have tried to get it to work with "getClass().getResource()" as I do the same for my Images, and the returned URL seems to be OK so far (something like jar:file:/c:/test.jar! testpkg/test.bat). I converted this URL into a String and tried to run Runtime.exec() with it, but I always get a Runtime-Exception.
    The String looks exactly like the URL, when I print them to console.
    These bat-files are essential for the application and I would not try to pack them into a jar if I hadn't to distribute this application as a signed applet to unknown users, too.
    I hope there is anyone out there who can tell me if and how it is possible to run an external program out of java, which is packed into a jar, so thanx in advance to any helpful replies.
    acdeka

    You can't run the .bats simply because the shell can't access it. You tell it to run a file that simply doesn't exist in the OS.

  • Runtime.exec() with language specific chars (umlauts)

    Hello,
    my problem is as follows:
    I need to run the glimpse search engine from a java application on solaris using JRE 1.3.1 with a search pattern containing special characters.
    Glimpse has indexed UTF8 coded XML files that can contain text with language specific characters in different languages (i.e. german umlauts, spanish, chinese). The following code works fine on windows and with JRE 1.2.2 on solaris too:
    String sSearchedFreeText = "Tür";
    String sEncoding = "UTF8";
    // Convert UTF8 search free text
    ByteArrayOutputStream osByteArray = new ByteArrayOutputStream();
    Writer w = new OutputStreamWriter(osByteArray, sEncoding);
    w.write(sSearchedFreeText);
    w.close();
    // Generate process
    String commandString = "glimpse -y -l -i -H /data/glimpseindex -W -L 20 {" + osByteArray.toString() + "}";
    Process p = Runtime.getRuntime().exec(commandString);
    One of the XML files contains:
    <group topic="service-num">
    <entry name="id">7059</entry>
    <entry name="name">T&#195;&#188;rverkleidung</entry>
    </group>
    Running the java code with JRE 1.2.2 on solaris i get following correct commandline
    glimpse -y -l -i -H /data/glimpseindex -W -L 20 {T&#195;&#188;rverkleidung}
    --> glimpse finds correct filenames
    Running it with JRE 1.3.1 i get following incorrect commandline
    glimpse -y -l -i -H /data/glimpseindex -W -L 20 {T??rverkleidung}
    --> glimpse finds nothing
    JRE 1.2.2 uses as default charset ISO-8859-1 but JRE 1.3.1 uses ASCII on solaris.
    Is it possible to change the default charset for the JVM in solaris environment?
    Or is there a way to force encoding used by Runtime.exec() with java code?
    Thanks in advance for any hints.
    Karsten

    osByteArray.toString()Yes, there's a way to force the encoding. You provide it as a parameter to the toString() method.

  • Runtime.exec with a unix shell script

    I've done a lot of google'ing and haven't found a definitive answer to my question, including the JavaWorld article that is pointed to quite often. Scenario1 - The program calling Runtime.exec is running in /dir1 and I have a script called test.sh in /dir2 (neither directory are in the path). If I call Runtime.exec("test.sh", null, "/dir2"), I get java.io.IOException: test.sh: not found. However, if I do "touch test.sh" AND "chmod +x test.sh" in /dir1 and call the same exec, it works AND runs the script in /dir2 not /dir1?!?! Also, if I copy /dir2/test.sh /dir1/test.sh and I call exec("test.sh"), it works fine. Scenario2 - If I get rid of /dir1/test.sh and call exec("/dir2/test.sh", null, "/dir2") OR exec("/bin/sh test.sh", null, "/dir2") - both of those work. I just don't get Scenario1. Why does having the dummy executable test.sh in /dir1 allow it to run in /dir2 OR if the script is in the local directory of the process calling exec, why does it work without adding the path? It doesn't seem consistent. Seems like the working directory I specify isn't set before trying to run it or something. On the flip side, if I have a java class in /dir2 called test.class, I can call exec("java test", null, "/dir2") and it just runs without jumping through any hoops - although I think the explanation for that is that "java" is in the path. Let me know what you think.
    Gary

    I think the problem is that the three argument version of exec specifies the command to run (should be full path to it if it is not in the current dir), the environment, then the working directory. The working directory will not help java to find the command to run in the first place. The following does work as you say:
    Runtime.exec("/bin/sh test.sh",null,"/dir2");This is because the full path to the command (/bin/sh) is specified, that runs in directory /dir2 then tries to find test.sh which it finds there.
    Basically to run anything, you should specify the full path in the command, the working directory will not help java find it.
    Dave

  • Runtime.exec() on shell script - unix

    Hello all.
    I'm gonna run a shell script and its parameter on unix solaris.
    This is the whole command:
    -> ex.sh "03/02/03 11:22:00"
    'ex.sh' consists of the line like this.
    -> ex date="$SSCREATE"
    So, I expect "03/02/03 11:22:00" would be replaced with '$SSCREATE'
    BTW, in Java class I invoke the shell in this way.
    Process p = Runtime.getRuntime().exec("./ex.sh \"03/02/03 11:22:00\"");
    InputStreamReader in = new InputStreamReader(p.getInputStream());
    BufferedReader br = new BufferedReader(in);
    But I can't get any output from this, I mean, when I use
    'br.readLine()'
    Why is that? Do I miss something?
    Thanks in advance.

    Thanks Jason.
    Actually, I succeeded to run only 'whatever.sh'.(without any parameters) - No problems using Runtime.exec().
    But, according what you wrote, is '/usr/bin/sh' a must as a prefix before real commnad line?
    And what if the argumet has "(double quote) at the both ends?
    I'd be glad if you show me the example.
    Thank you again.

  • Runtime.exec() on Unix with "grep"

    Hi does someone know how to execute a Unix command which is a ps result piped to grep. What I have runs perfectly for a simple ps like ps -aux runMyCommand, but not for something like ps -aux runMyCommand | grep 8080. I am using Runtime.exec() method.
    Please help.
    Thanks,

    the pipe is handled by the shell, so you cannot just do a runtime.exec("ps aux | grep 8080"), you could try something like this;
    String cmd = "sh -c 'ps aux | grep 8080' ";
    Runtime.exec(cmd);
    or even
    String cmd = "sh -c 'ps aux | grep 8080 | grep -v grep'";

  • Oracle career with unix scripting?

    hi
    iam working in a reputed mnc with CMMi level 5 .started as fresher (BE I.T) i have around 1.6 years of experiance in unix scripting/oracle sql production support.
    with learning of sql and shell scripting
    i want to make my career in oracle which should be more dynamic,learning and creative not like in production support which have monotonus life.
    could anyone please guide me about what career path i should choose ?
    DBA , i have heard is montonous job too with same routine backups,no creativity with nightshifts and all, but it is more stable as people said. I have no experiance of it.
    second devolper track in sql/pl sql having its own limitations like less demand, less financial growth etc. and people usually wants to shift in other area like BO
    i don`t have any info about others areas like that i would like to explore
    1)Datawarehousing
    2)Oracle apps
    3)Business objects (BO reporting)
    4)Oracle CRM etc
    5)Others (thats all i know,please add some others if they are creative )
    iam really confsused now which way to chosse. and how to jump in other company on wat basis?
    preparing for OCA right now but uncertain for career.
    Seeking good help from forum experts.

    "with learning of sql and shell scripting"
    Ah, very good move, so you must pick my tool, it is all about managing Oracle DB through Korn shell http://www.smenu.org.
    As of your question, the answer is in your mood and preference.
    Oracle DBA: Definitely more money, less opportunity. the job is dwindling somehow due to Grid automation and budget restriction.
    It is not that you don't need DBA, but manager thinks they can do without and don't (try) replace them. Pressure on DBA is a myth: more work = more money = happy DBA until wife run away.
    Developer : Definitely less money, not necessary more opportunity unless you have java EE. no shift but more pressure: Projects are always in late, usually in the development area, not in the infrastructure.
    One funny thing I noticed during these last 15 years, is that DBA have usually more children than others people (unix sys admin or Dev). Probably related to better earning.
    DBA can be boring or passionate depending on your own knowledge, most of others DBA I know are still in the job, only one is now JAVA EE guy.

  • Runtime.exec() with japanese arguments?

    I'm trying to invoke a C++ executable from java using the Runtime.exec() method. The C++ application accepts a filename as a command line argument & opens the file. This C++ app is unicode enabled i.e. it can accept UTF-16 (wide char) parameters. Howevere, when i invoke this application using Java's Runtime.exec() and specify a japanese file name as an argument, the japanese characters get converted to '?' characters by the time they are received in the C++ application. I'm running this application on Windows 2K, default i.e. English version.
    Looking at the source code of Runtime class, it seems that the exec()
    function makes use of a native helper function - execInternal(). Does
    this function support the entire unicode range?
    Is there any way we can avoid the conversion of japanese characters to '?' characters? Also, is there any other alternative for invoking an external application with Unicode (Say, japanese) arguments?
    Please reply ASAP.
    Thanks!

    I have a very similar problem. I am invoking cvs through Runtime.exec, and I am using it to add and check in files with chinese characters in the name, and it is failing with the following cvs error:
    Could not add resource file &#32435;&#20986; in directory C:\test to repository: cvs add: nothing known about ??
    The error message comes from cvs, and I have been able to track it down to the call string I pass to exec. It is correct when passed in but the command fails. For laughs I tried to call something simpler, like mkdir:
    err mkdir ?? is: The filename, directory name, or volume label syntax is incorrect.
    It seems pretty clear to me that although the callstring is correct, the call to exec fails, and the special characters are replaced with questionmarks, which seems to me to be an encoding issue.
    Did you get any solution to your problem, or does anyoen else have an answer for this?

  • Runtime.exec() with envp[] array

    I ran into an unsual problem.
    If I set an env array variable to something (at least one pair), then certain applications fail to start through Runtime.exec(command[], env[]). For example, my own java classes that require various xml and ftp libraries. The particular exception has been mentioned on the forum ( ie. Unrecognized Windows Sockets error: 10106:).
    It has to do with environment variables not being set for new process. When I do NOT set env[] variable (or pass null) then current env is used in .exec() call and everything works fine.
    So my question is how do I set my environment variables and/or append them to existing set.Or said differently if I pass a non-null array, current env set is not copied over.
    thanks,
    eugene

    >
    It has to do with environment variables not being set for new process. When I do NOT set env[] variable (or pass null) then current env is used in .exec() call and everything works fine.
    So my question is how do I set my environment variables and/or append them to existing set.Or said differently if I pass a non-null array, current env set is not copied over.
    >
    If you're using 1.5, you can create a java.lang.ProcessBuilder object rather than calling Runtime.exec(). The ProcessBuilder inherits the environment of the calling process, but then allows you to update individual entries.
    In 1.4, I don't believe that you can get a complete copy of the invoker's environment.

  • Runtime.exec with spaces not working EVEN using String[]!!

    Hi everyone,
    I need to start the rmi registry from some code,and i need to pass it the classpath of two jars when initialising it. My problem is that the paths I set aren't taken when they contain a space.
    Here's the code:
    <code>
    String rmiRegistryCommandLine[] = new String[] {
    + System.getProperty("java.home")
    + "\\bin\\rmiregistry.exe\"",
    "-J-Djava.class.path=\""+System.getProperty("user.dir")+"\\MyJar.jar\"",
    "1099"};
                                            Runtime.getRuntime().exec(rmiRegistryCommandLine);
    </code>
    I know that Runtime.exec(String) tokenizes the input, which is why I'm not using it, but Runtime.exec(String[]) isn't supposed to tokenize the input. System.getProperty("user.dir") can contain a space, so I put quotes around that, but I need the -J-Djava.class.path in the same string. I tried breaking it up into two more separate strings but it didn't even run for normal non-space paths then. I am sure that if the whole -J-Dblah....upto MyJar.jar was in quotes then it would work, but I need the classpath in quotes separately as it could contain a space.
    Can anyone help me get this working?

    Ya, that's fine but the command line I want to pass is:
    d:\j2sdk1.4.0\bin\rmiregistry.exe -J-Djava.class.path=d:\my dir with spaces\MyJar.jar;d:\my more dir with spaces\MyJar2.jar 1099
    If I say
    arg[0]="d:\\j2sdk1.4.0\\bin\rmiregistry.exe";, that's finebut arg[1] is the problem
    if I say
    arg[1]="-J-Djava.class.path=d:\\my dir with spaces\MyJar.jar; d:\\my more dir with spaces\\MyJar2.jar";, then it definitely won't work on 9X machines and probably not on NT.
    if I break up arg[1] into:
    arg[1]="-J-Djava.class.path=";
    arg[2]="d:\\my dir with spaces\\MyJar.jar";
    arg[3]=";";
    arg[4]="d:\\my more dir with spaces\\MyJar2.jar";I'll need to put quotes around the two individual two class paths or else it won't work.
    I find though that if I put the classpath as follows
    d:\"my dir with spaces"\MyJar.jar then it seems to work.
    I think a regular expression function to search for any directory with spacees and then put quotes around it and reinsert it into the path would be the solution.

  • Runtime.exec() with get command output

    hi,
    How can i get output of command which i run with runtime.exec().
    p.e.:
    Process p;
    Runtime r = Runtime.getRuntime();
    p = r.exec("net use \\pc\dir pswd /user:username");
    if command was done succesfully that in cmd is return this:
    "The command completed successfully." I can get this text, but i dont know how.
    Plese help
    thank you
    Palo

    Look at the API for Process

  • Runtime exec with blanks

    I there!
    i've a problem calling linux commands with blanks.
    eg:
    ---snip---
    String cpCommand = "cp /home/bullseye/my file.txt /tmp/tempFile.tmp";
    Process p = Runtime.getRuntime().exec(cpCommand);
    return p.waitFor()
    ---snip---
    i've tried with
    String cpCommand = "cp /home/bullseye/my\ file.txt /tmp/tempFile.tmp";
    and
    String cpCommand = "cp \"/home/bullseye/my file.txt\" \"/tmp/tempFile.tmp\"";
    but nothing works....
    could someone help me?!
    thx-a-lot

    I tried this command in Unix and it works.
    cp "/home/bullseye/my file.txt" /tmp/tempFile.tmp
    The java String for this will be
    String cpCommand = "cp \"/home/bullseye/my file.txt\" /tmp/tempFile.tmp";Hope this helps.
    vinod.

  • Using Runtime.exec() on Unix (AIX)

    I am attempting to use exec() on the server side to move files to a a windows share via Samba. I'm getting 2 resutls:
    If I run the code with exec(string) the server/share infrormation is not passed and Samba attempts to connect to ' '. I'd imagine this is the string being broken down by the StringTokenizer, it seeing the space and splitting the command. Is there a way to escape a space in StringTokenizer?
    If I run the code with exec(array[]) the executable is not even found, though it is above, which is confusing.
    I've also tried running with exec(string/array, null, <a good working dir>) with the exact same results on both.
    I have also tried to run array b below like this using sh:
    String[] b = new String[] {"/bin/sh", "-c", cmd};  
    Process pr = rt.exec(b);
    String cmd = "/usr/bin/smbclient \'\\\\nwfiles\\ISGData\' -U \'mi/username%pasword\' -d3 -c \'prompt off;cd \"IVOps\\SWAPS\\Calypso\\Reports\";lcd /opt/calypso/ftp/reports/; put UG5R1DRV120.TST;exit;\'";
    File wd = new File("/opt/calypso/ftp/reports");
    try {
       Process pr = rt.exec(cmd,null,wd);
       // Get any output
       InputStream in = pr.getInputStream();
       InputStreamReader inR = new InputStreamReader(in);
       BufferedReader buf = new BufferedReader(inR);
       String line;
       while  ((line = buf.readLine())!= null) { 
                 Log.error(this,"OUT MSG: " + line) ; 
       // Get any errors
       InputStream inErr = pr.getErrorStream();
       InputStreamReader inRErr = new InputStreamReader(inErr);
       BufferedReader bufErr = new BufferedReader(inRErr);
       String lineErr;
       while  ((lineErr = bufErr.readLine())!= null) { 
            Log.error(this,"ERR MSG: " + lineErr) ;
    } catch(Exception e) {
       Log.error(this,e.toString());
    }Edited by: kewlguy53403 on Jan 21, 2009 10:02 PM
    Edited by: kewlguy53403 on Jan 21, 2009 10:08 PM

    I believe that you are out of luck if you need spaces.
    I know for sure that that is true for exec(string). It parses the string whether you want it to or not. No way to stop it.
    There is a thread that discusses that somewhere, where someone looked through the VM source to determine what was going on.
    I think that a bug should have been opened on that but I can't recall if it happened now.
    A possible solution - use a script file. It has the commands. And then java runs that.

  • Stuck On Runtime.exec() with cmd.exe

    I've done my best to look through the forums and implement what's been said but I'm getting a problem where my whole Java program freezes when the exec() is executed. Can anyone tell me what I'm missing or have misunderstood ?
            try
                Process runSmodels = Runtime.getRuntime().exec("C:\\Windows\\System32\\cmd.exe lparse predict.pl | smodels 0");
                String smodelsOutput = new String();
                BufferedReader smodelsReader = new BufferedReader(new InputStreamReader(runSmodels.getInputStream()));
                String outputLine = smodelsReader.readLine();
                while(outputLine != null)
                    smodelsOutput += outputLine;
                    outputLine = smodelsReader.readLine();
                smodelsReader.close();
                System.out.println(smodelsOutput);
            catch(IOException exc)
                JOptionPane.showMessageDialog(rootPane, "Could not access Smodels.", "System Call Error", JOptionPane.ERROR_MESSAGE);
            }

    DarioAtUOW wrote:
    Pitfall 1 : Calling .exitValue(). My code doesn't do that.
    Pitfall 2 :Not enptying relevant streams. I do empty the input stream.
    Pitfall 3 : Assuming a command is an executable program. I don't do that, cmd.exe is in my command string explicitly.
    Pitfall 4 : As above in Pitfall 3.
    So, why accuse me of not reading the article ? You probably have many years of experience at Java, unlike me, who is just starting out with it at university.Pitfall 2 : Even though you are processing stdout you are not processing stderr! Always always always process both. This may or may not be the cause of your problem but by copying the process stderr to your System.err you may see why your process is stuck.
    Even Clippy, the Microsoft Office Assistant, could answer questions better than you have attempted to do.Ouch, I'm cut to the bone on your razor sharp wit. Phuck you.

  • Runtime.exec with wget

    Hi,
    well i want to execute wget command in a java class and print sdout in the shell
    this is my code:
              try {
                   Process ls_proc = Runtime.getRuntime().exec("wget http://antimilkado.free.fr/ted/screenshot.png");
                   BufferedReader ls_in = new BufferedReader(new InputStreamReader(ls_proc.getInputStream()));
                   try {
                        while ((ls_str = ls_in.readLine()) != null) {
                             System.out.println(ls_str);
                   catch (IOException e) {
                        System.err.println(e);
                        System.exit(0);
              }I've tested with other basic command like ls and co., it works but not with a wget, nothing is printed...
    I guess my code is good and there is maybe a problem with wget command i don't know
    If someone gets it :)
    thx
    ++
    SHiSo

    1) Get the InputStream from the connection.
    2) Open a FileOutpupStream to the file you wish to write.
    3) Loop reading bytes from the InputStream and write them to the FileOutputStream until you get EOF.
    e.g.
    InputStream is = ...
    FileOutputStream os = ...
    byte[] buffer = new byte[4096];// or any size you want
    for (int count = 0; (count = is.read(buffer)) >= 0;)
    os.write(buffer, 0, count);
    4) flush() then close() the FileOuputStream.

Maybe you are looking for

  • EDI outbound error

    Hi All we are working on EDI outbound messages and facing the following error <?xml version="1.0" encoding="UTF-8" standalone="yes" ?> - <!--  Request Message Mapping   --> - <SAP:Error xmlns:SAP="http://sap.com/xi/XI/Message/30" xmlns:SOAP="http://s

  • Problem with acrobat pro x

    Unable to open acrobat pro X I have a valid license number for CSS 5.5 design premium, the rest works fine just acrobat pro is not opening. It's asking for my license which I type, it appears like it accepted it but I get back to the same window aski

  • Formula based on Display data (summarized data)

    PrvYrAmt--PrvYrQty-- AvgPrice----CurYrQty--CurYrAmt (PrvYrAmt---- (CurYrQty * / PrvYrQty)---- Prv Yr AvgPrice) 150 - 10 - $15--15-- 225 100 - 20 - $5  - 10---- 50 Total  - $10 - 25---- 250(sum 275) When I try to create a Formula with CurYrAmt I need

  • Marks on inside of screen

    my imac is coming up for 12 months old, and has a series of grey shadows on the back of the glass - what can i do?

  • 4G and Data cutting in and out

    I have had a Thunderbolt for about 3 mo now and about 4 days ago my phone stopped picking up 4G. I have used the phone info test and have it set correctly to pick up 4G. The data will also cut in and out if I am streaming video or playing games. I am