Hi all,

I would like to execute an OS command like "cls" from my Java program, and am getting the following error:

D:\ext>java FileExtractionConsole
Exception in thread "main" java.lang.NullPointerException
        at FileExtractionConsole.startExtraction(FileExtractionConsole.java:46)
        at FileExtractionConsole.main(FileExtractionConsole.java:33)

D:\ext>

Here's the snipt of my code:

public class FileExtractionConsole {
 public static void main (String [] arg){
        FileExtractionConsole fe = new FileExtractionConsole ();           
        fe.displayMainMenu();
    }
 
 private void displayMainMenu () {
    try {
	Process p = Runtime.getRuntime().exec("cls");   // exception occurs here.
	System.out.println("\n            FILE EXTRACTION UTILITY\n\n");
	System.out.println(" Please enter User ID and Password");
	BufferedReader stdin = new BufferedReader( ....
                // rest of the code...
         } catch (IOException io) {
	printStream.println("Exception occured: " + io.getMessage());
         }		
   }
}

I get an exception on the line where I try to execute "cls" command. Am I doing something not correct here? Please advise...

Thanks.

I think the null pointer exception is occurring from the printStream.println() call, rather than the exec() call. You haven't initialized "printStream", so when the error from that exec() command is thrown (which is that "cls" is not a program, by the way), the catch statement is throwing the exception out to main().

To execute a command, you have to use "cmd /c <command>". However, if you are expecting that command to clear the command window in which you are running your program, it will not. Runtime.getRuntime().exec() creates a new separate process.

I think the null pointer exception is occurring from the printStream.println() call, rather than the exec() call. You haven't initialized "printStream", so when the error from that exec() command is thrown (which is that "cls" is not a program, by the way), the catch statement is throwing the exception out to main().

To execute a command, you have to use "cmd /c <command>". However, if you are expecting that command to clear the command window in which you are running your program, it will not. Runtime.getRuntime().exec() creates a new separate process.

Thanks for reply, Infact that null pointer exception was due to printStream.

Basically, what I would like to present a menu to the user, after selecting an item from menu, I want to clear the screen. will this command clear the screen? It looks like it doesn't. How/if I can do this?

...
try {
       Process p = Runtime.getRuntime().exec("cmd /c cls");	
} catch (IOException e) {
       // handle exception TO DO		
}
...

Thanks,

In short, you can't clear the console without system specific calls. You can write a bunch of blank lines to scroll everything to a point it looks clear, but that's about all without "workaround hacks".

If you want full control of the screen that you are running in, you should make the application a GUI application.

and you shouldn't even TRY to control the console. You do NOT own the console, NEVER assume you're the only thread/process writing to it.

and you shouldn't even TRY to control the console. You do NOT own the console, NEVER assume you're the only thread/process writing to it.

Thanks for advise. I will design a GUI for that.

This question has already been answered. Start a new discussion instead.