Packages and access mode problem

Discussion in 'Java' started by Neroku, Dec 23, 2006.

  1. Neroku

    Neroku Guest

    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
    Neroku, Dec 23, 2006
    #1
    1. Advertising

  2. Neroku

    Alan Krueger Guest

    Neroku wrote:
    > 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.
    Alan Krueger, Dec 23, 2006
    #2
    1. Advertising

  3. Neroku <> writes:

    > Alan Krueger ha escrito:


    >> Neroku wrote:
    >> > 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.


    > 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()

    --
    -- Jhair
    Jhair Tocancipa Triana, Dec 23, 2006
    #3
  4. Neroku

    Neroku Guest

    Alan Krueger ha escrito:

    > Neroku wrote:
    > > 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.


    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?
    Neroku, Dec 23, 2006
    #4
  5. 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
    Ehsan Khoddam mohammadi, Dec 24, 2006
    #5
  6. Neroku

    Neroku Guest

    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)
    Neroku, Dec 24, 2006
    #6
  7. Neroku

    Daniel Pitts Guest

    Neroku wrote:
    > 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.
    Daniel Pitts, Dec 25, 2006
    #7
  8. Neroku

    Rajdeep Guest

    I think u should use import statement.
    import A;
    it will solve the problem.
    Neroku wrote:
    > 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
    Rajdeep, Dec 26, 2006
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John J Lee
    Replies:
    3
    Views:
    473
    bruno at modulix
    Dec 1, 2005
  2. Edward Loper
    Replies:
    0
    Views:
    460
    Edward Loper
    Aug 7, 2007
  3. John J Lee
    Replies:
    0
    Views:
    514
    John J Lee
    Aug 7, 2007
  4. Edward Loper

    mmm-mode, python-mode and doctest-mode?

    Edward Loper, Aug 9, 2007, in forum: Python
    Replies:
    0
    Views:
    428
    Edward Loper
    Aug 9, 2007
  5. Neal Becker
    Replies:
    0
    Views:
    378
    Neal Becker
    Aug 9, 2007
Loading...

Share This Page