[Runtime]exec d'une commande DOS
Salut à tous,
J'ai une exception en essayant de créer un process via la fonction exec de la classe Runtime.
J'essaye de lancer la commande wscompile (du JWSDP). Même en essayant de lancer la commande "dir", j'ai la même erreur:
Code:
1 2 3 4 5 6 7 8
|
java.io.IOException: CreateProcess: wscompile -gen:client -d "C:\Documents and Settings\dme\.dum4ws\tmp\classes" -classpath "C:\Documents and Settings\dme\.dum4ws\tmp\classes" "C:\Documents and Settings\dme\.dum4ws\config-wsdl.xml" error=2
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(ProcessImpl.java:81)
at java.lang.ProcessImpl.start(ProcessImpl.java:30)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:451)
at java.lang.Runtime.exec(Runtime.java:591)
at java.lang.Runtime.exec(Runtime.java:464) |
Quand je lance exactement la même commande dans une invite DOS, ça marche.
ça fait la même chose si je spécifie le patch complet de la commande wscompile.
Quelqu'un peut me dire ce qui ne va pas...
Pour info voilà ma classe qui lance la commande:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
public class WSDL2JavaNoExit {
public static Logger m_logger = Logger.getLogger(WSDL2JavaNoExit.class);
/****
* Lance wscompile
*/
public static void run(String dir_out_stub) throws Exception{
try {
Runtime rt = Runtime.getRuntime();
String cmd[] = new String[7];
cmd[0] = "wscompile";
cmd[1] = "-gen:client";
cmd[2] = "-d";
cmd[3] = dir_out_stub;
cmd[4] = "-classpath";
cmd[5] = dir_out_stub;
cmd[6] = "L:\\config-wsdl.xml";
m_logger.debug("Launch the command " + cmd);
Process proc = rt.exec(cmd);
// any error message?
StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "ERROR");
// any output?
StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "OUTPUT");
// kick them off
errorGobbler.start();
outputGobbler.start();
// any error???
int exitVal = proc.waitFor();
m_logger.debug("ExitValue: " + exitVal);
}
catch (Exception e) {
m_logger.fatal("Exception catched",e);
}
}
}
/****
* Récupère la sortie standard de la commande wscompile
*/
class StreamGobbler extends Thread
{
public static Logger m_logger = Logger.getLogger(StreamGobbler.class);
InputStream is;
String type;
StreamGobbler(InputStream is, String type) {
this.is = is;
this.type = type;
}
public void run() {
try {
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ( (line = br.readLine()) != null)
m_logger.debug(type + ">" + line);
}
catch (IOException ioe) {
m_logger.error("Exception catched",ioe);
}
}
} |
Merci
fifi :wink:
Re: [Runtime]exec d'une commande
Citation:
Envoyé par fidififouille
Salut à tous,
J'ai une exception en essayant de créer un process via la fonction exec de la classe Runtime.
J'essaye de lancer la commande wscompile (du JWSDP). Même en essayant de lancer la commande "dir", j'ai la même erreur:
Code:
1 2 3 4 5 6 7 8
|
java.io.IOException: CreateProcess: wscompile -gen:client -d "C:\Documents and Settings\dme\.dum4ws\tmp\classes" -classpath "C:\Documents and Settings\dme\.dum4ws\tmp\classes" "C:\Documents and Settings\dme\.dum4ws\config-wsdl.xml" error=2
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(ProcessImpl.java:81)
at java.lang.ProcessImpl.start(ProcessImpl.java:30)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:451)
at java.lang.Runtime.exec(Runtime.java:591)
at java.lang.Runtime.exec(Runtime.java:464) |
Quand je lance exactement la même commande dans une invite DOS, ça marche.
ça fait la même chose si je spécifie le patch complet de la commande wscompile.
Quelqu'un peut me dire ce qui ne va pas...
Pour info voilà ma classe qui lance la commande:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
public class WSDL2JavaNoExit {
public static Logger m_logger = Logger.getLogger(WSDL2JavaNoExit.class);
/****
* Lance wscompile
*/
public static void run(String dir_out_stub) throws Exception{
try {
Runtime rt = Runtime.getRuntime();
String cmd[] = new String[7];
cmd[0] = "wscompile";
cmd[1] = "-gen:client";
cmd[2] = "-d";
cmd[3] = dir_out_stub;
cmd[4] = "-classpath";
cmd[5] = dir_out_stub;
cmd[6] = "L:\\config-wsdl.xml";
m_logger.debug("Launch the command " + cmd);
Process proc = rt.exec(cmd);
// any error message?
StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "ERROR");
// any output?
StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "OUTPUT");
// kick them off
errorGobbler.start();
outputGobbler.start();
// any error???
int exitVal = proc.waitFor();
m_logger.debug("ExitValue: " + exitVal);
}
catch (Exception e) {
m_logger.fatal("Exception catched",e);
}
}
}
/****
* Récupère la sortie standard de la commande wscompile
*/
class StreamGobbler extends Thread
{
public static Logger m_logger = Logger.getLogger(StreamGobbler.class);
InputStream is;
String type;
StreamGobbler(InputStream is, String type) {
this.is = is;
this.type = type;
}
public void run() {
try {
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ( (line = br.readLine()) != null)
m_logger.debug(type + ">" + line);
}
catch (IOException ioe) {
m_logger.error("Exception catched",ioe);
}
}
} |
Merci
fifi :wink:
L'erreur 2 indique qu'il ne trouve pas le fichier.
"Dir" est un mauvais exemple car c'est une commande interne du shell de windows.
"wscompile" est-il une batch commande?
Si oui, alors essaie avec "cmd /c".
Modifie ainsi ton code:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
// Création de la commande à éxecuter
StringBuffer command = new StringBuffer(512);
command.append("wscompile "); // préférable avec le path complet
command.append("-gen:client ");
command.append(-d \"" +dir_out_stub+ "\" ");
command.append(-classpath \"" +dir_out_stub+ "\" ");
command.append("L:\\config-wsdl.xml");
cmd[0] = "cmd";
cmd[1] = "/c";
cmd[2] = command.toString(); |