Jump to content
  • Advertisement
Sign in to follow this  

[java] How to write Java import libraries

This topic is 5135 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How do you write Java import libraries? Like
import javax.swing.*;

How do you write a library like that? I want to write a class that people can import into their programs.

Share this post

Link to post
Share on other sites
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;
import your.package.name.goes.here.*;

Share this post

Link to post
Share on other sites
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
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.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!