Thread: Using packages
View Single Post
  #4 (permalink)  
Old 01-31-2008, 11:44 PM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
Command prompt
Hello prfalco

I know this can be frustrating, but please try to understand that if you can do this, then you can use Java fundamentally without an IDE to spoil you.

I am assuming that your Java compiler and interpreter is working. Lets make sure of the structure:
This is your Letscheck class with the main() method. Currently it is part of the default package:
Code:
import person.*; public class Letscheck { public static void main(String[] args) { name n = new name(); System.out.println(n.get_name()); } }
This file, called Letscheck.java, is stored as:
Code:
c:\java\Letscheck.java
Next, you have your name class that is part of your person package:
Code:
package person; public class name{ private String name_person= "pablo"; public String get_name(){ return name_person; } // Added a constructor for you public name(){} }
this file, called, name.java, is saved as:
Code:
C:\java\person\name.java
Now open command prompt and type
Code:
cd c:\java\person\
To use javac we do not need a CLASSPATH. Type the following:
Code:
javac *.java
This will compile all your java source code in the "c:\java\person\" directory. Now, we do the same for "c:\java\" to compile the file with your main() method:
Code:
cd c:\java\ javac *.java
Now you should have the files:
Code:
c:\java\person\name.class c:\java\Letscheck.cass
To run a Java program with the Java interpreter, it needs a CLASSPATH. To set this, use the command prompt before you use the "java" program. Type this into the command prompt:
Code:
SET CLASSPATH=c:\java\
Note that all my directories ends with a back slash. Now, to run your program type:
Code:
java Letscheck
Note that I did not add the ".class" extension. The program should output:
Code:
pablo
That's all that there is to it. You can do all this quickly by writing a batch file. (.bat)

I really hope this helped you.
__________________
If your ship has not come in yet then build a lighthouse.
Reply With Quote