<?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>Binary Tides &#187; Java</title>
	<atom:link href="http://www.binarytides.com/blog/category/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.binarytides.com/blog</link>
	<description></description>
	<lastBuildDate>Sat, 24 Jul 2010 05:31:26 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Speed up Swing GUI Java Apps</title>
		<link>http://www.binarytides.com/blog/speed-up-swing-gui-java-apps/</link>
		<comments>http://www.binarytides.com/blog/speed-up-swing-gui-java-apps/#comments</comments>
		<pubDate>Wed, 26 May 2010 06:04:17 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://www.binarytides.com/blog/?p=178</guid>
		<description><![CDATA[I was writing a small database application using Java and Apache Derby in Netbeans. Soon, felt that the speed should be improved as it was slow compared to a c++ or python app.
There are a few tweaks which could give significant improvement in the speed of a swing gui application.

1. Multithreading &#8211; Proper use of [...]]]></description>
			<content:encoded><![CDATA[<p>I was writing a small database application using Java and Apache Derby in Netbeans. Soon, felt that the speed should be improved as it was slow compared to a c++ or python app.</p>
<p>There are a few tweaks which could give significant improvement in the speed of a swing gui application.</p>
<p><span id="more-178"></span></p>
<p>1. <strong>Multithreading</strong> &#8211; Proper use of threads can significantly improve the applications speed and also its &#8220;perceived speed&#8221; or responsiveness.</p>
<p>Time consuming tasks should not be done inside an event handler since this will effect the appearance of the gui component whose event handler this is. If a lengthy task for example is done inside the event handler of a menu then the menu may stay until the task is complete or if its a button then if may freeze i.e. stay pressed until the task is complete. This results into reduced usability.</p>
<p>Various solutions include SwingWorker  , Threads , invokeLater. E.g.Time-consuming tasks like I/O operatings , Database operations or creation of some kind of object or child window should be put in a thread. Lengthy tasks inside an event handler should be moved into a SwingWorker Thread or invokeLater so that the handler can return immediately keeping the GUI component responsive.</p>
<p>A Thread :</p>
<pre class="brush: java;">
new Thread () {
    public void run () {
        //Lengthy Code
    }
}.start ();
</pre>
<p>A SwingWorker :</p>
<pre class="brush: java;">
</pre>
<p>invokeLater :</p>
<pre class="brush: java;">
</pre>
<p>Multithreaded applications when properly designed give even better results on multicore processors. In any case threads will definitely increase the responsiveness or perceived speed of the gui in particular.</p>
<p>2. <strong>JVM Switches</strong> &#8211; jvm switches can be used to modify parameters like minimum and maximum memory that a java app can use. For a complete list of jvm options look <a href="http://blogs.sun.com/watt/resource/jvm-options-list.html">here</a>.<br />
Example :</p>
<pre class="brush: cpp;">
java -jar -Xverify:none -Xms32m -XX:PermSize=32m -Dsun.java2d.noddraw=true application.jar
</pre>
<p>3. Creating objects before they are actually needed &#8211; Object creation is a process that takes time. For example creation of a window frame object . So a window object can be created beforehand and kept hidden. When needed it can be simply displayed by calling the show() method. This increases the speed of the application.</p>
<p>4. <strong>Reusing Objects</strong> &#8211; Reusing objects prevents creating them again and again and slowing down the app. This totally depends on the logic of the application how objects can be kept in memory and reused when necessary.</p>
<p>5. <strong>StringBuffer versus String</strong> &#8211; Strings are immutable means they cannot be changed once they are created. Hence any operation like concatenation done on a string results in creation of more string objects.<br />
StringBuffer on the other hand is an object which can be changed and hence any operation like concatenation results into the same object being changed instead of creation of a new one.</p>
<p>Hence StringBuffer is faster than String. If the application uses too many Strings and string operations then StringBuffer should be used to gain speed.</p>
<p>Apart from these other techniques for speed gain include using native functions , AOT (Ahead of Time compilers) , JIT (Just in Time compilers) etc.</p>
<p>If hardware acceleration is enabled then using opengl can improve GUI speed and can be enabled using the switch :<br />
-Dsun.java2d.opengl=true</p>
<img src="http://www.binarytides.com/blog/?ak_action=api_record_view&id=178&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.binarytides.com/blog/speed-up-swing-gui-java-apps/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Install Apache Tomcat 5.5 on Ubuntu 8.04 Hardy Heron</title>
		<link>http://www.binarytides.com/blog/install-apache-tomcat-5-5-on-ubuntu-8-04-hardy-heron/</link>
		<comments>http://www.binarytides.com/blog/install-apache-tomcat-5-5-on-ubuntu-8-04-hardy-heron/#comments</comments>
		<pubDate>Sat, 21 Mar 2009 16:49:00 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Tomcat]]></category>
		<category><![CDATA[Ubuntu]]></category>

		<guid isPermaLink="false">http://www.binarytides.com/blog/?p=39</guid>
		<description><![CDATA[To install and run Tomcat on Ubuntu (8.04 over here) the following packages should be installed from synaptic :
1.  tomcat5.52. tomcat5.5-webapps3. sun-java6-jdk
They can be installed from command line using the command sudo apt-get

After synaptic finishes installing them open /etc/default/tomcat5.5 and edit it as follows :
Find :#JAVA_HOME=/usr/lib/jvm/java-6-sun
Change : Remove the # so that it is [...]]]></description>
			<content:encoded><![CDATA[<p>To install and run Tomcat on Ubuntu (8.04 over here) the following packages should be installed from synaptic :</p>
<p>1.  tomcat5.5<br />2. tomcat5.5-webapps<br />3. sun-java6-jdk</p>
<p>They can be installed from command line using the command sudo apt-get</p>
<p><span id="more-39"></span></p>
<p>After synaptic finishes installing them open /etc/default/tomcat5.5 and edit it as follows :</p>
<p>Find :<br />#JAVA_HOME=/usr/lib/jvm/java-6-sun</p>
<p>Change : Remove the # so that it is uncommented.<br />JAVA_HOME=/usr/lib/jvm/java-6-sun</p>
<p>It points to the java home which can be java-6-sun or any other java .</p>
<p>Then restart tomcat5.5 from command line like this :</p>
<p>enlightened@enlightened-desktop:~$ sudo /etc/init.d/tomcat5.5 restart<br />* Stopping Tomcat servlet engine tomcat5.5                              [ OK ]<br />* Starting Tomcat servlet engine tomcat5.5                              [ OK ]</p>
<p>Now open in your browser the url :</p>
<p>http://localhost:8180/</p>
<p>And the welcome page of Apache Tomcat should appear.</p>
<p>The file<br />/usr/share/tomcat5.5/conf/tomcat-users.xml<br />has the default username and password listed which is username : tomcat and password : tomcat.</p>
<p>On the tomcat welcome page Tomcat Administration link is on the left :<br />http://localhost:8180/admin</p>
<p>Opening this page might show the error message :<br />HTTP Status 403 &#8211; Access to the requested resource has been denied</p>
<p>To fix the above issue open the file<br />/usr/share/tomcat5.5/conf/tomcat-users.xml</p>
<p>sudo gedit /usr/share/tomcat5.5/conf/tomcat-users.xml</p>
<p>and add a role called admin and user called admin with that role :</p>
<p>&lt;role rolename=&#8221;admin&#8221;/&gt;<br />&lt;user username=&#8221;admin&#8221; password=&#8221;tomcat&#8221; roles=&#8221;tomcat,admin&#8221;/&gt;</p>
<p>Restart tomcat from command line as shown above.</p>
<p>Now log in to the administration panel :<br />http://localhost:8180/admin</p>
<p>with username : admin and password : tomcat.<br />The login should take you to the admin panel without any error.</p>
<p>To the left is another link of Tomcat Manager :<br />http://localhost:8180/manager/html</p>
<p>which might still show the error of Access denied.</p>
<p>To fix this issue simply login to the admin panel and create a role called manager (if it doesnt exist) and assign this role to a non-admin user like tomcat or create a new user.</p>
<p>Then log in to the Tomcat Manager page and it should open fine without any errors.</p>
<p>Now /usr/share/tomcat-webapps is the folder that contains the jsp files/webapps which are accessible from localhost:8180 url.</p>
<p>To change the directory of webapps to say one in /home/username/tomcat</p>
<p>Open : /usr/share/tomcat5.5-webapps/ROOT.xml</p>
<p>Look for :<br />Context path=&#8221;/&#8221; docBase=&#8221;/usr/share/tomcat5.5-webapps/ROOT&#8221;<br />and change the docBase value to the folder where the tomcat webapps would be.<br />e.g.</p>
<p>Context path=&#8221;/&#8221; docBase=&#8221;/home/enlightened/tomcat&#8221;<br />or<br />Context path=&#8221;/&#8221; docBase=&#8221;/var/www/tomcat&#8221;</p>
<p>Save the file and restart tomcat.</p>
<p>One more thing you can do is copy the contents of the /usr/share/tomcat5.5-webapps/ROOT directory to this new directory and then open the url localhost:8180 to check that everything is working fine. The admin and manager pages on the left and the jsp examples also should all open fine as previously.</p>
<img src="http://www.binarytides.com/blog/?ak_action=api_record_view&id=39&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.binarytides.com/blog/install-apache-tomcat-5-5-on-ubuntu-8-04-hardy-heron/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Execute an SQL Script in JDBC</title>
		<link>http://www.binarytides.com/blog/execute-an-sql-script-in-jdbc/</link>
		<comments>http://www.binarytides.com/blog/execute-an-sql-script-in-jdbc/#comments</comments>
		<pubDate>Fri, 04 Apr 2008 14:21:00 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://www.binarytides.com/blog/?p=24</guid>
		<description><![CDATA[The SQL Script should have comments starting with &#8211; or &#8212; only on new lines and each command should end with a ; .
Reading a sql file and putting all of it in a string variable and feeding to the execute command would result in an exception. All instructions must be executed individually. A proper [...]]]></description>
			<content:encoded><![CDATA[<p>The SQL Script should have comments starting with &#8211; or &#8212; only on new lines and each command should end with a ; .</p>
<p>Reading a sql file and putting all of it in a string variable and feeding to the execute command would result in an exception. All instructions must be executed individually. A proper sql script should have all instructions starting on a newline and all comments on a newline. Start with an empty string. Start reading the lines ; if a line has a &#8211; as the first character then continue else add it to the query string. Now if the last character of the query string is a ; then a command is complete so execute the query and make the query string empty and loop up. Thats it</p>
<p><span id="more-24"></span></p>
<p>Executing an sql script in jdbc with the following code should work &#8230;</p>
<pre class="brush: java;">
//Now read line bye line
String thisLine, sqlQuery;
try {
    sqlQuery = &quot;&quot;;
    while ((thisLine = d.readLine()) != null)
    {
        //Skip comments and empty lines
        if(thisLine.length() &gt; 0 &amp;&amp; thisLine.charAt(0) == '-' || thisLine.length() == 0 )
            continue;
        sqlQuery = sqlQuery + &quot; &quot; + thisLine;
        //If one command complete
        if(sqlQuery.charAt(sqlQuery.length() - 1) == ';') {
            sqlQuery = sqlQuery.replace(';' , ' '); //Remove the ; since jdbc complains
            try {
                stmt.execute(sqlQuery);
            }
            catch(SQLException ex) {
                JOptionPane.showMessageDialog(null, &quot;Error Creating the SQL Database : &quot; + ex.getMessage());
            }
            catch(Exception ex) {
                JOptionPane.showMessageDialog(null, &quot;Error Creating the SQL Database : &quot; + ex.getMessage());
            }
            sqlQuery = &quot;&quot;;
        }
    }
}
catch(IOException ex) {
}
catch(Exception ex) {
    JOptionPane.showMessageDialog(null, &quot;Error Creating the SQL Database : &quot; + ex.getMessage());
}
</pre>
<p>where d could be BufferedReader and c could be a Statement object.</p>
<img src="http://www.binarytides.com/blog/?ak_action=api_record_view&id=24&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.binarytides.com/blog/execute-an-sql-script-in-jdbc/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Reading Writing and Saving Configurations to a File in Java</title>
		<link>http://www.binarytides.com/blog/reading-writing-and-saving-configurations-to-a-file-in-java/</link>
		<comments>http://www.binarytides.com/blog/reading-writing-and-saving-configurations-to-a-file-in-java/#comments</comments>
		<pubDate>Wed, 02 Apr 2008 07:24:00 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://www.binarytides.com/blog/?p=23</guid>
		<description><![CDATA[Configuration files are used in applications to store settings, user preferences and other configuration parameters of an application so that. When configurations are changed from within the application these need to be saved so that next time the application starts with these new settings.

In Java reading and writing to a configuration is pretty simple and [...]]]></description>
			<content:encoded><![CDATA[<p>Configuration files are used in applications to store settings, user preferences and other configuration parameters of an application so that. When configurations are changed from within the application these need to be saved so that next time the application starts with these new settings.</p>
<p><span id="more-23"></span></p>
<p>In Java reading and writing to a configuration is pretty simple and straight forward.</p>
<p>A configuration file can store data in many ways. One simple technique is :</p>
<p>key=value</p>
<p>Apart from this XML can also be used to save parameters.</p>
<p>The following two methods should serve the purpose of reading and saving configurations :</p>
<pre class="brush: java;">
//Writing and Saving Configurations
private void setPreference(String Key, String Value) {
    Properties configFile = new Properties();
    try {
        InputStream f = new FileInputStream(&quot;configuration.xml&quot;);
        configFile.loadFromXML(f);
        f.close();
    }
    catch(IOException e) {
    }
    catch(Exception e) {
        JOptionPane.showMessageDialog(null, e.getMessage());
    }
    configFile.setProperty(Key, Value);
    try {
        OutputStream f = new FileOutputStream(&quot;configuration.xml&quot;);
        configFile.storeToXML(f,&quot;Configuration file for the Profit System&quot;);
    }
    catch(Exception e) {
    }
}
//Reading Configurations
private static String getPreference(String Key) {
    Properties configFile = new Properties();
    try {
        InputStream f = new FileInputStream(&quot;configuration.xml&quot;);
        configFile.loadFromXML(f);
        f.close();
    }
    catch(IOException e) {
    }
    catch(Exception e) {
        JOptionPane.showMessageDialog(null , e.getMessage());
    }
    return (configFile.getProperty(Key));
}
</pre>
<p>Instead the storeFromXML and loadFromXML store and load methods can be used to simply store the data in the key=value format instead of xml format.</p>
<img src="http://www.binarytides.com/blog/?ak_action=api_record_view&id=23&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.binarytides.com/blog/reading-writing-and-saving-configurations-to-a-file-in-java/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
