Jump to content
  • Advertisement
Sign in to follow this  
jewishchainsaw

How do you use java packages?

This topic is 4856 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

Looked it up, the tutorials aren't doing me a lot of good. I already did something weird to my path file in my environmental varaibles under system in control manager, so i didn't have to manually set the directery everytime i wanted to compile something, and my classpath is set to some sort of photoshop program. I need to learn how to use packages because my program is getting messy, but every tutorial i read makes some sort of bizarre assumption that people understand how java interacts with windows' file manager when creating packages, and that you automatically know what the hell they are talking about when they decide to randomly spew out computer terminology i have never heard of like classpath and such. Come to think of it, that is every tutorial i've ever read. I'm a plug and play kind of person, i just need step by step instructions of how, not why, hopefully without messing what is already running. I could probably figure it out, but it would take an hour or two of boredom and frustration, whereas if i have someone to explain it to me, it will only take me a few minutes.

Share this post


Link to post
Share on other sites
Advertisement
If you're looking into how to create packages yourself, have you read This One?

Now, If what you meant was how to use packages (and I assume you're talking about Jar files since you talked about classpaths), lets say, you have an external library lib.jar, and you need it to compile/run your application, it suffices to create for example a bat file containing those lines:

Compile.bat:

javac -cp lib.jar;. MyOwnClass.java



Run.bat:

java -cp lib.jar;. MyOwnClass


That way you wont have to mess with the global environment variable Classpath or anything at all. Just run your compile.bat/run.bat files accordingly

Cheers

Share this post


Link to post
Share on other sites
there is a chance i COULD be using jar files, then again, i have no clue what that means. I just need a way of seperating my files so they arent all in the same folder. I'll try your link.

Share this post


Link to post
Share on other sites
Jar files are basically a .zip file with some info inside. Basically, you could put all of your classes in a .jar and then distribute your application, or separate several packages and such.
Read my given link, and this one and you might understand it all better.
Likely, youll want to use the package approach while you're coding, and then when you're ready to distribute, pack all your files in a Jar.
It might sound a little overwhelming at first, but believe, its not.

Cheers

Share this post


Link to post
Share on other sites
Ok, so I hacked up some of my old package code, to make a quick tutorial for you.
So, lets pretend you're making an application, it has a main class, "Main.java" from now on, and you have some classes. One of them is "Sound.java" which handles sound (duh!), and "Network.java" -you guess what it is for-. Also, you have a "ZipManager.java" that basically is an utilitary class to help compressing/decompressing zip files (yeah, Java does that, but lets pretend it doesnt).
As you can see, you could divide the structure of your application like this:

Main.java
Engine/
Sound.java
Network.java
Util/
ZipManager.java


This is the file/folder structure.
Right? Ok, now lets get coding a bit....

On top of Sound.java and Network.java, before the imports, you put this

package Engine;


So It might look a little like this

/**
*Class Sound
*Version 2.0
*@author Me
*/


package Engine;

import javax.whatever.*;

public class Sound{
...





And the same for Network.java
Now, as for ZipManager.java, I bet you know it, this is what has to be put on top:

package Util;


Right now, you want to call your newly created packages from Main.java, isnt it?
So, you might do something like this

/**
*Class Main
*Version 1.0
*@author Me
*/


import Util.ZipManager;
import Engine.Sound;
import Engine.Network;

import javax.whatever.*;

public class Main extends JFrame{
...





And thats all about there is to know about packages for a start, this way youll keep a tidy folder structure like you say you want to.

As an extra bonus, Ill tell you how to create Jar files. You might want to create an script (bat file for instance) to compile your app and make the Jars, to be ready for distribution.

Compile_and_pack.bat

javac Main.java
jar cf engine.jar Engine
jar cf util.jar Util


Do you see how this is all making sense now (I hope). This way, you have

Main.class
engine.jar
util.jar


And, in order to run the app:

java -cp engine.jar;util.jar;. Main


Where "-cp" is short for "-classpath=" (IIRC) and basically tells Java where to find the Classes your app might need (hence, the name "Classpath"). This is the short version. The long one, is when you use the global env var "CLASSPATH" (either %CLASSPATH% or $CLASSPATH in win/*nix), like you mentioned before, but the first one is recommended.
And in case you still dont understand very well this whole Jar thing, well, for instance engine.jar is a .zip containing Network.class and Sound.class, and is meant for distributing your app to the end-user without giving away folders. You could also put your Main.class in a jar (just follow the example), but thats up to you now.
Hope this was of any help, cheers and good luck

Share this post


Link to post
Share on other sites
Thank you satan, i am forever indebited to you. (Again. Mwu-ha-ha.) That clears up a lot, but i am still curious, how do you use files that are in other folders that aren't subdirectories? Take your example, except like this
project folder/

Main/
main.java
Engine/
Sound.java
Network.java
Util/
ZipManager.java

I know that seems like a braindead way of doing it, (I would use your method) but is there anyway to do this?

Share this post


Link to post
Share on other sites
Ok, the point is knowing how to use the classpath correctly.

Following your example, to compile main.java, you do this

javac -cp ../. main.java

Which basically tells the java compiler the folder "../." (aka "project folder/") is the place to look for "packages" like JAR files or simply directories.
Now, if you want you can also use absolute pathnames, like this

javac -cp "c:\My Projects\project folder" main.java

Which is basically the same thing, only that you're using an absolute path. Notice the "" are because the pathname contains spaces, otherwise you can skip those

Now, In order to run the app, the same thing goes:

java -cp ../.;. main

And you could also use the absolute pathname.

java -cp "c:\My Projects\project folder;." main


Notice I use an extra path in the -cp (;.) which basically is saying: The place where you, the java virtual machine are about to find the class files (*.class), is ../. and also . (the current directory - and the semi-colon ';' is meant to serve as a separator for dirs, thats all, and in Unix, its not ';' but ':'). If you dont add the ;. at the end, the VM wouldnt know where to find the main.class even if youre standing there with your command prompt (because you are OVERRIDING the classpath setting with -cp!). If you dont get this now, you will do with time, just remember that classpaths are a way to tell Java were to find class files, thats it. You dont have to put the ;. in the compile because youre telling the compiler what to compile (main.java) and this is explicit. But when you're running the program, its not explicit for the java VM on where is it going to find the class files required for execution (by default, its . - aka the current dir - and the ones pointed in the global enviroment variable %CLASSPATH%, but if you override those settings with the -cp option, then you have to specify the current folder too).
I hope its clear now.

Cheers dude

Share this post


Link to post
Share on other sites
i guess i do need to know how to import classes that are outside of a subdirectory. I tried to run a test program, but i couldn't figure out how to use javac -cp. Are you supposed to change something in your import statement?

I don't know how to create code boxes, so here are the file directories, my code, and my Dos instructions

C:\junkyardwarz\tests\ptest\Homeboy1\Homeboy.java
C:\junkyardwarz\tests\ptest\Homey1\Homey.java

Homeboy.java

package Homeboy1;

public class Homeboy
{
Homeboy()
{
}
}


Homey.java

package Homey1;

import Homeboy1.*;

class Homey
{
Homeboy homeboy;

Homey()
{
homeboy = new Homeboy();
}
}

Dos Intructions

C:\cd\junkyardwarz\tests\ptests

C:\javac -cp ../. Homeboy.java
error: cannot read: Homeboy.java

C:\javac -cp ../. Homey.java
error: cannot read: Homey.java

I tried a bunch of other stuff, but I thought this followed the format that DLS gave for main, i tried to follow your instructions as literally as possible. I feel kind of like a dunce, but i don't know how to make it work. I would have thought trying to compile main from the directory project folder in the example i provided earlier would require you to write out the directory main somewhere too. Anyways, i'm stuck, and i don't want to work on my program any more until i can clean up the mess of files i have. Anybody know what i did wrong?

Share this post


Link to post
Share on other sites
You're compiling from C:\ -
Quote:

C:\javac -cp ../. Homey.java


I think you need to be compiling from C:\junkyardwarz\tests\ptest\Homeboy1\ and C:\junkyardwarz\tests\ptest\Homey1

The reason is that ../. is a relative path. Relative to C:\ the path ../. is just C:\ so javac is looking in C:\ for your packages and not finding anything.

Relative to C:\junkyardwarz\tests\ptest\Homeboy1\ the path ../. is C:\junkyardwarz\tests\ptest\ so if you compile from within the directory C:\junkyardwarz\tests\ptest\Homeboy1\ javac will look for packages & classes in path C:\junkyardwarz\tests\ptest\ which is what you want it to do.

Sorry, kinda bad explenation, but its late and I have bad indigestion [sad]

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!