why i am not getting any exception stack track

I have built a java class which is being called in another class what I am noticing is that the whole code is executing without throwing any exception even when the input file is not there in the directory. This is causing lot of issues for me.

How can I ensure that some exception or stacktrace is printed when the file is not found in this code that I have written



_______________________________________________________________________________________________
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

public class RunBatchFile {

      private String filename=null;
      private String filelocation=null;
      
      
      public RunBatchFile(String filename) {
            // TODO Auto-generated constructor stub
            
            this.filename=filename;
            
            
      }
      
      public void runcommand()
      {
      try
    {
            System.out.println(filename);
            
        Process p=Runtime.getRuntime().exec("cmd /c " +  filename);
       
        p.waitFor();
        BufferedReader reader=new BufferedReader(
            new InputStreamReader(p.getInputStream())
        );
        String line;
        while((line = reader.readLine()) != null)
        {
            System.out.println(line);
        }

    }
    catch(IOException e1) {
          e1.printStackTrace();
          System.out.println(e1.getMessage());
          System.err.println("An IOException was caught!");
            
    }
    catch(InterruptedException e2) {System.out.println(e2.getMessage());}
      
      }
      

}
 
_________________________________________________________________


import java.io.FileNotFoundException;
import java.io.IOException;

public class TestBatch {

      public TestBatch() {
            // TODO Auto-generated constructor stub
      }

      public static void main(String[] args) {
            // TODO Auto-generated method stub
            
            RunBatchFile r1= new RunBatchFile("C://automation//triggerfile.bat");
            r1.runcommand();
            

      }

}
anshumaEngineeringAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
You're reading stdout (there might not be any) but not stderr (there might be some)
dpearsonCommented:
The reason you're not getting a stacktrace is because there's no exception.

What you are doing is running "cmd /c <file>".  Java successfully runs that command for you.  If that command fails to do what you want (which in this case is run a batch file) then the command will report that on stderr.  That's not an exception because Java has no visibility into what "cmd" is doing (it's a Windows executable).

So your code is working correctly, but you need to read from stderr to see the error message - which will just be text and then you'll need to parse that text.

(Of course in this case, you can also check whether the file exists before you call "cmd" - then you'd get an exception, but you can't do things like detect if there's an error inside the batch file itself, without reading stderr).

Unfortunately reading from stdout and stderr at the same time isn't too easy.
Fortunately, there is a class ProcessBuilder that can help.

Check out this example - in particular note the redirectErrorStream() call
http://www.concretepage.com/java/java-processbuilder-example

and you can read about the whole class here:
https://docs.oracle.com/javase/8/docs/api/java/lang/ProcessBuilder.html

Hope that helps,

Doug

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
anshumaEngineeringAuthor Commented:
how can I modify this code i.e check for file whether it exists or not before passing it to the cmd execution. Please share the best practice for it
CEHJCommented:
File f = new File("xxx");
if (f.exists() && f.isFile()) {
  ...
}

Open in new window

anshumaEngineeringAuthor Commented:
thanks a lot CEHJ and Doug. You both are awesome !!! Very much appreciate your help
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.