Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

creating/ using a package in java 1

Status
Not open for further replies.

Number6

Programmer
Jul 5, 2000
9
US
I am experimenting with packages in Java using the Certification book examples. I tried to expand to use a package. I have two classes, LovePotion and DummyClass. Without the Java package line of: package com.group1;, LovePotion executes properly and creates the DummyClass object. No matter what combination of package lines I try to implement, I get error messages, such as: Class com.group1.DummyClass not found. The source code for DummyClass.java compiled without problems, but LovePotion.java had the error. What am I doing wrong? Remember, that without the package line, the code works. Code is below:

package com.group1;

public class DummyClass
{
public DummyClass()
{
System.out.println("Enter DummyClass Constructor");

}// end of DummyClass constructor

public static void main(String args[])
{
System.out.println("Entered DummyClass");
}
}// end of DummyClass


package com.group1;

public class LovePotion
{
LovePotion()
{
System.out.println("Enter LovePotion constructor");

}// end of LovePotion method

public static void main(String args[])
{
LovePotion lp = new LovePotion();
System.out.println("Love Potion in com.group1");
DummyClass dc = new DummyClass();

}// end of main method

}// end of class



 
Have you checked your directories or are the two classes in the same folder? java will look for your classes in the source_code_folder\com\group1\ folder. Hope that helps

JavaDude32
 
I have tried this, but I am still unsuccessful. Here is the new directory structure:

c:\com\group1

c:\com\group2

The calling java class, LovePotion is in the com\gourp1 folder. The called java class, DummyClass, is in com\group2. No matter what combination of pakage statements I write, I still get a java:15 error. How should the package statement be written for source class com\group1\LovePotion.class to access class com\group2\dummyClass?
 
There are three scenarios:
1. If you have the two classes in the same directory and part of the same package then it should be fine. Both LovePotion and DummyClass should be in the directory c:\com\group1 and have the package statement package com.group1;.

2. If you have DummyClass in a different directory but the same package i.e com.group1;. then you must have the directory that the DummyClass class exists in on the classpath. In order to run LovePotion the JVM must be able to find the DummyClass class file, firstly it checks the current directory, if the DummyClass file is not there it checks the classpath to locate it.

3. DummyClass is in a different package and a different directory. Say DummyClass is in package com.group2; Then to use it in LovePotion you must import it like: import com.group2.DummyClass; before you can use it. It must also be on the classpath as explained in point 2 above.

 
I am still receiving errors. Both LovePotion and DummyClass are in c:\com\group1. I have included a classpath line in my autoexec.bat file: CLASSPATH=.;C:\JDK1.2.2BIN;C:\COM;C:\COM\GROUP1;C:\COM\GROUP2

If I comment out the package line in both source files, they compile okay and run from a DOS prompt. If I umcomment the package lines, DummyClass compiles okay, but LovePotion still gives the following error:

LovePotion.java:15: Class com.group1.DummyClass not found

I cannot see anything wrong with the classpath statement. My PC reboots okay, so I guess that the classpath is okay, yet the behavior suggests that the classpath statement is not coded correctly or is being ignored.
 
This should work:
1) put both java files in the same directory (wherever is convenient for you)
2)change both packages to com.group1
3)add "c:\classes" to your classpath
3)in the directory where the java files are, execute
"javac -d c:\classes *.java"
4)run the class by executing "java com.group1.LovePotion" in any directory
 
Does anyone know if these package related probems are just on Microsoft platforms? I had a lot of similar difficulties on Windows 98. I thought it was due to some kind of conflict between the underlying DOS PATH command and the
-classpath statement.
 
