import javax.swing.*;
[java] How to write Java import libraries
How do you write Java import libraries? Like
How do you write a library like that? I want to write a class that people can import into their programs.
To make your source file part of a package add this to the top of the source file (above your imports):
package your.package.name.goes.here;
Pretty similar to namespaces in C/C++ and C#, but instead of a user-defined scope, the scope just covers the whole source file.
Then you can import like so:
import your.package.name.goes.here.YourClass;
//or
import your.package.name.goes.here.*;
package your.package.name.goes.here;
Pretty similar to namespaces in C/C++ and C#, but instead of a user-defined scope, the scope just covers the whole source file.
Then you can import like so:
import your.package.name.goes.here.YourClass;
//or
import your.package.name.goes.here.*;
Just like TheBluMage said, but I'll explain a few things in detail.
import <declaration>
The declaration is really just the directory to your classes from your classpath.
So, lets say you set your classpath to c:\java
you put some classes in
c:\java\net\mygames\server
you would then import them like:
import net.mygames.server.*;
so now you can use that class file:
ServerWidget widget = new ServerWidget();
if you didn't declare it you could do:
net.mygames.server.ServerWidget widget = new net.mygames.server.serverWidget ServerWidget();
which would have done the same thing. The compiler would look in your classpath.
If you using an IDE such as eclipse, you can set the classpath at the project level. If you are going old school with the dos editor and using javac/java to do everything... then you can type : set CLASSPATH = c:\java
that would set the classpath to c:\java. or you can put it to whatever you want. You can also have multiple classpaths. Seperate them by a ; The compiler will check each path for your classes using the path from the import.
Like TheBlueMage said, if you want to be able to import them, you need to put the package designator as the first code in your source file.
import <declaration>
The declaration is really just the directory to your classes from your classpath.
So, lets say you set your classpath to c:\java
you put some classes in
c:\java\net\mygames\server
you would then import them like:
import net.mygames.server.*;
so now you can use that class file:
ServerWidget widget = new ServerWidget();
if you didn't declare it you could do:
net.mygames.server.ServerWidget widget = new net.mygames.server.serverWidget ServerWidget();
which would have done the same thing. The compiler would look in your classpath.
If you using an IDE such as eclipse, you can set the classpath at the project level. If you are going old school with the dos editor and using javac/java to do everything... then you can type : set CLASSPATH = c:\java
that would set the classpath to c:\java. or you can put it to whatever you want. You can also have multiple classpaths. Seperate them by a ; The compiler will check each path for your classes using the path from the import.
Like TheBlueMage said, if you want to be able to import them, you need to put the package designator as the first code in your source file.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement