简体   繁体   中英

Java is not recognized as an internal or external command for eclipse but works well from cmd

I am running a sw developped under eclipse (in java) from the cmd. It works well. Once I want to run it from

I am adding a runnble java sw (B.jar) to another java program (A). The "call" of the B.jar works well from the cmd and does the job correctly. Once called from the program (A), it gives me the error of the "unrocognized command". I execute the program (A) from eclipse. Have we to configure some additional information in Eclipse? Or maybe have I to execute directly from the distribution of the software (A)

ScreenShot for the execution from cmd: 在此处输入图像描述

Screenshot from the ececution from eclipse: 在此处输入图像描述

2020-06-24 12:57:26,172 [Thread-7] INFO - 'java' n'est pas reconnu en tant que commande interne 2020-06-24 12:57:26,173 [Thread-7] INFO - ou externe, un programme ex‚cutable ou un fichier de commandes. 2020-06-24 12:57:36,141 [Thread-6] INFO - Appuyez sur une touche pour continuer...

The script from (A) to call (B):

private final InputStream inputStream;

class Flux implements Runnable {

    private final InputStream inputStream;

    Flux(InputStream inputStream) {
        this.inputStream = inputStream;
    }

    private BufferedReader getBufferedReader(InputStream instream) {
        return new BufferedReader(new InputStreamReader(instream));

    }

    public void run() {
        BufferedReader br = getBufferedReader(inputStream);
        String line = "";
        try {
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

String command =  B_SW_AbsolutePath + " "+ usersCommand;
try {
            Runtime rt = Runtime.getRuntime();
            this.proc = rt.exec(command, {""}, outputDir);
            Trace.info("execution "+command+" "+outputDir.toString());
            
            Flux outputStream = new Flux(
                    this.proc.getInputStream());
            Flux errorStream = new Flux(
                    this.proc.getErrorStream());
            new Thread(outputStream).start();
            new Thread(errorStream).start();
            this.proc.waitFor(10,TimeUnit.SECONDS);
        } catch (IOException e) {
            Trace.erreur("External software can not be run " + e.getMessage());
        }
        
        finally{
            
            this.process.destroy(); 
        }
    }

Thank you so much for your help

By using the Runtime.exec(String cmd,String[] envp,File dir) overload with {""} for envp you have provided the child process with no environment variables and particularly no PATH envvar, equivalent to an empty one. Since your batch file apparently runs a simple program name java instead of a complete pathname, it needs to be looked up in the PATH variable and since the PATH variable is empty that lookup fails. If you don't want to or can't change the batch file to specify complete paths for everything (and otherwise not depend on any envvars) -- and if necessary similarly change everything it runs -- the best solution is to pass null for the second (envp) argument to exec so it uses the default which is the parent Java's own envvars. The harder method is to construct a list of the correct envvars (with values) manually, and pass that.

Also, instead of concatenating "script"+" "+"argument" which Runtime.exec must re-split, you can use the String[] overload with new String[]{"script","command"} . Depending on the compiler you might not need the new String[] ; I haven't kept track of all the type-deduction changes because I usually prefer to be explicit. But since you want the combination for your tracing, maybe not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM