<?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>Conor's Blog &#187; Java</title>
	<atom:link href="http://blog.macaoidh.name/tag/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.macaoidh.name</link>
	<description>PHP, Music, Linux</description>
	<lastBuildDate>Mon, 05 Jul 2010 13:25:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Java &#8211; Drawing Squares</title>
		<link>http://blog.macaoidh.name/2009/12/05/java-drawing-squares/</link>
		<comments>http://blog.macaoidh.name/2009/12/05/java-drawing-squares/#comments</comments>
		<pubDate>Sat, 05 Dec 2009 03:28:07 +0000</pubDate>
		<dc:creator>Conor</dc:creator>
				<category><![CDATA[College]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Languages]]></category>
		<category><![CDATA[drawing]]></category>
		<category><![CDATA[icsp]]></category>
		<category><![CDATA[reges]]></category>
		<category><![CDATA[regular expression]]></category>
		<category><![CDATA[shape]]></category>
		<category><![CDATA[squares]]></category>

		<guid isPermaLink="false">http://blog.macaoidh.name/?p=284</guid>
		<description><![CDATA[This weeks ICSP assignment was to write a program which draws squares. The task was to draw squares from a set list in an array, but I opted rather to accept a command line argument in my script. To write this I had to delve back into regular expression, which I hate because I don&#8217;t [...]]]></description>
			<content:encoded><![CDATA[<p>This weeks ICSP assignment was to write a program which draws squares. The task was to draw squares from a set list in an array, but I opted rather to accept a command line argument in my script. To write this I had to delve back into regular expression, which I hate because I don&#8217;t understand!</p>
<p>The program source is below. You should execute the program from the command line giving the with and height of the square you wish to draw, for example:</p>
<pre class="brush: bash">
java Squares 4x4
</pre>
<p>And the code:</p>
<pre class="brush: java">
import java.lang.String;
import java.util.regex.*;

public class Squares{
        public int dimensions[];

        public Squares(String[] args){
                if(args.length==1){
                        Pattern form=Pattern.compile("^\\S+x\\S+$");
                        Matcher fit=form.matcher(args[0]);
                        if(fit.matches()){
                                try{
                                        String[] dims=args[0].split("x");
                                        int width=Integer.parseInt(dims[0]);
                                        int height=Integer.parseInt(dims[1]);
                                        this.dimensions=new int[] {width,height};
                                }
                                catch(NumberFormatException e){
                                        System.err.println("Argument must be a string, width by height, in the form 6x6");
                                        System.exit(1);
                                }
                        }
                        else{
                                System.out.println("Argument must be a string, width by height, in the form 6x6");
                                System.exit(1);
                        }
                }
                else{
                        System.err.println("Please supply an argument, which must be a string, width by height, in the form 6x6");
                        System.exit(0);
                }
        }

        public String draw(){
                int i;
                int z;
                int width=this.dimensions[0];
                int height=this.dimensions[1];
                String square="";

                for(i=0;i&lt;height ;i++){
                        for(z=0;z&lt;width;z++){
                                if((i==0||i==height-1)&#038;&#038;(z==0||z==width-1))
                                        square+="+";
                                else if(z==0||z==width-1)
                                        square+="|";
                                else if((i==0||i==height-1))
                                        square+="-";
                                else
                                        square+=" ";
                        }
                        square+="\n";
                }

                return square;
        }

        public static void main(String[] args){
                Squares square=new Squares(args);
                String draw=square.draw();
                System.out.println(draw);
        }
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.macaoidh.name/2009/12/05/java-drawing-squares/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java &#8211; Calculate Taxi Fares</title>
		<link>http://blog.macaoidh.name/2009/12/02/java-calculate-taxi-fares/</link>
		<comments>http://blog.macaoidh.name/2009/12/02/java-calculate-taxi-fares/#comments</comments>
		<pubDate>Wed, 02 Dec 2009 00:21:25 +0000</pubDate>
		<dc:creator>Conor</dc:creator>
				<category><![CDATA[College]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Languages]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[fare]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[Scratch]]></category>
		<category><![CDATA[taxi]]></category>

		<guid isPermaLink="false">http://blog.macaoidh.name/?p=281</guid>
		<description><![CDATA[This week my ICSP assignment was to calculate taxi fares. The task was: Using the fare table write a program that calculates the fares and test if it is correct. Upload your .java or your Scratch file. A template file for java will be provided. (Hint, only the boundary values needs to be tested. The [...]]]></description>
			<content:encoded><![CDATA[<p>This week my ICSP assignment was to calculate taxi fares. The task was:</p>
<blockquote><p>Using the fare table write a program that calculates the fares and test if it is correct. Upload your .java or your Scratch file. A template file for java will be provided. (Hint, only the boundary values needs to be tested. The fare table is also presented in the slides. The compile command for java should be &#8220;javac TaxiRide.java&#8221;)</p>
<table border="0">
<tbody>
<tr>
<th colspan="2">Fare table</th>
</tr>
<tr>
<th>Distance (km)</th>
<th>Cost (€)</th>
</tr>
<tr>
<td>&gt;50</td>
<td>30</td>
</tr>
<tr>
<td>50-100</td>
<td>25, plus 9 for each km &gt; 50</td>
</tr>
<tr>
<td>100 &#8211; 2000</td>
<td>190, plus 8 for each km &gt; 100</td>
</tr>
<tr>
<td>&gt; 2000</td>
<td>1800</td>
</tr>
</tbody>
</table>
</blockquote>
<p>It wasn&#8217;t the hardest task. The only thing that confused me was the Java switch syntax. In PHP you can have conditions in a switch statement such as:</p>
<pre class="brush:php">
&lt;?php
switch($num){
        case ($num&lt;10):
                // blah
        break;
        case ($num&lt;100):
                // blah
        break;
        default:
                //blah
?&gt;
</pre>
<p>I couldn&#8217;t get Java to accept anything other than a value for the variable bring switched. So I did it the old-fashioned way! My code is below&#8230;</p>
<pre class="brush:java">
public class TaxiRide{
        public int distance=0;

        public TaxiRide(String arg){
                try{
                        int distance=Integer.parseInt(arg);
                        this.distance=distance;
                }
                catch(NumberFormatException e){
                        System.err.println("Arguement must be an integer representing the distance traveled");
                        System.exit(1);
                }
        }

        public int calculateFare(){
                int distance=this.distance;
                int fare=0;
                int i=0;
                if(distance&lt;50){
                        return 30;
                }
                if(distance%lt;100){
                        fare=25;
                        distance-=50;
                        for(i=0;i&lt;distance;i++){
                                fare+=9;
                        }
                        return fare;
                }
                if(distance&lt;2000){
                        fare=190;
                        distance-=100;
                        for(i=0;i&lt;distance;i++){
                                fare+=8;
                        }
                        return fare;
                }
                return 1800;
        }

        public static void main(String[] args){
                TaxiRide taxiRide = new TaxiRide(args[0]);
                int fare=taxiRide.calculateFare();
                System.out.println("The fare is: "+fare+" euro for traveling "+taxiRide.distance+"kM\n Thank you\n");
        }
}
</pre>
<p>I feel i am learning a lot from this course. Lately I have been experimenting in a few different languages. I added a bit to the Scratch Linux installer, written in Perl, so that it added an icon in the applications menu during installation. I&#8217;ve also been messing around with bash, trying to automate some of my most common commands&#8230; was thinking of writing a small bash script that backed up my data when my external hard drive is plugged in, similar to Time Machine on OSX.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.macaoidh.name/2009/12/02/java-calculate-taxi-fares/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java &#8211; Project Euler Multiples</title>
		<link>http://blog.macaoidh.name/2009/11/21/java-project-euler-multiples/</link>
		<comments>http://blog.macaoidh.name/2009/11/21/java-project-euler-multiples/#comments</comments>
		<pubDate>Sat, 21 Nov 2009 18:32:05 +0000</pubDate>
		<dc:creator>Conor</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Languages]]></category>
		<category><![CDATA[1000]]></category>
		<category><![CDATA[3]]></category>
		<category><![CDATA[5]]></category>
		<category><![CDATA[euler]]></category>
		<category><![CDATA[icsp]]></category>
		<category><![CDATA[multiples]]></category>
		<category><![CDATA[project]]></category>
		<category><![CDATA[project euler]]></category>

		<guid isPermaLink="false">http://blog.macaoidh.name/?p=272</guid>
		<description><![CDATA[This weeks ICSP assignment was one of the Project Euler challenges. Here is the task: This weeks Bonus Task is to solve the First Problem using Java: &#8220;If we list all the natural numbers below 10 that are multiples of 3 or 5, we * get 3, 5, 6 and 9. The sum of these [...]]]></description>
			<content:encoded><![CDATA[<p>This weeks ICSP assignment was one of the <a href="http://projecteuler.net/index.php?section=about">Project Euler</a> challenges. Here is the task:</p>
<p>This weeks Bonus Task is to solve the First Problem using Java:<br />
&#8220;If we list all the natural numbers below 10 that are multiples of 3<br />
or 5, we * get 3, 5, 6 and 9. The sum of these multiples is 23.</p>
<p>Find the sum of all the multiples of 3 or 5 below 1000.&#8221;</p>
<p>In the code below, I have achieved this objective and also expanded on it. My program allows you to pass on two multiples and a max integer value to the program for which to calculate the results. You can execute:</p>
<pre class="brush: java">java Multiples 3 5 1000</pre>
<p>That command will find the sum of multiples of 3 and 5 below 1000, as stated in the task. You can also change the values to calculate different sums. The code for Multiples.java is below:</p>
<pre class="brush: java">/*
 * Developer:   Conor Mac Aoidh
 * Homepage:    http://blog.macaoidh.name/2009/11/21/java-project-euler-multiples/
 */

public class Multiples{
        public static void main(String[] args){
                Multiples multiples=new Multiples();
                int one=0;
                int two=0;
                int max=0;
                if(args.length==3){
                        try{
                                one=Integer.parseInt(args[0]);
                                two=Integer.parseInt(args[1]);
                                max=Integer.parseInt(args[2]);
                        }
                        catch(NumberFormatException e){
                                System.err.println("Argument must be an integer");
                                System.exit(1);
                        }
                }
                else
                        System.out.println("You must supply integers in the format:\n multiple_one multiple_two max_integer");

                int sum=multiples.sum(one,two,max);

                System.out.println("Find the sum of all the multiples of "+one+" or "+two+" below "+max);
                System.out.println("Sum:"+sum);
        }

        public int sum(int one,int two,int max){
                int i=0;
                int sum=0;
                for(i=0;i&lt;max ;i++){
                        if(i % one==0)
                                sum=sum+i;
                        if(i % two==0)
                                sum=sum+i;
                }
                return sum;
        }
}</pre>
<p>Simple as that!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.macaoidh.name/2009/11/21/java-project-euler-multiples/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Java Visual Hello World Popup</title>
		<link>http://blog.macaoidh.name/2009/11/15/java-visual-hello-world-popup/</link>
		<comments>http://blog.macaoidh.name/2009/11/15/java-visual-hello-world-popup/#comments</comments>
		<pubDate>Sun, 15 Nov 2009 13:07:19 +0000</pubDate>
		<dc:creator>Conor</dc:creator>
				<category><![CDATA[College]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Languages]]></category>
		<category><![CDATA[add text to jframe]]></category>
		<category><![CDATA[hello world]]></category>
		<category><![CDATA[jar]]></category>
		<category><![CDATA[jbutton]]></category>
		<category><![CDATA[jframe]]></category>
		<category><![CDATA[jpanel]]></category>
		<category><![CDATA[popup]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[visual]]></category>

		<guid isPermaLink="false">http://blog.macaoidh.name/?p=261</guid>
		<description><![CDATA[I spent yesterday going a bit deeper into Java with one of my lecturers. I decided to try and write a more complicated hello world app. It&#8217;s quite simple really once you get around the strict OOP and different syntax&#8230; This is how I did it: First you need to import some of the java [...]]]></description>
			<content:encoded><![CDATA[<p>I spent yesterday going a bit deeper into Java with one of my lecturers. I decided to try and write a more complicated hello world app. It&#8217;s quite simple really once you get around the strict OOP and different syntax&#8230; This is how I did it:</p>
<p>First you need to import some of the java libraries that we will be using:</p>
<pre class="brush: java">
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
</pre>
<p>Then add the class, Popup. Make sure to name the file Popup.java</p>
<pre class="brush: java">
public class Popup{
private JFrame frame=new JFrame("Hello!");
private JPanel panel=new JPanel();
private JButton button=new JButton("Say Hello");
private JLabel label=new JLabel("Click the button to say hello...",SwingConstants.CENTER);
private ButtonListener buttonListener=new ButtonListener();
</pre>
<p>What that actually means is create a new instance of JFrame, JPanel, JButton and JLabel and store them in the frame,panel, button and label variables. As for the ButtonListener, we will deal with that later. Now we will deal with the launchFrame function which does what it says on the tin &#8211; launches the frame! Basically it just connects all of the pieces of information stored into the variables above and displays them together.</p>
<pre class="brush: java">
public void launchFrame(){
int height=400, width=600;
frame.setSize(width,height);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
</pre>
<p>Above we create two new integers which store the width and height of our frame. The next line sets the variables to the frame size. The final line tells the Java application to close when the window, or JFrame, is closed. Now that we have the configuration out of the way we can move on to adding the variables to the frame.</p>
<pre class="brush: java">
frame.getContentPane().add(label,BorderLayout.CENTER);
panel.add(button);
frame.getContentPane().add(panel,BorderLayout.WEST);
button.addActionListener(buttonListener);
frame.setVisible(true);
}
</pre>
<p>The code above adds the label to the frame, then adds the button to the panel and then the panel to the frame. Once again disregard the button.addAction.. line for now. The final line simply sets the frame to visible so we can see it.</p>
<p>Now that we have the frame settled, we need to add our essential main function to start up the execution:</p>
<pre class="brush: java">
public static void main(String args[]){
Popup popup=new Popup();
popup.launchFrame();
}
}
</pre>
<p>This code basically creates an instance of our class, Popup and then executes the function launchFrame which we have created in that class.</p>
<p>That&#8217;s the guts of it, but now we need to talk about these lines:</p>
<pre class="brush: java">
private ButtonListener buttonListener=new ButtonListener();</pre>
<pre class="brush: java">
 public void launchFrame(){
....
button.addActionListener(buttonListener);
</pre>
<p>Basically we have previously created a button, but these lines define what happens when the button is pressed. The second line of code tells the machine to listen out for the clicking of the button, and when that happens to execute the variable buttonListener. We have created the variable buttonListener, which creates a new instance of the ButtonListener class which we have yet to create.</p>
<p>That is the next step, to create that class. Create a new file called ButtonListener.java and add this code:</p>
<pre class="brush: java">
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;</pre>
<pre class="brush: java">
public class ButtonListener implements ActionListener{
</pre>
<p>The first four lines import the libraries that we are using. The final line is important. It states that our class, ButtonListener is implementing the ActionListener class. Below is the content of our new class:</p>
<pre class="brush: java">
private JFrame pressed=new JFrame("Hello World!");</pre>
<pre class="brush: java">
public void actionPerformed(ActionEvent actionEvent){
</pre>
<p>First, we create a new JFrame for our popup just like we did in the other file. The function actionPerformed is  a function from the ActionListener class that we are implementing. The parameters for the function are predefined so we need them. The rest of the code is very similar to the previous code so I won&#8217;t explain it in depth. Basically it just configures the new frame, adds the label and then displays it:</p>
<pre class="brush: java">
int height=160, width=200;
pressed.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel pressedtext=new JLabel("Hello World!",SwingConstants.CENTER);
pressed.getContentPane().add(pressedtext,BorderLayout.CENTER);
pressed.setSize(width,height);
pressed.setVisible(true);
}</pre>
<p>Save that file and then compile both files by executing:</p>
<pre class="brush: bash">
javac ButtonListener.java Popup.java
</pre>
<p>Now if you run the program you should see a nice visual dialogue:</p>
<pre class="brush: bash">
java Popup
</pre>
<p>You can download a jar archive of the program <a href="http://files.macaoidh.name/Download/Java-Popup-Hello-World">here</a>.</p>
<p>The full code is below. Popup.java:</p>
<pre class="brush: java">
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;

public class Popup{
        private JFrame frame=new JFrame("Hello!");
        private JPanel panel=new JPanel();
        private JButton button=new JButton("Say Hello");
        private JLabel label=new JLabel("Click the button to say hello...",SwingConstants.CENTER);
        private ButtonListener buttonListener=new ButtonListener();

        public void launchFrame(){
                int height=400, width=600;
                frame.setSize(width,height);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.getContentPane().add(label,BorderLayout.CENTER);
                panel.add(button);
                frame.getContentPane().add(panel,BorderLayout.WEST);
                button.addActionListener(buttonListener);
                frame.setVisible(true);
        }

        public static void main(String args[]){
                Popup popup=new Popup();
                popup.launchFrame();
        }
}
</pre>
<p>ButtonListener.java:</p>
<pre class="brush: java">
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;

public class ButtonListener implements ActionListener{
        private JFrame pressed=new JFrame("Hello World!");

        public void actionPerformed(ActionEvent actionEvent){
                int height=160, width=200;
                pressed.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                JLabel pressedtext=new JLabel("Hello World!",SwingConstants.CENTER);
                pressed.getContentPane().add(pressedtext,BorderLayout.CENTER);
                pressed.setSize(width,height);
                pressed.setVisible(true);
        }

}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.macaoidh.name/2009/11/15/java-visual-hello-world-popup/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>ICSP Week One Assignment</title>
		<link>http://blog.macaoidh.name/2009/10/25/icsp-week-one-assignment/</link>
		<comments>http://blog.macaoidh.name/2009/10/25/icsp-week-one-assignment/#comments</comments>
		<pubDate>Sun, 25 Oct 2009 20:38:05 +0000</pubDate>
		<dc:creator>Conor</dc:creator>
				<category><![CDATA[College]]></category>
		<category><![CDATA[Games]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Languages]]></category>
		<category><![CDATA[caputre the flag]]></category>
		<category><![CDATA[icsp]]></category>
		<category><![CDATA[Scratch]]></category>

		<guid isPermaLink="false">http://blog.macaoidh.name/?p=250</guid>
		<description><![CDATA[I started a course in UCD last week called &#8220;Introduction to Computer Science and Programing&#8221;, or ICSP. It&#8217;s gonna be going on for three months and then I will have a big exam which I will probably fail! The whole course centers around the use of Java, I&#8217;m not sure yet if we are going [...]]]></description>
			<content:encoded><![CDATA[<p>I started a course in UCD last week called &#8220;Introduction to Computer Science and Programing&#8221;, or <a href="http://www.csi.ucd.ie/content/introduction-computer-science-and-programming-icsp2009">ICSP</a>. It&#8217;s gonna be going on for three months and then I will have a big exam which I will probably fail!</p>
<p>The whole course centers around the use of Java, I&#8217;m not sure yet if we are going to be using anything else. We haven&#8217;t actually even started with Java yet, but we are using this program called <a href="http://scratch.mit.edu/">Scratch</a> to program our Java for us! I wouldn&#8217;t say that it is the Dream-weaver of Java, far from it. In fact I think that it is a great method of getting into Java. It has a set of preconfigured code which you can arrange into blocks, which then run &#8211; quite similar to the source of Java actually except simplified. It also eliminates the need for compiling.</p>
<p>Last week I went to my first &#8220;lecture&#8221; which was good enough, but I learned something valuable that day; computer hardware is not the road that I want to go down! I didn&#8217;t really enjoy the hardware aspect of the day but when it came to the Scratch lessons I was flying! For our first assignment we had to write a game in Scratch, which is essentially it&#8217;s own unique programing language. The game is called &#8220;Capture the Flag&#8221;. I was actually flat out all week and didn&#8217;t get a chance to start work on it until today, which is coincidentally the deadline for it&#8217;s completion! I spent about three hours making the game, here&#8217;s the manual and the game:</p>
<h3>Capture The Flag</h3>
<p><strong>Overview</strong><br />
Capture the flag is a simple two player game which involves stealing the flag from the opposite colours corner.</p>
<p><strong>Controls</strong><br />
Blue Player<br />
Up Arrow Key = Move Up<br />
Down Arrow Key = Move Down<br />
Left Arrow Key = Move Left<br />
Right Arrow Key = Move Right</p>
<p>Red Player<br />
W Key = Move Up<br />
S Key = Move Down<br />
A Key = Move Left<br />
D Key = Move Right</p>
<p><strong>Rules</strong><br />
To gain a point a player must capture the opposite colour flag and return it to their base (the starting point).</p>
<p>If a player is in possession of a flag and his opponent touches him the flag is returned to it&#8217;s base and the player to his base.</p>
<p>To win a player must gain three points.</p>
<p><strong>Faults</strong><br />
The one fault with the game is that, at least on my computer, when one player is using the keyboard to move the other player cannot move simultaneously. Although this is a fault it does also make the game a bit more interesting because the players have to fight even to move!</p>
<p><applet id='ProjectApplet' style='display:block' code='ScratchApplet' codebase='http://scratch.mit.edu/static/misc' archive='ScratchApplet.jar' height='387' width='482'>
<param name='project' value='../../static/projects/ConorMacAoidh/732336.sb'></param></applet> <a href='http://scratch.mit.edu/projects/ConorMacAoidh/732336'>Learn more about this project</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.macaoidh.name/2009/10/25/icsp-week-one-assignment/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creating A Jar Archive</title>
		<link>http://blog.macaoidh.name/2009/04/27/creating-a-jar-archive/</link>
		<comments>http://blog.macaoidh.name/2009/04/27/creating-a-jar-archive/#comments</comments>
		<pubDate>Mon, 27 Apr 2009 21:42:21 +0000</pubDate>
		<dc:creator>Conor</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Languages]]></category>
		<category><![CDATA[comporession]]></category>
		<category><![CDATA[compress a java file]]></category>
		<category><![CDATA[guide]]></category>
		<category><![CDATA[hello world]]></category>
		<category><![CDATA[jar]]></category>
		<category><![CDATA[jar cmf]]></category>
		<category><![CDATA[manifest]]></category>
		<category><![CDATA[MF]]></category>
		<category><![CDATA[seal]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://blog.macaoidh.name/?p=198</guid>
		<description><![CDATA[I made the HelloWorld class a few days ago in this post. I am getting a bit ahead of myself here but what if you have an application with many files? Do you let the user handle all those files yourself? No! The Java Archiving Format .jar is the solution. With it you can create [...]]]></description>
			<content:encoded><![CDATA[<p>I made the HelloWorld class a few days ago in <a title="Hello World - Java" href="http://blog.macaoidh.name/2009/04/26/hello-world-java/">this post</a>. I am getting a bit ahead of myself here but what if you have an application with many files? Do you let the user handle all those files yourself? No! The Java Archiving Format .jar is the solution. With it you can create an entire application and bring it down to one file. Then you can run the whole application from that file! This can be done with the option of compression aswell.</p>
<p>Anyway even though it seems useless to create a jar archive of one file I am going to create one for the HelloWorld.class. First I need to create a file in which to store the manifest. This file will contain crucial information about the application and how it is run. I am going to do the bare minimum at the moment but there are endless possibilities with this file! This is what I put in HelloWorld.mf:</p>
<pre>Main-Class: HelloWorld
Name: class/
Sealed: true</pre>
<p>The main class tells the application which class to load first. Name and sealed tell the application which folders to seal. You might want to seal a package to ensure  version consistency among the classes in your software.</p>
<p>I now have two files; HelloWorld.class and HelloWorld.MF. Now I must compress them both into a jar archive and run the application. To create the jar archive execute:</p>
<pre>jar cmf HelloWorld.mf HelloWorld.jar HelloWorld.class</pre>
<p>Basically this says create a new jar archive including the manifest and the class file and locate it in a new file called HelloWorld.jar. Now the application is executable. To run it simply type:</p>
<pre>java -jar HelloWorld.jar</pre>
<p>You should see the same output from the original application:</p>
<pre>[conor@host java]$ java -jar HelloWorld.jar
Hello World!</pre>
<p>Simple as that!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.macaoidh.name/2009/04/27/creating-a-jar-archive/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Hello World &#8211; Java</title>
		<link>http://blog.macaoidh.name/2009/04/26/hello-world-java/</link>
		<comments>http://blog.macaoidh.name/2009/04/26/hello-world-java/#comments</comments>
		<pubDate>Sun, 26 Apr 2009 11:56:07 +0000</pubDate>
		<dc:creator>Conor</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Languages]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[command line]]></category>
		<category><![CDATA[compile]]></category>
		<category><![CDATA[first application]]></category>
		<category><![CDATA[hello world]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://blog.macaoidh.name/?p=195</guid>
		<description><![CDATA[I wrote my first Java application today. Ir is traditional to start out with something simple and boy this is simple and it works! First you need to download and install the Java Development Kit (JDK) from Sun here. Then change directory to your workspace &#8211; where ever you want! In my case it was: [...]]]></description>
			<content:encoded><![CDATA[<p>I wrote my first Java application today. Ir is traditional to start out with something simple and boy this is simple and it works!</p>
<p>First you need to download and install the Java Development Kit (JDK) from Sun <a href="https://cds.sun.com/is-bin/INTERSHOP.enfinity/WFS/CDS-CDS_Developer-Site/en_US/-/USD/ViewFilteredProducts-SingleVariationTypeFilter">here</a>. Then change directory to your workspace &#8211; where ever you want! In my case it was:</p>
<pre>cd /home/conor/java</pre>
<p>Then open up a new file named HelloWorld.java in your favourite text editor. In my case it was:</p>
<pre>vim HelloWorld.java</pre>
<p>Now we start the coding:</p>
<pre>class HelloWorld{
  public static void main(String[] args){
    System.out.println("Hello World!");
  }
}</pre>
<p>This is basically just a class that tells Java to output the words &#8216;Hello World! Now the code must be turned into machine code so that it is faster to run. To do that simply type:</p>
<pre>javac HelloWorld.java</pre>
<p>This creates a new file named HelloWorld.class which is written in machine code. If you open this file in your text editor all you will see is a load of gobledeegook. Now the application is ready to run. Type the following command to run it:</p>
<pre>java HelloWorld</pre>
<p>You should then see Hello World! written on your screen like this:</p>
<pre>[conor@host java]$ java HelloWorld
Hello World!</pre>
<p>Another one in the bag!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.macaoidh.name/2009/04/26/hello-world-java/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