The "package problems" (which aren't really problems its just a matter of understanding how they work :) ) are common to the Java platform on all OS.

Basically the jist of it is to always ensure you have your classpath pointed to the root of your package tree. So if you have a java source file placed in

h:\java\src\com\acme\Demo.java

Where Demo.java looks like :

package com.acme;
public class Demo
{
public static void main(String[] args)
{
//Some code
}
}

... if you want your classes in h:\java\classes : compile as :
javac -classpath [yourclasspath] -d H:\java\classes H:\java\src\com\acme\Demo.java

(where yourclasspath is an optional classpath if you need other libaries to compile )

This will place class files in H:\Java\classes\com\acme.

To execute either put 'H:\Java\classes' in your classpath and : java com.acme.Demo

or just java -cp H:\Java\classes com.acme.Demo

Note : Its easier when getting the hang of it to be specific about things by using full path names. Once you have got it working you can use common sense to cd to correct dirs and work w/ relative path names.

Hope that helps a bit ...
RjB.
 
Oh boy. I am using Windows 98. I have tried all of these suggestions and I still cannot get the package "concept" to compile, so let me explain again what I have tried.

First, both DummyClass.java and LovePotion.java are in the c:\com\group1 directory. When each file has no package statement, they compile and execute without any problems. No errors whatsoever.

When I add a package statement( package com.group1; ) to both source files, DummyClass.java compiles, but LovePotion.java produces the following error: java:15: Class com\group1\DummyClass not found, but in windows explorer it is there.

When I add a classpath statement to the javac command, I still get the exact same error message. The command is: javac -classpath c:\com\group1 LovePotion.java

When I compile DummyClass to c:\classes, by using use the -d option, the DummyClass class file is written to c:\classes\com\group1. This is clearly shown in windows explorer. Then no matter how I attempt to compile LovePotion.java, I still get the same java error 15. Only the pathname changes, depending on what I code for the package statement.

I am including the two source files below:

package com.group1;

public class DummyClass
{
public DummyClass()
{
System.out.println("DummyClass constructor");
}

public static void main(String args[])
{
System.out.println("Entered DummyClass");
}


public class LovePotion
{
LovePotion()
{
System.out.println("LovePotion constructor");
}
public static void main(String args[])
{
LovePotion lp = new LovePotion();
System.out.println("Love Potion in com.group1");
DummyClass dc = new DummyClass()
}
}

I have change my autoexec.bat file to include c:\classes and
c:\classes\com\group1 at different time, but I still keep getting the same java:15 error message.


What in heaven's name is going on?

Thanks
 
Your classpath is
CLASSPATH=.;C:\JDK1.2.2BIN;C:\COM;C:\COM\GROUP1;C:\COM\GROUP2

since the package is com.group1 you need to have the
directory 1 above the com directory in the classpath.

?
|
|com|
|-group1
|
|-group2

You see com.group1 is the same as saying com/group1/. This will
work from 1 above com not from com.
Add "c:\" to the classpath.
CLASSPATH=.;%CLASSPATH%;c:
Also you don't need the
jdk\bin directory on your %CLASSPATH%, only on your %PATH%
 
Hi again

A few thoughts
[1] You need a ';' on 'DummyClass dc = new DummyClass()' in LovePotion.java
[2] If you want the LovePotion class in the com.group1 package you will need to either
[2.1] - create a new file LovePotion.java with the package name at the top of the file (didn't seem to be in the listing above :) - I think this is proabably the showstopper problem for you as its compiling into the default package instead).
[2.2] - Insert the LovePotion class in the DummyClass.java file - but dont make it public (can only have one public class per java source file)

Ok - here's the steps to make it work (well - work on my machine at least :) ) I've used approach (2.1) above.

[1] create directory
H:\Java\src\com\group1
and place LovePotion.java and DummyClass.java in here

[2] Compile 'em. Assuming you will want to output your classes in H:\Java\classes (which will put the actual class files under the package tree H:\Java\classes\com\group1\ )
[2.1] CD to H:\Java\src
[2.2] javac -d H:\java\classes -classpath H:\java\classes com\group1\DummyClass.java
javac -d H:\java\classes -classpath H:\java\classes com\group1\LovePotion.java


or just ...
javac -d H:\java\classes -classpath H:\java\classes com\group1\*.java

Note : you need the "-classpath H:\java\classes" in there as the LovePotion class needs to know where to find the compiled DummyClass !)

[3] Run 'em
java -cp H:\java\classes com.group1.LovePotion
LovePotion constructor
Love Potion in com.group1
DummyClass constructor


Viola !

Try again but fix the ';' error and most importantly put the package statement in LovePotion.java. Over to you
RjB.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top