Wednesday, March 9, 2011

Compiling my assignment via the command prompt

I wrote my assignment in Eclipse. I'm really enjoying Eclipse, apart from it's unfortunate name-in-common with a certain teen-vamp flick (at least Eclipse IDE came up higher on Google search that Twilight Eclipse).

The only issue with writing my assignment in an IDE is that we're meant to start writing java in txt files only, and make sure they compile in the command prompt to ensure the IDE isn't assisting when things fall over. It also makes one pay more attention to all the curly brackets and semi-colons when the IDE isn't automatically highlighting mistakes...

Compiling and Running a Java Program from the Command Prompt:
  1. Check that JDK is installed.
    (Somehow I didn't have this installed, I thought that I did, given that Eclipse could compile my programs. Maybe I did have it installed and it was just in a non-default location)
  2. Install JDK
  3. Create a temporary folder C:\Uni (or whatever you want to call it). Copy and paste the .java files into this folder
  4. Run Command Prompt (Start > Run > Cmd) Type "cd \uni" to navigate to the directory
  5. Run javac Company.java and get a funky error: 'javac is not recognized as an internal or external command, blahdy blah blah (why can't the language settings in Windows override the spelling of error messages, so I see "recognised" rather than "recognized"?)
  6. To tell the system where to find javac and all the other wonderful JDK programs that we installed in step 2, run set path=%path%;C:\Program Files\Java\jdk1.6.0_24\bin\ or whatever the path is. Make sure you include the semi-colon between set path = %path% and the directory, otherwise it doesn't work. NB: There's nothing to indicate this was successful, just the system prompt being displayed again...
  7. Run javac Company.java again. Once again, there's nothing to display that this was successful, just the next system prompt.
  8. Run dir to see what's in the directory. Lo and behold, there's some new files - all my .java files now have a .class file
  9. Run the .class file with the main method by running main Company (no .class or .java required
This is what my command prompt session looked like:

C:\Documents and Settings\mel>cd \uni

C:\uni>javac Company.java
'javac' is not recognized as an internal or external command,
operable program or batch file.

C:\uni>set path=%path%;C:\Program Files\Java\jdk1.6.0_24\bin\

C:\uni>javac Company.java

C:\uni>dir
 Directory of C:\uni

09/03/2011  10:34 PM    <DIR>          .
09/03/2011  10:34 PM    <DIR>          ..
09/03/2011  10:56 PM             1,578 Company.class
09/03/2011  09:51 PM             2,017 Company.java
09/03/2011  10:34 PM             1,451 Employee.class
09/03/2011  09:57 PM             2,501 Employee.java
09/03/2011  10:34 PM             1,667 Team.class
09/03/2011  09:41 PM             2,868 Team.java
               6 File(s)         12,082 bytes
               2 Dir(s)   9,604,157,440 bytes free

C:\uni>java Company
[Team Name]: Team Awesome!!
[Team Leader]
ID: 1003
Name: Melissa Jones
Hourly Salary Rate: 90.0

[Programmer List]
ID: 1001
Name: Cameron Lawson
Hourly Salary Rate: 77.0

ID: 1002
Name: Carly Rodwell
Hourly Salary Rate: 45.0

ID: 1004
Name: Kimberley Phillips
Hourly Salary Rate: 44.0

ID: 1005
Name: Liam Garde
Hourly Salary Rate: 33.0

ID: 1006
Name: Dan McNaulty
Hourly Salary Rate: 32.0

ID: 1007
Name: Louise Latz
Hourly Salary Rate: 12.0

[Team Name]: Team of Champions
[Team Leader]
ID: 1008
Name: Tom Caley
Hourly Salary Rate: 54.0

[Programmer List]
ID: 1009
Name: Nick Bellas
Hourly Salary Rate: 63.0

ID: 1010
Name: Angela Turnour
Hourly Salary Rate: 50.0

ID: 1011
Name: Eric Lim
Hourly Salary Rate: 60.0

ID: 1012
Name: Sanath Kumar
Hourly Salary Rate: 42.0

ID: 1013
Name: Sandeep Badwal
Hourly Salary Rate: 80.0

ID: 1014
Name: Germaine Yong
Hourly Salary Rate: 25.0


C:\uni>

No comments:

Post a Comment