Packages and access mode problem

N

Neroku

Hello, Consider I have two folders: Ap and Bp. Each folder is a package
and has a source file, A.java and B.java respectively.
Those files contain the code below:

----------A.java-----------------------------------
package Ap;
public class A
{
public static void foo()
{
System.out.println("From A.foo()");
}
}

----------B.java-------------------------------
package Bp;
class B
{
public static void main(String []args)
{
Ap.A.foo();
}
}

It compiles fine, but when I run the B main method I get the following
error:

IllegalAccessException: Class
koala.dynamicjava.interpreter.EvaluationVisitor can not access a member
of class Bp.B with modifiers "public static"
at sun.reflect.Reflection.ensureMemberAccess(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)

I do not acces any member of class Bp.B, Any ideas??

TIA
 
A

Alan Krueger

Neroku said:
It compiles fine, but when I run the B main method I get the following
error:

IllegalAccessException: Class
koala.dynamicjava.interpreter.EvaluationVisitor can not access a member =================
of class Bp.B with modifiers "public static"
at sun.reflect.Reflection.ensureMemberAccess(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)

I do not acces any member of class Bp.B, Any ideas??

The "B main method" is a public static member of class Bp.B, so yes,
you're trying to access a method on Bp.B.

Also, it looks like you're trying to run this through DynamicJava
instead of the normal JVM.
 
J

Jhair Tocancipa Triana

Neroku said:
Alan Krueger ha escrito:
So, I can't run the main method because the B class is not public, and
therefore the class that call the main method in B is unable to access
to it because both clases are not in the same package.
This explains the runtime error I get, right?

Of course not.

(debian-unstable)jtocancipa@golem:~/tmp$ javac Ap/A.java
(debian-unstable)jtocancipa@golem:~/tmp$ javac Bp/B.java
(debian-unstable)jtocancipa@golem:~/tmp$ java Bp/B
From A.foo()
 
N

Neroku

Alan Krueger ha escrito:
The "B main method" is a public static member of class Bp.B, so yes,
you're trying to access a method on Bp.B.

So, I can't run the main method because the B class is not public, and
therefore the class that call the main method in B is unable to access
to it because both clases are not in the same package.
This explains the runtime error I get, right?
 
E

Ehsan Khoddam mohammadi

HI
How do you invoke your class files with "java"
?
Neroku äæÔÊå ÇÓÊ:
 
N

Neroku

Ehsan Khoddam mohammadi ha escrito:
HI
How do you invoke your class files with "java"
?
Neroku äæÔÊå ÇÓÊ:
Hello, Consider I have two folders: Ap and Bp. Each folder is a package
and has a source file, A.java and B.java respectively.
Those files contain the code below:

----------A.java-----------------------------------
package Ap;
public class A
{
public static void foo()
{
System.out.println("From A.foo()");
}
}

----------B.java-------------------------------
package Bp;
class B
{
public static void main(String []args)
{
Ap.A.foo();
}
}

It compiles fine, but when I run the B main method I get the following
error:

IllegalAccessException: Class
koala.dynamicjava.interpreter.EvaluationVisitor can not access a member
of class Bp.B with modifiers "public static"
at sun.reflect.Reflection.ensureMemberAccess(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)

I do not acces any member of class Bp.B, Any ideas??

TIA

I click on the run botton in DrJava, but I do:
java B (current working directory is Bp)
I yields a different error.

I seems it only works fine if I do:
java Bp/B (current working directory is the parent of Bp)
 
D

Daniel Pitts

Neroku said:
Hello, Consider I have two folders: Ap and Bp. Each folder is a package
and has a source file, A.java and B.java respectively.
Those files contain the code below:

----------A.java-----------------------------------
package Ap;
public class A
{
public static void foo()
{
System.out.println("From A.foo()");
}
}

----------B.java-------------------------------
package Bp;
class B
{
public static void main(String []args)
{
Ap.A.foo();
}
}

It compiles fine, but when I run the B main method I get the following
error:

IllegalAccessException: Class
koala.dynamicjava.interpreter.EvaluationVisitor can not access a member
of class Bp.B with modifiers "public static"
at sun.reflect.Reflection.ensureMemberAccess(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)

I do not acces any member of class Bp.B, Any ideas??

TIA

This looks more like a problem with koala.dynamicjava, whatever that
is.

I'm guessing you can solve this issue by change
"class B" to "public class B"

HTH
- Daniel.
 

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

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top