<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>JavaWords &#187; General</title>
	<atom:link href="http://javawords.com/category/general/feed/" rel="self" type="application/rss+xml" />
	<link>http://javawords.com</link>
	<description>A blog about Java Programming Language. Tips and solutions for common problems, tools and resources, tutorials and guides. A window to share my Java experience with others.</description>
	<lastBuildDate>Tue, 22 Dec 2009 14:43:17 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Start a native application from your Java code</title>
		<link>http://javawords.com/2008/04/04/start-a-native-application-from-your-java-code/</link>
		<comments>http://javawords.com/2008/04/04/start-a-native-application-from-your-java-code/#comments</comments>
		<pubDate>Sat, 05 Apr 2008 02:17:10 +0000</pubDate>
		<dc:creator>xfrag</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://javawords.com/2008/04/04/start-a-native-application-from-your-java-code/</guid>
		<description><![CDATA[Starting a native application from your Java code is as simple as the following command:
Runtime.getRuntime().exec(&#34;notepad.exe&#34;);
Of course, this adds platform dependency to your code, and if you want your application to be portable it&#8217;s a good practice to avoid using the exec() method. But if it is necessary to initiate a native application for any reason, [...]]]></description>
			<content:encoded><![CDATA[<p>Starting a native application from your Java code is as simple as the following command:<br />
<code>Runtime.getRuntime().exec(&quot;notepad.exe&quot;);</code></p>
<p>Of course, this adds platform dependency to your code, and if you want your application to be portable it&#8217;s a good practice to avoid using the <em><strong>exec()</strong></em> method. But if it is necessary to initiate a native application for any reason, the way is easy and pretty straight-forward. The fact is that there&#8217;s always a case where executing a command of the operating system is the only way to achieve specific functionality. </p>
<p>In this case, independence can be approached if the command to be executed exist(with common or different syntax) in various operating systems. The first step is to find out the syntax of the command for every OS. Then within the code, you can use <em><strong>System.getProperty(&#8221;os.name&#8221;)</strong></em> to determine the OS that is being to host your Java application and finally use this information to execute the command using the corresponding syntax.</p>
<p>There is one more useful feature of the <em><strong>exec()</strong></em> method: it&#8217;s return value is an instance of the <strong><em>Process</em></strong> class, which can be used to get access to the actual process of the application initiated.  Check out the javadoc of the  <strong><em>Process</em></strong> class to find out more.</p>
]]></content:encoded>
			<wfw:commentRss>http://javawords.com/2008/04/04/start-a-native-application-from-your-java-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to use log4j logging API</title>
		<link>http://javawords.com/2008/01/08/how-to-use-log4j-logging-api/</link>
		<comments>http://javawords.com/2008/01/08/how-to-use-log4j-logging-api/#comments</comments>
		<pubDate>Tue, 08 Jan 2008 22:25:45 +0000</pubDate>
		<dc:creator>xfrag</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://javawords.com/2008/01/08/how-to-use-log4j-logging-api/</guid>
		<description><![CDATA[Logging is an important and pretty useful mechanism for every application. It can help developers to debug and improve their code or test it&#8217;s functionality. Apache has made a great work in this field with it&#8217;s tool named log4j. You can find more information about it here.
In this post I will provide a quick guide [...]]]></description>
			<content:encoded><![CDATA[<p>Logging is an important and pretty useful mechanism for every application. It can help developers to debug and improve their code or test it&#8217;s functionality. Apache has made a great work in this field with it&#8217;s tool named log4j. You can find more information about it <strong><a href="http://logging.apache.org/log4j/1.2/index.html">here</a></strong>.</p>
<p>In this post I will provide a quick guide to help you get up and started with log4j. But fist of all you&#8217;ll have to download the <strong><a href="http://logging.apache.org/log4j/1.2/download.html">latest version</a></strong>. Once you do, follow the instructions below:</p>
<p><strong>1.</strong> Add log4j&#8217;s jar(log4j-x.x.xx.jar where x.x.xx is the version of the release you&#8217;ve downloaded) file to your application&#8217;s classpath.</p>
<p><strong>2.</strong> Create a file named &#8220;log4j.properties&#8221; and place it in the default package of your source code(the root folder where all your code resides). Copy the following lines in it:<br />
<code># Log levels<br />
# Uncomment the following line to enable full loggin for every class<br />
#log4j.rootLogger=trace, stdout, R<br />
log4j.logger.gr.xfrag=trace, stdout, R<br />
<br />
# Console appender configuration<br />
log4j.appender.stdout=org.apache.log4j.ConsoleAppender<br />
# Pattern to output the caller&#039;s file name and line number.<br />
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout<br />
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n<br />
<br />
# Rolling File Appender<br />
log4j.appender.R=org.apache.log4j.RollingFileAppender<br />
# Path and file name to store the log file.<br />
log4j.appender.R.File=./logs/applog.log<br />
log4j.appender.R.MaxFileSize=500KB<br />
# Keep one backup file<br />
log4j.appender.R.MaxBackupIndex=1<br />
# Rolling File Appender layout<br />
log4j.appender.R.layout=org.apache.log4j.PatternLayout<br />
log4j.appender.R.layout.ConversionPattern=%d - %c - %p - %m%n</code><span id="more-20"></span><br />
Here is a quick explanation of what the above properties do:<br />
&#8220;log4j.logger.xxx&#8221; controls the loggers, where xxx is the package for witch you want to set log levels.<br />
The line<br />
<em>log4j.logger.foo.bar=trace, stdout, R</em><br />
means that you want every class under the <em>foo.bar</em> package to <strong>have a logging level</strong> of <em>trace</em>, and <strong>output the log</strong> to the standard output(the console) using a Console Appender named <em>stdout</em> and to a file using a Rolling File Appender named <em>R</em>.<br />
<br />
After that you define the appenders I mentioned earlier and set their properties.<br />
The lines<br />
<em>log4j.appender.stdout=org.apache.log4j.ConsoleAppender<br />
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout<br />
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) &#8211; %m%n</em><br />
define the ConsoleAppender named stdout, set it&#8217;s layout to PatternLayout and then assign a pattern for the output.</p>
<p>Following the same procedure the RollingFileAppender is defined, setting some additional properties: the file name of the log, the size of the log and the number of backup files to keep.</p>
<p><strong>3.</strong> The third and final step is about the code you&#8217;ll have to use. There&#8217;s not much to be said, this is the easiest part. Take a look of the class below:<br />
<code>import org.apache.log4j.Level;<br />
import org.apache.log4j.Logger;<br />
<br />
public class Main {<br />
&nbsp;&nbsp;&nbsp;&nbsp;private static Logger logger = Logger.getLogger(Main.class);<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;public static void main(String[] args) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;long time = System.currentTimeMillis();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;logger.info(&quot;main method called..&quot;);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;logger.info(&quot;another informative message&quot;);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;logger.warn(&quot;This one is a warning!&quot;);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;logger.log(Level.TRACE, <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;And a trace message using log() method.&quot;);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;long logTime = System.currentTimeMillis() - time;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;logger.debug(&quot;Time taken to log the previous messages: &quot; <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;+ logTime + &quot; msecs&quot;);<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Exception logging example:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String subs = &quot;hello&quot;.substring(6);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}catch (Exception e){<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;logger.error(&quot;Error in main() method:&quot;, e);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
}</code><br />
First you have to import <em>org.apache.log4j.Logger</em> and <em>org.apache.log4j.Level</em>(if you want to use the log() method). </p>
<p>Then instantiate a static <em>Logger</em> object using the static method <em>getLogger()</em> and provide your class as an argument.</p>
<p>To log messages through your class, you use the logging methods of the created Logger object. There are two ways to log messages:<br />
<strong>Directly</strong>, using the methods <em>trace()</em>, <em>debug()</em>, <em>info()</em>, <em>warn()</em>, <em>error()</em> and <em>fatal()</em>, providing the message as an argument.<br />
<strong>Using the <em>log()</em> method</strong>, providing the level of the message from the <em>Level</em> class and the message to log as arguments.</p>
<p>According to the configuration you provide in the properties file, a message will be logged &#8211; or not. For example, if you set the level to WARN, all messages of level WARN, ERROR and FATAL will be logged and everything else will be omitted. In other words, the hierarchy is:<br />
TRACE &lt; DEBUG &lt; INFO &lt; WARN &lt; ERROR &lt; FATAL.</p>
<p>Now, check out how the appenders output the messages according to their pattern. Here&#8217;s what you get at the console when you run the Main class:<br />
<code> INFO [main] (Main.java:12) - main method called..<br />
 INFO [main] (Main.java:13) - another informative message<br />
 WARN [main] (Main.java:14) - This one is a warning!<br />
TRACE [main] (Main.java:15) - And a trace message using log() method.<br />
DEBUG [main] (Main.java:19) - Time taken to log the previous messages: 0 msecs<br />
ERROR [main] (Main.java:26) - Error in main() method:<br />
java.lang.StringIndexOutOfBoundsException: String index out of range: -1<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at java.lang.String.substring(String.java:1938)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at java.lang.String.substring(String.java:1905)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at gr.xfrag.loggingexample.Main.main(Main.java:24)</code></p>
<p>And the same in the log file:<br />
<code>2008-01-10 15:39:46,724 - gr.xfrag.loggingexample.Main - INFO - main method called..<br />
2008-01-10 15:39:46,724 - gr.xfrag.loggingexample.Main - INFO - another informative message<br />
2008-01-10 15:39:46,724 - gr.xfrag.loggingexample.Main - WARN - This one is a warning!<br />
2008-01-10 15:39:46,724 - gr.xfrag.loggingexample.Main - TRACE - And a trace message using log() method.<br />
2008-01-10 15:39:46,724 - gr.xfrag.loggingexample.Main - DEBUG - Time taken to log the previous messages: 0 msecs<br />
2008-01-10 15:39:46,724 - gr.xfrag.loggingexample.Main - ERROR - Error in main() method:<br />
java.lang.StringIndexOutOfBoundsException: String index out of range: -1<br />
&nbsp;&nbsp;at java.lang.String.substring(String.java:1938)<br />
&nbsp;&nbsp;at java.lang.String.substring(String.java:1905)<br />
&nbsp;&nbsp;at gr.xfrag.loggingexample.Main.main(Main.java:24)</code><br />
Besides the deferences of the formating, notice how long it takes to log those messages &#8211; zero milliseconds on my machine. If you ask me, I&#8217;ll say that speed is the most impressive feature of log4j..</p>
<p>I hope the above information will help you get started with log4j and logging in general. You can learn more by reading the manual located at log4j&#8217;s home page: <strong><a href="http://logging.apache.org/log4j/1.2/manual.html">Log4j Manual</a></strong></p>
<p>I also uploaded the example code as a NetBeans 6.0 project. Use the following link to download the zip archive:<br />
<strong><a href='http://javawords.com/wp-content/uploads/2008/01/logging-example.zip' title='Log4j Example NetBeans Project'>Log4j Example NetBeans Project</a></strong></p>
]]></content:encoded>
			<wfw:commentRss>http://javawords.com/2008/01/08/how-to-use-log4j-logging-api/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
	</channel>
</rss>
