Java compilation problem - Caused by package access

J

JSR

If class TestPackage and DefPack are in 2 separate files, how can we
access DefPack from TestPackage?

Note -
1. TestPackage is in package testapps and DefPack is in the default
package space. This is the problem.
2. If I were to comment out the package statement within
TestPackage.java, this example would compile.
3. I need to be able to compile with the pacakge specifications being
the way they are.

// Compilation unit - TestPackage.java

package testapps;

public class TestPackage {
public static void main(String[] args) {
new DefPack();
}
}


// Compilation unit - DefPack.java

public class DefPack {
public DefPack() {
System.out.println("DefPack created");
}
}
 
C

Christophe Vanfleteren

JSR said:
If class TestPackage and DefPack are in 2 separate files, how can we
access DefPack from TestPackage?

Note -
1. TestPackage is in package testapps and DefPack is in the default
package space. This is the problem.
2. If I were to comment out the package statement within
TestPackage.java, this example would compile.
3. I need to be able to compile with the pacakge specifications being
the way they are.

You really can't do that. You can not import classes from the default (== no
package statement given) package. So you'll never be able to reach DefPack
from TestPackage.

The only solution is to put DefPack in a proper package.
// Compilation unit - TestPackage.java

package testapps;

public class TestPackage {
public static void main(String[] args) {
new DefPack();
}
}


// Compilation unit - DefPack.java

public class DefPack {
public DefPack() {
System.out.println("DefPack created");
}
}
 
J

JSR

Christophe Vanfleteren said:
JSR said:
If class TestPackage and DefPack are in 2 separate files, how can we
access DefPack from TestPackage?

Note -
1. TestPackage is in package testapps and DefPack is in the default
package space. This is the problem.
2. If I were to comment out the package statement within
TestPackage.java, this example would compile.
3. I need to be able to compile with the pacakge specifications being
the way they are.

You really can't do that. You can not import classes from the default (== no
package statement given) package. So you'll never be able to reach DefPack
from TestPackage.

The only solution is to put DefPack in a proper package.
// Compilation unit - TestPackage.java

package testapps;

public class TestPackage {
public static void main(String[] args) {
new DefPack();
}
}


// Compilation unit - DefPack.java

public class DefPack {
public DefPack() {
System.out.println("DefPack created");
}
}


Thanks for the info. I unfortunately have to integrate my code with
some third party code written this way.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,470
Messages
2,571,807
Members
48,797
Latest member
PeterSimpson
Top