File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes how to create a java package? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to create a java package?" Watch "how to create a java package?" New topic

how to create a java package?

Sebastian Constantin

Joined: Aug 20, 2008
Posts: 5
I am having problems with creating a java package.I don't understand what the CLASSPATH system variable is.In the Java tutorial section on Sun's website it says
To display the current CLASSPATH variable, use these commands in Windows and Unix (Bourne shell):

In Windows: C:\> set CLASSPATH

but when I do that on my Computer it says

Environment variable CLASSPATH not defined.

How do I define this CLASSPATH variable?

ps: I am using NetBeans to write my programs and I don't run them from the command line so I didn't need to set the CLASSPATH variable (until now).

Thank you for your help!
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

path is used for javac

classpath is for java

you can set path like(environmental variable in windows) below

right click on mycomputer->properties->advance->environmental variable->systemvariable->there you set path of your java installation path upto bin

for class path:

right click on mycomputer->properties->advance->environmental variable->uservariable->there you set path of your java class file to execute

Hope This Helps
Sebastian Constantin

Joined: Aug 20, 2008
Posts: 5
Let's say I want to make a package named "SoundPackage" and put it in the "Packages" folder which has the path "D:\Packages\".And then let's say I want to make a project which uses that package and the project has the path "D:\My Documents\Java Projects\Project". Which path do I have to specify in the "User variable" section of "Environment variables" , the one to the project or the one to the "Packages" folder?
Vijitha Kumara

Joined: Mar 24, 2008
Posts: 3902

If you are executing from Project folder then add the D:\Packages\ and the current directory (denoted by . (dot)) to the classpath. You have to use the fully qualified class names since your classes are in the packages.

hope this helps,

[How to ask questions] [Twitter]
Puneet Mittal
Ranch Hand

Joined: Dec 18, 2007
Posts: 45

hi first of all the CLASSPATH shows the path where your .class file is present. so you need to update your environment variable naming classpath so that when you run any tool it will first check for that environment variable while executing any .class file. and package is basically nothing but a folder names. "please tell me if i am wrong" so what you have to do is that use the following command:

package <folder name>;

here package is a keyword and the folder name means you have to create a folder and give its name here in place of folder name.
for some file suppose you want to create a package com.puneet so use this:-

so this will create your file inside package com.puneet.
and if you want some different file in upper package com but in different inner folder so then go for this:-

so now your file will be created in package com.sebastian

and one more thing while updating your classpath in environment variable, just give the value of variable name CLASSPATH and its value as the path where your .class file is present, but dont give the package name in the path. like

suppose your file is present in C:\JDK5.0\src\com\puneet;
so while you compile this file, compiler will automatically create your .class file by default in same folder in same package. so while updating your environment variable, just give classpath value as
C:\JDK5.0\src; instead of C:\JDK5.0\src\com\puneet; becoz if you do so your runtime will consider will give error as com\puneet is your package name, not a classpath name.
[ August 20, 2008: Message edited by: PUNEET MITTAL ]
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46375
Are you a real beginner? Just starting? Without complicated applications requiring access to third-party code? Are you using a recent version of Java?

If you can answer yes to those questions, there is a far easier way to handle the CLASSPATH:

Leave it alone and don't do anything with it.

Of course you still have to set up your PATH.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46375
To create a package: two ways to do it.

All your classes must have the package declared as the very first statement, before "import" statements.

1: Create a folder with the same name (all lower-case) as the package, and put your java files in that. Navigate there. Compile them by writing
Remember that there are classes (client classes) which have dependencies on other classes (supplier classes). You have to compile the "supplier" classes before their clients.

2: Don't create a folder; navigate to the folder one level up from your package, which contains the classes, and compile the classes like this
javac -d .
The same thing about supplier classes first applies.

To execute, navigate to the folder one level up (if you used method 2 you will already be there) and write java.mypackage.MyClassWithMainMethod command line arguments

As with most of these problems, there is something useful in the Java� Tutorials.
I agree. Here's the link:
subject: how to create a java package?
It's not a secret anymore!