Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

More package questions...

 
Joel Cochran
Ranch Hand
Posts: 301
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm still struggling with packages, so I thought I'd come to the friendly Ranch...
Let me start by saying that I do understand the basics. I know I need a package statement, I know the packages basically refer to a directory strucutre and have all kinds of benefits.
Let's say I have a package "com.myDomain.myPackage.subPackage"... I assume that I have to emulate this directory structure somewhere on my PC?
Let's say on my PC I have this in "C:\JavaSource\Development", do I need to compile these in the "C:\JavaSource\Development\com\myDomain\myPackage\mySubPackage" diretory?
And what happens when I implement this to another machine and the parent directory isn't the same... nothing? Am I correct in thinking I can simply copy the "com" directory (or put it in a .jar) in order to implement?
I know this shouldn't be so hard but this is really fuzzy for me right now...
TIA,
Joel
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joel, I think you got it quite right!
 
Mohan Panigrahi
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Joel,
That is correct. To add some more :

Let's say on my PC I have this in "C:\JavaSource\Development", do I need to compile these in the "C:\JavaSource\Development\com\myDomain\myPackage\mySubPackage" diretory?
And what happens when I implement this to another machine and the parent directory isn't the same... nothing?

Yes. This is because the class file will not contain information about the directory structure used to compile it.
Also, as you might already know, it is not necessary to go to that particular directory where java file is lying to compile it. As for example if your file name was Ex4.java, the following statement would compile as well
javac com\myDomain\myPackage\subPackage\Ex4.java
 
Joel Cochran
Ranch Hand
Posts: 301
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replys. I just needed someone to confirm that what I thought was correct. This should be a simple concept but I had struggled with it so much off an on that I just didn't trust myself.
Thanks again,
 
Shivade Sanjay
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Joel
Let me add something to your previous dicusssions.
You do not have to create directory structure manually if you are not using some kind of tool. The javac compiler has a option -d dir which after compilation of the class puts classes in respective folder as per specified in package in .java file.
eg. My java file (dt.java) has this code
package MyJava.dt;
import java.text.*;
import java.util.*;
import java.sql.*;

public class dt
{
.
.
.
while compiling I will say
javac dt.java -d C:\Test
so it creates subdirectories MyJava\dt under C:\Test folder.
It's so easy..
You might be knowing this .. but thought just let you know ...
Thanks
Sanjay
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic