Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Running a unix script from a java program

Posted on 2008-10-01
5
Medium Priority
?
509 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 60

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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

722 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