13.07.2015 Views

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

104Chapter 4Where Am I? Executi<strong>on</strong> C<strong>on</strong>textor even a whole package of classes that write log messages to System.out(e.g., System.out.println("some message")). Now you realize that you’dlike the output to go somewhere else.You could redirect standard out, as in:$ java SomeClass > logbut that requires the user to remember to redirect the output every time theprogram is invoked. That’s fine for testing, or if the output is intended to goto a different place each time it is invoked. But, in this example, we always wantthe output to go to the same locati<strong>on</strong>.Without changing any of the System.out.println() statements, all themessages can be sent to a new locati<strong>on</strong> by reassigning the System.out printstream. The System class has a setter for out—that is, a method which will letyou set a new value for out. In your Java program, open the new destinati<strong>on</strong>file and give this to the System class:PrintStream ps = new PrintStream("pathname");System.setOut(ps);It will be used from that point forward in the executi<strong>on</strong> of this program as itsout output stream.CAUTIONChanging standard out (or in, or err) will make the change for all classesfrom here <strong>on</strong> in this invocati<strong>on</strong> of the Java runtime—they are static fields ofthe <strong>on</strong>e System class. Since this is so serious a move, the Java SecurityManager (see Secti<strong>on</strong> 5.8.4.2) provides a check for setIO to see if the Javaprogram is allowed to make such changes. If such a security manager is inplace and you are not allowed to make such changes, an excepti<strong>on</strong>(SecurityExcepti<strong>on</strong>) will be thrown. Note also that the permissi<strong>on</strong> appliesto setting any of the fields; it doesn’t divide the permissi<strong>on</strong> into setting <strong>on</strong>e(e.g., out) but not another (e.g., in).4.3.2 Envir<strong>on</strong>ment VariablesWhen <strong>Linux</strong> programs are run they have the open file descriptors describedabove. They also carry with them a list of “name=value” pairs called theirenvir<strong>on</strong>ment. These envir<strong>on</strong>ment variables allow for c<strong>on</strong>text to be shared am<strong>on</strong>g

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!