Home > OS >  Bat file working from terminal not working from Java
Bat file working from terminal not working from Java

Time:11-23

I found my command line tool gets stuck in between and needs a enter to process and I found that it’s because of "QUICKEDIT" mode in cmd and we want to disable it to avoid that. So I searched for Java options to disable quick edit mode on my app launch but I got only bat file from here quickedit.bat.

And this bat file works perfect when I run from my command prompt it disable quick edit mode in the current session itself which is the same I want. So I kept that bat file in my folder via installer and run it first on every launch but it’s not turning off the quick edit mode for current session.

I have tried using both process builder and runtime.exec.

Below is my code

ProcessBuilder pb = new ProcessBuilder("cmd", "/c", "quickedit.bat");
File dir = new File(System.getProperty("user.home") File.separator "AppData" File.separator "Local" File.separator);
pb.directory(dir);
Process p=null;
try {
    p = pb.start();
} catch (IOException e2) {
    // TODO Auto-generated catch block
    e2.printStackTrace();
}

BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
try {
    while ((line = in.readLine()) != null) {
        System.out.println(line);  // ----Here i get the same output i get when i run the bat file
        }
} catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}
BufferedReader inerr = new BufferedReader(new InputStreamReader(p.getErrorStream()));

try {
    while ((line = inerr.readLine()) != null) {
        System.out.println(line);
    }
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

It gives me this:

output of my java program

When I run my bat file directly like this:

output of bat file

But through Java it didn't disable quick edit in my current command prompt whereas it disables at once I run the actual bat file. So can anyone say the reason or how to fix it or any other way to disable it for ever from Java?

CodePudding user response:

Try pb.inheritIO(); before you call its start method. What you seem to have is a hybrid batch/Powershell script that that relies on stderr to determine which of the two it executes so this should require correct processing of stderr.

CodePudding user response:

I didn't look at your bat file the most common issue on this kind of think is that process run from java did'nt share the environnement variable of your local setup nor jvm a quick fix to verify that is :

pb.environment().putAll(System.getenv());

hoping this will work :) then you just have to found which specific environnment variable is missing :)

  • Related