Solved

Running a unix script from a java program

Posted on 2008-10-01
5
502 Views
Last Modified: 2013-12-26
I writing a program which has to execute a shell script but it cant. But I can  execute other commands like mv without a problem. here is my code snipet. "
try
            {
                String s = null;
                String [] cmd = new String [];
                cmd [0] = "sh";
                cmd [1] = "-c";
                cmd [2] = "mv eppix/calls/nml/unproctest/" + myFiles [i] + " u1/LOADED";
                cmd [3] = "./TT_TEST_load.sh eppixgui";
                             
                Process process = Runtime.getRuntime ().exec (cmd);
   
            }
            catch (Exception e)
            {
                e.printStackTrace ();
            }

the mv commnad excutes without a problem but ""./TT_TEST_load.sh eppixgui";: this script doest wnat to run. But when I run the script directly on the shell it runs without problems please help
try

            {

                String s = null;

                String [] cmd = new String [];

                cmd [0] = "sh";

                cmd [1] = "-c";

                cmd [2] = "mv eppix/calls/nml/unproctest/" + myFiles [i] + " u1/LOADED";

                cmd [3] = "./TT_TEST_load.sh eppixgui";

                              

                Process process = Runtime.getRuntime ().exec (cmd);

    

            }

            catch (Exception e)

            {

                e.printStackTrace ();

            }

Open in new window

0
Comment
Question by:mafifi
5 Comments
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22616312
I may be mistaken but ./ signifies current folder, so ensure that you are executing this with the that .sh in the same folder as your java program.

If not, you will have to change directories OR use absolute path.
0
 

Author Comment

by:mafifi
ID: 22616340
The Java program in in the same folder as the script.
0
 
LVL 12

Expert Comment

by:basav_com
ID: 22619972
Similar type of question has been answered here.
http://www.experts-exchange.com/Programming/Languages/Java/New_to_Java/Q_23743912.html
Can you pls check the current directory by running 'pwd' command as said by Venabili / mwvisa1.
0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 22620821
May I ask,
why are you running the shell script to move files around? Java has method File.renameTo that moves files or folders.
0
 
LVL 9

Accepted Solution

by:
mbodewes earned 500 total points
ID: 22626723
So if you write this out you get:

"sh" "-c" "mv eppix/calls/nml/unproctest/" + myFiles [i] + " u1/LOADED" "./TT_TEST_load.sh eppixgui"

Which is a bit weird because it will see both "eppix/calls/nml/unproctest/" + myFiles [i] + " u1/LOADED" but also "./TT_TEST_load.sh" and "eppixgui" as arguments to the mv command. Isn't it possible to create a shell script that takes one argument (myFiles[i]) and executes everything you try to put in the cmd[] array?

0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now