Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-29-2008, 08:09 PM
Member
 
Join Date: Jan 2008
Posts: 7
prfalco is on a distinguished road
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!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-29-2008, 11:14 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
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. 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
File Type: zip C Drive.zip (892 Bytes, 7 views)
__________________
If your ship has not come in yet then build a lighthouse.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-31-2008, 10:49 PM
Member
 
Join Date: Jan 2008
Posts: 7
prfalco is on a distinguished road
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. 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.
Bookmark Post in Technorati
Reply With Quote
  #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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 02-01-2008, 12:19 AM
Member
 
Join Date: Jan 2008
Posts: 7
prfalco is on a distinguished road
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.

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.
Finally !!! I've got it. I've printed it for future reference, and of course, study it. Many many thanks.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 02-01-2008, 12:38 AM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
Excellent
I'm very glad that it worked. It took me some time and effort to create that post.
__________________
If your ship has not come in yet then build a lighthouse.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Importing packages from the packages within same application. sta2003 New To Java 3 02-12-2008 01:03 PM
Commonly used packages Java Tip Java Tips 0 12-17-2007 12:05 PM
using Packages JavaForums Java Blogs 0 11-15-2007 07:50 PM
Closing packages uncopywritable New To Java 0 08-14-2007 01:47 AM
packages ai_2007 Advanced Java 1 07-31-2007 02:10 PM


All times are GMT +3. The time now is 01:14 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org