Frage

I have a Java Application program that runs various Unix commands while the main application is executing.

public static void shellExec(){
    try{

       System.out.println("Entering into the test shell loop");
        List<String> commands = new ArrayList<String>();
        commands.add("filePath"); //(filepath is the location of a file which contains various Unix command)
         System.out.println(commands);


        ProcessBuilder pb = new ProcessBuilder(commands);
         pb.redirectErrorStream(true);
        Process process = pb.start();
        System.out.println("Started the Process Builder");


        StringBuilder out = new StringBuilder();
        BufferedReader br = new BufferedReader(new    InputStreamReader(process.getInputStream()));
        String line = null, previous = null;
        while ((line = br.readLine()) != null)
            if (!line.equals(previous)) {
                previous = line;
                out.append(line).append('\n');
                System.out.println(line);
            }


    }catch(Exception ex){

        ex.printStackTrace();
        System.exit(1);

    }   

public static void main(String args[]) {

ds = new SQLMXDataSource();
    ds.setUrl("jdbc:t4sqlmx://" + args[0] + ":11600/");
    ds.setUser("super");
    ds.setPassword(args[1]);
    ds.setCatalog("SPM60");
    ds.setSchema("RUV60");
    ds.setMaxPoolSize(10);
    ds.setMinPoolSize(5);
    ds.setMaxStatements(10);
    String filePath = args[2]; 

     stmtExample();
}

Now as you must have guessed that filePath(to be used in shellExec method) is captured at Run Time,

java retest "dev.ind.pascal.com" "passwd" "dev/config/proc.sh"

But when i call this filePath in shellExec method: commands.add("filePath"); The variable is not expanded and hence the shellExec method fails. Note that the double quote is mandatory. Please tell me how to achieve this. I tried: commands.add(\"filePath\") but it still doesnot work. Please help.

Now the filePath keeps on changing every week, So i cant hardcode that value within the program.

War es hilfreich?

Lösung

Try

command.add("\"" + filepath + "\"");

Andere Tipps

You should probably try by removing filepath from quotes. The line will look as follows:

commands.add(filePath);

When you write commands.add("filePath") you are not passing value of variable filePath, instead you are passing string of value filePath

I set value of filePath to "C:/test/I am here" and got the following response:

Entering into the test shell loop
[C:/test/I am here]

You need to make filePath a static variable of your class. In your code it is a local variable of your main method, and therefore inaccessible to the shellExec method. Then remove the quotes from "filePath" in your shellExec method, so you are passing the static variable and not the String "filePath".

class MyClass {

  static String filePath;

  public static void shellExec(){
      try{

         System.out.println("Entering into the test shell loop");
          List<String> commands = new ArrayList<String>();
          commands.add(filePath); //(filepath is the location of a file which contains various Unix command)
      ...
  }

  public static void main(String args[]) {

    ds = new SQLMXDataSource();
    ds.setUrl("jdbc:t4sqlmx://" + args[0] + ":11600/");
    ds.setUser("super");
    ds.setPassword(args[1]);
    ds.setCatalog("SPM60");
    ds.setSchema("RUV60");
    ds.setMaxPoolSize(10);
    ds.setMinPoolSize(5);
    ds.setMaxStatements(10);
    filePath = args[2]; 

    stmtExample();
  }

}

If you need to preface the filePath with a command name, you can do something like this:

commands.add("filePath " + filePath);

Try to add single quotes for filepath like,

command.add("'filepath'");
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top