Java Packages question

Posted on 2006-04-17
Last Modified: 2010-03-31

Again simple things send me nuts.

Here is the scenario..

I have two classes in the same package.
1. I need to make thoese two classes available to the outside the package so any other java application can instantiate it

2. I MUST allow any of these two classes to create an instance of the other.

This is an example os I have.....

package a.b.c.d;

class X
package a.b.c.d;

import a.b.c.d.*;

class Y
  public static void main(String [] args)
   X   xx = new X();   //I am getting an error saying that it cannot find the class.

I have set the CLASSPATH to a.b.c.d.

Whats the solution.
Question by:prain
    LVL 14

    Expert Comment

    The classpath should point to parent directory of a package a.b.c.d. In other words do this:

    say Your parent dir is c: then
    1) add to classpath c:
    2) put Your source to c:\a\b\c\d
    3) compile the source and make sure it stays in a directory c:\a\b\c\d
    4) now run Your code in c: like that:  java a.b.c.d.Y

    it should work

    Author Comment

    Ok. I was wrong in writing that classpath line. Well I have set the classpath correctly - to the parent.

    I  does not work.

    Where are the experts here?
    LVL 92

    Accepted Solution

    > import a.b.c.d.*;

    thats not needed as class is in same package

    > class X

    declare the class public

    public class X

    same for class Y
    LVL 30

    Expert Comment

    Have a look at this for a brush up on classpath:

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
    Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
    Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
    Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

    731 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now