954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

OS command from JAVA program...

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.

new_2_java
Junior Poster
127 posts since Apr 2007
Reputation Points: 7
Solved Threads: 6
 

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 ". 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.

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

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 ". 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,

new_2_java
Junior Poster
127 posts since Apr 2007
Reputation Points: 7
Solved Threads: 6
 

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.

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

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.

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 
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.

new_2_java
Junior Poster
127 posts since Apr 2007
Reputation Points: 7
Solved Threads: 6
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You