Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

packages are real horrors  RSS feed

 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Working on packages.Having multiple doubts.Do one can share a link of a page from where I can understand it easily
 
Paweł Baczyński
Bartender
Posts: 2074
44
Firefox Browser IntelliJ IDE Java Linux Spring
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, packages are not horrors at all. You must understand that the package structure and the directory structure need to correspond to each other. Also that dependencies in different packages must be compiled before client classes.

Once you have several packages needing compiling simultaneously, you are probably better off using an IDE or Ant/Maven. And it is unusual for me to advocate IDEs on this forum!
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here I am with one of my doubt many to follow soon...
I am creating a package p1 in directory as c:\f1


I created another package p2 in directory as d:\f2

 
Tapas Chand
Ranch Hand
Posts: 614
9
BSD Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. The package name should be same as the directory name.
If the path of java file is C:\f1\p1\Temp1.java then it is fine.
If the path of java file is C:\f1\Temp1.java then it is wrong.

2. You are creating an instance of Temp. But you have not shown what is Temp.java
Does it exist?
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So sorry it was the instance of Temp1 and Temp2 class respectively but this site is not allowing me to edit it as m using it on my mobile
Now the doubt is actually that when I compiled Temp2.java
I expected it to implicitly compile my Temp1.java even I had done the task of setting classpath of package p1
Now I moved Temp1.java in package p1
Then again I compiled Temp2.java it got compiled and makes a package in d:\f2 with Temp1.class
How this happened?
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your paths are all wrong; unless you specify the location of the two packages in your classpath youi cannot find them. You might not find them even if you do add them to the classpath. I suggest you create yourself a java directory somewhere. I often give this advice: look here and here.
Note the instructions are similar on Windows® and Linux; I think on Windows® you can write md as an abbreviation for mkdir. Obviously you can choose your own names for the folders. You want to create the p1 and p2 directories inside the java folder and you can compile from there. At a beginning stage put the XXX.java files in those folders and you can have the XXX.java files and the XXX.class files in the same places. Later on you may wish to put the source and class files in different locations.
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not getting how I won't be able to access the package even after setting its address as classpath
And even my doubt remains why when I compiled my Temp2.Java it don't implicitly compile Temp1.java
And how it suddenly implicitly compiles Temp1.java and creates its package with Temp1.class parallel to p2 package which contains Temp2.class
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As soon as I put Temp1.java in package p1 which is at c:\f1
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are compiling from the command line you usually have to list the locations of all the .java files
javac p1/*.java p2/*.java
Or you can compile one class at a time. Or you can compile all the .java files in a particular package one package at a time. You can navigate to the package
cd p1
javac *.java
cd ../p2
javac *.java

Remember to compile dependencies first.
 
Knute Snortum
Sheriff
Posts: 4270
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sachin Tripathi wrote:I am not getting how I won't be able to access the package even after setting its address as classpath


(This is a Windows example)

Say I am in a project folder c:\java\Play and I have a package net.snortum; in file Main.java. Then when I try to execute Main (java net.snortum.Main), it will look in c:\java\Play\net\snortum for Main.class. If you have it c:\java\Play\somewhere\else, java will never know where to look for it.

And even my doubt remains why when I compiled my Temp2.Java it don't implicitly compile Temp1.java
And how it suddenly implicitly compiles Temp1.java and creates its package with Temp1.class parallel to p2 package which contains Temp2.class


Java doesn't implicitly compile anything.
 
Tim Holloway
Saloon Keeper
Posts: 18789
74
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want "implicit compiles" use a tool such as Ant or Maven. They were developed specifically to handle problems like that. Before I had Ant, I used Windows batch files and they were no fun.

IDEs can compile "implicitly" as well, if you cannot compile a project without an IDE, you won't get hired by me. I consider IDEs to be valuable, but they should never be essential.

In Ant, you can tell the compile task a directory tree and it will compile every Java file in that tree, You can also define a separate directory tree to hold the results (classes) from the compilation.

Maven goes even further. Its "compile" goal will automatically compile everything in the project's src/main/java directory (or if you like src\main\java) and output the classes to target/classes.
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I am creating a package p1 in directory as c:\f1


I created another package p2 in directory as d:\f2
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I run Temp2.class
It says:Could not find or load main class p2.Temp2
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry my fault...haven't set the path for p2
 
Tapas Chand
Ranch Hand
Posts: 614
9
BSD Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I dont understand why you are creating 2 different directories like C:\f1 and D:\f2.
Forget about f1 and f2.
Create a single directory say, D:\java
create p1 and p2 packages like D:\java\p1\ and D:\java\p2\
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was just trying to see all the aspects of package
Another doubt
When we try to access class of particular package from a class of default package
It gives error at compilation time:
Cannot access Temp1
bad source file:.\Temp1.java
File does not contain class Temp1
Please remove or make sure it appears in the correct subdirectory of the sourcepath
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Temp1 is the class in package
 
Tapas Chand
Ranch Hand
Posts: 614
9
BSD Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is why default packages are discouraged.
But I think it is the other way round.
You can access packaged class from default package, but you cannot access default package class from packaged class.

I dont know how you are getting these compilation errors.
Can you post your code and commands for compilation.
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone
@campbell :finally felling little ease in package thanks
@knute :thanks though haven't understood your example properly
@tim:thanks man
@tapas:thanks
Now talking about implicit compiling
Creating a Base class with show method in it in Base.java
creating a child class and declaring a reference var of Base type and accessing show method of Base class by that reference var in Child.java

When I compile Child.java
It creates Base.class
And Child.class both
That what was implicit compiling for me.not very sure what to call it then
 
Knute Snortum
Sheriff
Posts: 4270
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sachin Tripathi wrote:
@knute :thanks though haven't understood your example properly

You're welcome. Is there anything I can clarify?

Now talking about implicit compiling
Creating a Base class with show method in it in Base.java
creating a child class and declaring a reference var of Base type and accessing show method of Base class by that reference var in Child.java

When I compile Child.java
It creates Base.class
And Child.class both
That what was implicit compiling for me.not very sure what to call it then

You're right, it compiles Base.java too. I never knew this. Thanks for pointing it out.
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah knute you can
Actually I m stillhnot much familiar with packages so if you can explain stuffs by taking simple package name as p1 and p2 like I did in my example
I mean actually want you to simplify things by talking in the way I can understand
 
Knute Snortum
Sheriff
Posts: 4270
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sachin Tripathi wrote:
Actually I m stillhnot much familiar with packages so if you can explain stuffs by taking simple package name as p1 and p2 like I did in my example
I mean actually want you to simplify things by talking in the way I can understand

I'll try. Let's take the program you posted before:


In order to execute this code, you have use the package name "p1" with the class name "Temp1" line this "p1.Temp1". For example, you might issue the command java p1.Temp1. This means look for a class called "Temp1" in the directory "p1" (I am ignoring the classpath for now).

But if you compile Temp1 in the directory "f1", Java will never find it. How does it know to search in "f1"?
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much knute
Now I understood what you really meant by your previous post
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Knute said, the Temp1 files must be in the p1 directory. You haveAnd when you add the Temp2 classYuo need to be inside the f1 directory where it says ** and you call the classes p1.Temp1 and p2.Temp2

The .java files may be in other directories and the .class files compiled into the current locations. The is how IDEs like Eclipse usually store the code.
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But if I am using package making command then why Temp1.class get stored in default package in directory f1
And even through I'll be in same directory while compiling it and from same directory I can run the class by using java Temp1
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh sorry Campbell I haven't seen your post
Now I understood everything
After compiling Temp2.java why Temp1.java don't get implicitly compiled
(Now please don't say java don't support implicit compiling )
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because Java doesn't support implicit compiling.

I don't know why, but you cannot compile files in other packages without a specific instruction. Possibly because the packages may contain only the .class files, particularly if they wree provided as .jars not containing any .java files. If there are no .java files, how are you going to compile them? If you are importing any classes, the javac tool appears to be programmed to assume they have already been compiled to .class files.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sachin Tripathi wrote: . . .
Now I understood everything
. . .
Well done
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell and knute you know what ,in my college the prof we got ,they are the real pain you can't discuss anything with them you just have to listen what they learnt in their college
But with you guys around its just awesome .

This forum is best for java Learner's and you guys make it bestest
 
Tim Holloway
Saloon Keeper
Posts: 18789
74
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
I don't know why, but you cannot compile files in other packages without a specific instruction.


Because the javac command (at least as Oracle implements it) accepts a list of source files (java files, not packages) and a destination directory.

What makes things confusing is that people are used to "globbing". Globbing is a limited wild-card expansion feature. As in "erase *.java". It may or may not be present in a give OS/command-shell environment, and when it is, it usually does not cover descending into sub-directories.

But well-constructed Java apps are nothing but subdirectories (packages). That's why tools like Ant support a "super-glob" syntax that also descends directories. But command shells - and even the venerable Unix "make" utility typically do not. You have to do it all the hard way. In most cases, the wildcards are applied at the shell level, so the Java compiler doesn't see "*.java", it sees what the shell expanded from "*.java".
 
Tim Holloway
Saloon Keeper
Posts: 18789
74
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sachin Tripathi wrote:
This forum is best for java Learner's and you guys make it bestest


Mission Accomplished, then. That's what we're here for!

And when you need to know not just "how", but "why"!
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
c:\java\Temp1.java
c:\java\Temp2.java

This is the directory structure of both(.java) whose code I had posted before
Now when I compile Temp2.java it gives compilation errors
Package p1 does not exist
Then I moved Temp1.java in package p1 by making a directory p1
So now my java files are at
c:\java\p1\Temp1.java
c:\java\Temp2.java

Now when I compile Temp2.java by javac -d . Temp2.java
It got compiled by creating a package p2 with Temp2.class and implicitlyimplicitly compiles Temp1.java as Temp1.class in p1
So now my .class are at c:\java\p1\Temp1.class
c:\java\p2\Temp2.class
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Success
 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sachin Tripathi wrote:
So now my java files are at
c:\java\p1\Temp1.java
c:\java\Temp2.java
Now when I compile Temp2.java by [size=24] javac -d . Temp2.java
It got compiled by creating a package p2 with Temp2.class and implicitlyimplicitly compiles Temp1.java as Temp1.class in p1
So now my .class are at c:\java\p1\Temp1.class
c:\java\p2\Temp2.class


It might work, but you really need to get in the habit of placing your source (.java) files in the proper directories, matching your declared packages.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It will only work if you have few classes. The -d option tells the javac tool to move the output files into directories and it will create the directories if necessary. The . option tells the javac tool to look for source files in the location . which means the current directory. The problem with that approach, rather than
javac -d ../src/ p1/Test1.java
is that you may have two classes with the same name. The idea of packages is that they permit you to disambiguate classes with similar names. Once you have enough classes that you have two with the same name, you will no longer be able to compile them. For the time being, maybe you should put the .java files in the same folders as the package structure, rather than having separate src and bin folders.
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So are you telling me to place source code and its .class in a same package
Kindly please elaborate how to organize source code and its .class
Efficiently by using SRC and bin
I have understood almost all the basics of package now want to use it efficiently.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I wrote yesterday:-With separate src and bin folders:
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!