Results 1 to 6 of 6

Thread: Using packages

  1. #1
    prfalco is offline Member
    Join Date
    Jan 2008
    Posts
    7
    Rep Power
    0

    Default Using packages

    Hello there!
    I have someting that seams trivial, but it's a pain in the neck for me.
    I have several related classes that I want to organize in a package.
    So, in every class, in the top I write: package whatever;
    I have a folder named whatever where I put all my classes, then compile like this: javac -classpath c:\whatever c:\whatever\class1.java
    Then I setup my environment variable CLASSPATH and tested like this: C:\>set classpath and I get my C:\whatever, so seems to be ok.
    But when I try to 'use' one of the classes in my whatever package I get an error like "no such a package"
    Somebody help me please!

  2. #2
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    10

    Default Example

    Welcome to the forums prfalco

    I made an example for you. It is a package that you can run and use from text files and batch files. I just self studied this topic, so I can explain this in detail, if that is what you want. :D The example has been attached to this reply. It was created in Windows and it is a ".zip" file. Unzip it in the root of your C drive and run the batch file using "Command Prompt". Do not double click on it, because if there is a problem it will disappear before you can read it.

    I hope this helped. ;)
    Attached Files Attached Files
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  3. #3
    prfalco is offline Member
    Join Date
    Jan 2008
    Posts
    7
    Rep Power
    0

    Default Me again...

    Quote Originally Posted by tim View Post
    Welcome to the forums prfalco

    I made an example for you. It is a package that you can run and use from text files and batch files. I just self studied this topic, so I can explain this in detail, if that is what you want. :D The example has been attached to this reply. It was created in Windows and it is a ".zip" file. Unzip it in the root of your C drive and run the batch file using "Command Prompt". Do not double click on it, because if there is a problem it will disappear before you can read it.

    I hope this helped. ;)
    I have a class as the one that follows on a folder called person :

    package person;

    public class name{

    private String name_person= "pablo";

    public String get_name(){
    return name_person;
    }

    }

    I compile it using: javac name.java with success.

    Then I have this other class which is supposed to use the other one just importing it :

    import person.*;

    public class Letscheck {

    public static void main(String[] args) {
    name n = new name();
    System.out.println(n.get_name());
    }
    }

    I compile like this: javac c:\person\Letscheck.java and guess what? I get my dear error: package person does not exist.
    I set up my classpath environment variable in windows XP to point to .;c:\person.

    Gee.....

    Thanks for your help.

  4. #4
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    10

    Default 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. :D

    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:
    Java Code:
    import person.*;
    public class [COLOR="Teal"]Letscheck[/COLOR] {
    
    	public static void main(String[] args) {
    		name n = new name();
    		System.out.println(n.get_name());
    	}
    }
    This file, called Letscheck.java, is stored as:
    Java Code:
    [B]c:\java\[/B][COLOR="Teal"]Letscheck[/COLOR].java
    Next, you have your name class that is part of your person package:
    Java Code:
    package person;
    public class [COLOR="RoyalBlue"]name[/COLOR]{
    	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:
    Java Code:
    [B]C:\java\person\[/B][COLOR="RoyalBlue"]name[/COLOR].java
    Now open command prompt and type
    Java Code:
    cd c:\java\person\
    To use javac we do not need a CLASSPATH. Type the following:
    Java 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:
    Java Code:
    cd c:\java\
    javac *.java
    Now you should have the files:
    Java 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:
    Java Code:
    SET CLASSPATH=c:\java\
    Note that all my directories ends with a back slash. Now, to run your program type:
    Java Code:
    java Letscheck
    Note that I did not add the ".class" extension. The program should output:
    Java 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. :D
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  5. #5
    prfalco is offline Member
    Join Date
    Jan 2008
    Posts
    7
    Rep Power
    0

    Default Yes yes yes yes !!!!

    Quote Originally Posted by tim View Post
    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. :D

    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:
    Java Code:
    import person.*;
    public class [COLOR="Teal"]Letscheck[/COLOR] {
    
    	public static void main(String[] args) {
    		name n = new name();
    		System.out.println(n.get_name());
    	}
    }
    This file, called Letscheck.java, is stored as:
    Java Code:
    [B]c:\java\[/B][COLOR="Teal"]Letscheck[/COLOR].java
    Next, you have your name class that is part of your person package:
    Java Code:
    package person;
    public class [COLOR="RoyalBlue"]name[/COLOR]{
    	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:
    Java Code:
    [B]C:\java\person\[/B][COLOR="RoyalBlue"]name[/COLOR].java
    Now open command prompt and type
    Java Code:
    cd c:\java\person\
    To use javac we do not need a CLASSPATH. Type the following:
    Java 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:
    Java Code:
    cd c:\java\
    javac *.java
    Now you should have the files:
    Java 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:
    Java Code:
    SET CLASSPATH=c:\java\
    Note that all my directories ends with a back slash. Now, to run your program type:
    Java Code:
    java Letscheck
    Note that I did not add the ".class" extension. The program should output:
    Java 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. :D
    Finally !!! I've got it. I've printed it for future reference, and of course, study it. Many many thanks.

  6. #6
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    10

    Default Excellent

    I'm very glad that it worked. It took me some time and effort to create that post. ;)
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

Similar Threads

  1. Replies: 3
    Last Post: 02-12-2008, 12:03 PM
  2. Commonly used packages
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-17-2007, 11:05 AM
  3. Closing packages
    By uncopywritable in forum New To Java
    Replies: 0
    Last Post: 08-13-2007, 11:47 PM
  4. packages
    By ai_2007 in forum Advanced Java
    Replies: 1
    Last Post: 07-31-2007, 12:10 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •