2016-02-17 1 views
4

J'essaie d'exécuter un fichier bash via Java dans Ubuntu14.0. fichier bash a le code pour générer un modèle de mobilité pour le fichier NS2Exécution du package NS dans un fichier bash via Java

"#!/bin/bash +x 
cd /home/maria/ns-allinone-2.35/ns-2.35/indep-utils/cmu-scen-gen 
ns cbrgen.tcl -type cbr -nn 10 -seed 1 -mc 5 -rate 5.0" 

est exécutable et donnant sortie si elles sont exécutées de manière explicite par le biais du terminal.

Mais lorsqu'il est exécuté par java sa donne l'erreur suivante:

/home/maria/Documents/test.sh: line 4: ns: command not found 
Execution failed. 
org.apache.commons.exec.ExecuteException: Process exited with an error: 127 (Exit value: 127) 
at org.apache.commons.exec.DefaultExecutor.executeInternal(DefaultExecutor.java:404) 
at org.apache.commons.exec.DefaultExecutor.execute(DefaultExecutor.java:166) 
at org.apache.commons.exec.DefaultExecutor.execute(DefaultExecutor.java:153) 
at test.opencmd.runScript(opencmd.java:18) 
at test.opencmd.main(opencmd.java:30) 

Voici mon code:

package test; 

import java.io.IOException; 
import org.apache.commons.exec.CommandLine; 
import org.apache.commons.exec.DefaultExecutor; 
import org.apache.commons.exec.ExecuteException; 

public class opencmd { 
    int iExitValue; 
    String sCommandString; 

    public void runScript(String command){ 
     sCommandString = command; 
     CommandLine oCmdLine = CommandLine.parse(sCommandString); 
     DefaultExecutor oDefaultExecutor = new DefaultExecutor(); 
     oDefaultExecutor.setExitValue(0); 
     try { 
      iExitValue = oDefaultExecutor.execute(oCmdLine); 
     } catch (ExecuteException e) { 
      System.err.println("Execution failed."); 
      e.printStackTrace(); 
     } catch (IOException e) { 
      System.err.println("permission denied."); 
      e.printStackTrace(); 
     } 
    } 

    public static void main(String args[]){ 
     opencmd testScript = new opencmd(); 
     testScript.runScript("bash /home/maria/Documents/test.sh"); 
    } 
} 
+0

Êtes-vous en cours d'exécution à partir d'un IDE? Que faire si vous utilisez '/ full/path/to/ns' à la place? –

+0

Oui J'utilise eclipse. J'ai essayé de faire ce que vous avez suggéré mais son erreur de donner –

+0

En utilisant le '/ full/path/to/ns' fonctionne? –

Répondre

2

S'il vous plaît ci-dessous:

Process p = Runtime.getRuntime().exec("/home/maria/ns-allinone-2.35/ns-2.35/indep-utils/cmu-scen-gen/setdest/setdest -v 2 -n 50 -p 2.0 -s 10.0 -t 200 -x 500 -y 500 -m 2 -M 15"); 
Process p = Runtime.getRuntime().exec("/home/maria/ns-allinone-2.35/bin/ns /home/maria/ns-allinone-2.35/ns-2.35/indep-utils/cmu-scen-gen/cbrgen.tcl -type cbr -nn 10 -seed 1 -mc 5 -rate 5.0"); 
+1

Merci. problème résolu –