Solved

Running a unix script from a java program

Posted on 2008-10-01
5
504 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
java 8 lambda expresssions exception handling 3 89
java jdbc batch example 8 32
tomcat not starting 6 45
jdbc error in jsp application 20 45
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

770 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