wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Problem compiling a package then accessing it Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem compiling a package then accessing it" Watch "Problem compiling a package then accessing it" New topic
Author

Problem compiling a package then accessing it

simon fletcher
Ranch Hand

Joined: Aug 04, 2012
Posts: 50
I am using the latest version of JDK. I use Windows Notebook to write source files. I compile them and run the class files in a DOS window. Everything works fine until I try and use my own packages. I'll explain what I did recently. There are four very basic files being used. It doesn't matter what they are but I'll quickly say what they are anyway. One creates rectangle objects (just points that designate corners). Another takes a number and randomly decides whether it will be negative or positive (for the x and y values). Another creates the points. The forth contains a main which instantiates four rectangle objects. There are no GUI's or graphics, or anything advanced. Very basic.

Step1: I created the first three source files in a folder called Ch5Q1, which is here:
C:\Documents and Settings\SKF\My Documents\Java\Ch5Q1
The three source files that would get packaged all had
package Ch5Q1;
as the first line of each file:

Step2: While making the current directory the same as the one the source files to be packaged are in, I created a package using the following in a DOS window:
javac -classpath "C:\Documents and Settings\SKF\My Documents\Java" *.java
The three source files appeared to compile successfully, producing three class files in the Ch5Q1 directory. This is the package, I'm assuming.

Step 3: In a directory called Ch5Q2 (located C:\Documents and Settings\SKF\My Documents\Java\Ch5Q2), I created a source file, called FourRecs.java, that uses the three classes of the Ch5Q1 package. This source file has the line
import Ch5Q1.*;
in it, on the first line.

After this, I don't know what to do to compile and then run FourRecs. Everything I have done so far has resulted in either the thing not compiling or not running if it does compile. All the classes are defined as public.

If I don't use packaging and importing and, with all four source files in the same directory, compile FourRecs.java using
javac FourRecs.java
all four source files compile and FourRecs runs perfectly. I've recomoiled and run it several times this way and it always works.

My questions are, upon completeing step three, what should make the current directory be, and what should I enter in the DOS window to compile the package? Then, what should the current directory be and what should I enter to compile FourRecs.java, the source file that uses the package? And finally what would I enter to run the FourRecs class file?

I have tried packaging several other basic programs I've made, all ones that work when not packaged, and had the same negative results. When I import java packages that come with the JDK, it works fine. If more information is needed, please let me know. Thanks.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38481
    
  23
Welcome to the Ranch

If you are only at the chapter 5 stage, you are probably better off not using package names in the first place.
You would have to tell us the package declarations you are using, but putting .class files inside a folder and then importing from a package of the same name doesn’t sound right. I would refer you to the Java Tutorials, and search for some old posts, eg this one. When you have your .class files with package names, they must be in a file structure corresponding to those package names. So you might be better // commenting out the package names, and those import statements.
Note some books add package names in the files you download, but that is because you might download several hundred files and get confused without package names.
Stevens Miller
Ranch Hand

Joined: Jul 26, 2012
Posts: 523
    
    3

This is precisely the kind of thing that had me gnashing my teeth when I first learned Java (if that's any comfort to you). Unlike most C environments, Java doesn't have a "linker" that binds up everything into one executable for you (the ".jar" archive is distantly similar, but not the same and, anyway, you aren't using that here).

At run time, the Java Runtime Environment will try to find any classes it needs as and when your program needs them. So, you need to specify the path(s) where those things can be found.

In your case, you've created classes in java\Ch5Q1, in a package called Ch5Q1 (and good for you for making your package and directory names match, as that's one thing I didn't know I had to do at first). To compile code that uses those classes, you need to tell the compiler where to find them. If your current directly is java\Ch5Q2, then you can compile with

This tells the compiler that the starting point for any imports will be the parent of your current directory. The compiler will look there for a directory matching the package name you imported from (that's why your package names and directories have to match).

While still in the same directory, you need to run the main() method in a class file you compiled in that directory (using the command above). But, you also need to tell the JRE where to find class files it might need during the run of your program. You might think that this would work:

But, unlike the javac command, the JRE won't look in your current directory (you're going to be surprised, at some point, to learn that the JRE pretty much has no idea what a "current directory" is). So, you need to add your current directory (or, wherever your startup class resides) to the list of directories in your classpath, like this:

That ought to run.

To be more explicit, here are (sort of) your steps.

Step One - Save this file as "Rec.java" into java\Ch5Q1:


Step Two - Compile it like this:


Step Three - Save this file as "FourRecs.java" into java\Ch5Q2:


Step Four - Compile it like this:



Step Five - While still in java\Ch5Q2, run your program like this:


The local policy in these forums is not to give the answer, but to guide the person to it. However, I found this remarkably hard to figure out and, when I did, I did it by finding a Web page somewhere that just spelled it out for me. It's one of those things you just have to know, because, well... you just have to, that's all.

Hope that helps.
simon fletcher
Ranch Hand

Joined: Aug 04, 2012
Posts: 50
Thanks for everyone's help. I've been trying to figure this out, on and off, for weeks and I eventually, very shortly afterposting this question, realized where my problem lay. When I was executing the FourRecs.class file, I wasn't properly using the classpath. I wasn't including the current directory of FourRecs. I was entering:

C:\Documents and Settings\SKF\My Documents\Java>java -classpath "C:\Do
cuments and Settings\SKF\My Documents\Java" FourRecs

instead of:

C:\Documents and Settings\SKF\My Documents\Java>java -classpath ".;C:\Do
cuments and Settings\SKF\My Documents\Java" FourRecs

One tiny little missed detail and I now have teeth that are ground down to nubs and no more hair to pull out.

Thanks again for you help.
simon fletcher
Ranch Hand

Joined: Aug 04, 2012
Posts: 50
As an extention to the idea of packaging, I read (and can't find where I read it) that it's possible for one to turn one's own packages into .jar files, place these into the ext directory of JDK, import them as normal, and then not have to use -classpath when executing. Has anybody used this and know how to do this?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38481
    
  23
You don’t need a linker if you have all the packages in the right folder structure, and go to the root of that directory structure. You need to specify a classpath if you are using external .jars, however.

I still think it is best to omit package names until you have more than about twelve classes in your application; you don’t need them if you are at the stage of such small apps.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38481
    
  23
Don’t put your work inside a JDK installation folder. At least that’s a rule of thumb. What will happen to your work when you uninstall Java and install a new version?

Another cheat: you can dispense with a classpath if you unzip dependency .jars into your own folder structure.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Problem compiling a package then accessing it