Solved

Running a unix script from a java program

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
The viewer will learn how to implement Singleton Design Pattern in Java.
This video teaches viewers about errors in exception handling.
Suggested Courses

627 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