Why an java.lang.IllegalAccessError?
Hi, I have a number of classes in a custom package 'blackdragon53.dao'
One of these classes is Dao_Stairs, which has the following public static attributes;
publicstaticint LEVEL = 2;
publicstaticint SOLID = 1;
publicstaticint NORMAL = 0;
publicstaticint LEVELS = 30;
I have another class which makes use of one of these attributes, I import the package like so;
import blackdragon53.dao.*;
I am able to access the classes and even declare Dao_Stairs objects, but when I try to access Dao_Stairs.SOLID, I get an error.
int solidFlag = Dao_Stairs.SOLID;
I get an
Exception in thread "main" java.lang.IllegalAccessError: tried to access field Dao_Stairs.SOLID from class Dao_ToN
error. Any reason why it should throw this error?
[1277 byte] By [
Maykin53a] at [2007-11-26 19:34:59]

If you can instantiate Dao_Stairs, then you should be access it's static members.. I've really got no idea why it would chuck an IllegalAccessError... maybe something weird in the packaging... can you post your whole Dao_Stairs class... and maybe the "client" method in Dao_ToN?
just one thing... if LEVEL is a constant then make it final...
public static final int LEVEL = 2;
I don't think that's the problem but it is another potential gotcha.
Thanks for the reply corlettk.
Here is my Dao_Stairs class. As you can see I've declared the static integers as final like you suggested.
package blackdragon53.dao;
import java.io.*;
public class Dao_Stairs implements Serializable
{
public static final int LEVEL = 2;
public static final int SOLID = 1;
public static final int NORMAL = 0;
public static final int LEVELS = 30;
int[] type;
public Dao_Stairs()
{
type = new int[LEVELS];
refreshTypes();
}
public int getType(int level)
{
return type[level];
}
public void setType(int level, int newType)
{
type[level] = newType;
}
public void refreshTypes()
{
for(int i=0;i<LEVELS;i++)
{
type[i] = 0;
}
}
}
And here are the parts of Dao_ToN that are relevant.
import javax.swing.*;
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import blackdragon53.dao.*;
public class Dao_ToN
{
// Flags
public static final int BOARD = 0;
public static final int TITLE = 1;
public static final int MENU = 2;
public static final int MAP = 3;
public static final int SCENE = 4;
int solidFlag;
int stage = 0;
String errorMessage = "Unknown Error";
// Objects
Dao_Library library;
Dao_Board board;
Vector boardVector;
Vector boardCoordFlag;
JList boardList;
Component focusComponent;
// Frames
JFrame frame;
JDesktopPane contentPane;
JInternalFrame errorFrame;
JDesktopPane errorContentPane;
// Constructor
public Dao_ToN()
{
solidFlag = Dao_Stairs.SOLID;
JFrame.setDefaultLookAndFeelDecorated(true);
frame = new JFrame("Tears of Nighon");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane = new JDesktopPane();
contentPane.setBackground(Color.black);
contentPane.setOpaque(true);
contentPane.setDragMode(JDesktopPane.LIVE_DRAG_MODE);
frame.setContentPane(contentPane);
board = new Dao_Board();
boardCoordFlag = new Vector();
boardVector = new Vector();
boardList = new JList(boardVector);
library = new Dao_Library();
}
...
// Main
public static void main(String[] args)
{
Dao_ToN ton = new Dao_ToN();
ton.loadBoard("DaoTest1.dbd");
ton.renderMain();
}
...
The program compiles but during execution, throws an exception at the beginning of the constructor.
The weird part is that when I use 'blackdragon53.dao.Dao_Stairs' instead of 'Dao_Stairs', it works fine. But I'd prefer to have access to my static variables nonetheless.
Could it be the way the package is packaged? Since there is something wrong with this computer's ClassPath variable, I have just put my package in directory of this file + '/blackdragon53/dao/'.
Thanks for taking the time to help.>
Smells like you have two Dao_Stairs.class files; you compile with one and run with an older version. Search and delete all unnecessary class files and recompile everyhing from scratch.
A good idea, thanks for the suggestion.
There was, however, no Dao_Stairs class in the directory. Since Dao_Stairs is part of a package that I created with a different project earlier, it has already been compiled within the package, so when I compile the new project, Dao_Stairs doesn't need to be compiled.
I'm also having problems with putting packages in different directories and setting the classpath variable, so I think I may have to re-install Java - it could be a problem associated with the package.
Thankyou both for your help.
Any last ideas before I reinstall Java?
Reinstalling Java is almost certain to be a complete waste of time. That isn't going to improve your competence in managing the classpath at all.
I set the classpath exactly as it should be, I had an experienced developer check it, and noone on these boards was able to tell me what I did wrong.
It is not my competence in setting the classpath that is the problem.
Another strange outcome is that when I try to access the Dao_Stairs static variables from another project, from the same package as Dao_Stairs, it works fine.
However, if I add Dao_Stairs to the project I am working on, remove it from the previous package, and include it into the directory, it still doesn't work.
Any ideas?
Here is a list of what does and doesn't work;
import blackdragon53.dao*; // works
Dao_Stairs stairs = new Dao_Stairs(); // works
int solid = Dao_Stairs.SOLID; // throws illegal access error at runtime
int solid = blackdragon53.dao.Dao_Stairs.SOLID; // works
int type = Dao_Stairs.getType(1); // works
Then you've definitely got classpath and/or package and/or old version issues, regardless of what you or any other experienced developer says.
Because I have classpath issues, I put the package is in the same directory as my project. Should it not still work? The package contains all the class files from that package, including Dao_Stairs. There are no other Dao_Stairs.class files, I've done a search of my harddrive to make
> Because I have classpath issues, I put the package is
> in the same directory as my project.
I don't know what that means. Java knows nothing about "projects." Are you using an IDE, where "project" means something? Or are you using that term in some more general sense?
> Should it not
> still work?
I can't say without details of what's where, what you're doing, and what the classpath is. (And note that if you ARE using an IDE, the classpath environment variable is probably ignored. IDEs usually have their own place to set classpth for the project or for the IDE.)
> The package contains all the class files
> from that package, including Dao_Stairs. There are no
> other Dao_Stairs.class files, I've done a search of
> my harddrive to make sure.
Hmmm... Hard to say more wthout specifics.
> int type = Dao_Stairs.getType(1); // worksThis is either a typo or something else is going on. This can't compile with the code you showed, because getType is non-static.
> There are no> other Dao_Stairs.class files, I've done a search of> my harddrive to make sure.Is there a Dao_Stairs nested class?
Print out Dao_Stairs.class and blackdragon53.dao3.DaoStairs.class.
Sorry, by project I meant all of the class files for this specific program;
In a single directory I have;
- the class file that I am trying to execute (Dao_ToN.class)
- other classes, such as Dao_Library, Dao_Statistic, Dao_Description
- other files that I use for the project (serialized objects of Dao_Board and picture files)
That is what I meant by project.
In this same directory, I have the following sub-directory "/blackdragon53/dao/" that contains the class files from the package 'blackdragon53.dao'.
The classpath variable is currently "C:\Program Files\QuickTime\QTSystem\QTJava.zip;C:\Program Files\Java\jre1.5.0_11\lib\ext\";
Since I can already declare and initialise Dao_Stairs objects and make use of its public methods, shouldn't I be able to access Static attributes?
> int type = Dao_Stairs.getType(1); // worksSorry my mistake, that should read;int type = stairs.getType(1); // works
> > There are no> > other Dao_Stairs.class files, I've done a search> of> > my harddrive to make sure.> > Is there a Dao_Stairs nested class?No, there isn't.
> In this same directory, I have the following
> sub-directory "/blackdragon53/dao/" that contains the
> class files from the package 'blackdragon53.dao'.
Okay, then "this same directory"--the one that contains blackdragon53--should be on your classpath.
> The classpath variable is currently "C:\Program
> Files\QuickTime\QTSystem\QTJava.zip;C:\Program
> Files\Java\jre1.5.0_11\lib\ext\";
Get the Java ext directory off of there. You don't need it. I don't see anything there that will let you find Dao_Stairs or any of your other classes. How are you running this?
> Since I can already declare and initialise Dao_Stairs
> objects and make use of its public methods, shouldn't
> I be able to access Static attributes?
Yes. So something fishy is going on.
jverda at 2007-7-21 17:37:54 >

blackdragon53.dao.Dao_Stairs.class is the only Dao_Stairs.class that exists.
It is here;
package blackdragon53.dao;
import java.io.*;
public class Dao_Stairs implements Serializable
{
public static final int LEVEL = 2;
public static final int SOLID = 1;
public static final int NORMAL = 0;
public static final int LEVELS = 30;
int[] type;
public Dao_Stairs()
{
type = new int[LEVELS];
refreshTypes();
}
public int getType(int level)
{
return type[level];
}
public void setType(int level, int newType)
{
type[level] = newType;
}
public void refreshTypes()
{
for(int i=0;i<LEVELS;i++)
{
type[i] = 0;
}
}
}
>
Try running from the command line like so:
java -cp <whatever> -verbose:class WhatverYourClassIs
This will show you all the classes it loads and from where. You might have to add > someFile
if the output scrolls past your screen.
Paste the exact command here, and what it says about where it loaded Dao_Stairs from.
jverda at 2007-7-21 17:37:54 >

Have you recompiled Dao_Stairs recently? Did those member variables used to be non-public? Add a new one, just for testing purposes, recompile, and see if you can access that one, with and without the package name.
jverda at 2007-7-21 17:37:54 >

>Get the Java ext directory off of there. You don't need it. I don't see >anything there that will let you find Dao_Stairs or any of your other >classes. How are you running this?
It is on there because previously I tried to run my program with the blackdragon53.dao package inside that directory instead, but it wouldn't even compile, saying the package doesn't exist.
I added the directory of the package (which is the directory of Dao_ToN.class) to the classpath just now but there is no difference in execution.
At first I was using JCreator, now I'm trying to execute it from the command line - both return the same outcome.
Also recompile the other class, just for good measure, after recompiling Dao_Stairs.
jverda at 2007-7-21 17:37:54 >

>Have you recompiled Dao_Stairs recently? Did those member >variables used to be non-public? Add a new one, just for testing >purposes, recompile, and see if you can access that one, with and >without the package name. Good idea, I'll try that.
Okay, so say you've got this:
C:\dev\Dao_ToN.java
C:\dev\blackdragon53\dao\Dao_Stairs.java
There's no Dao anything .class anywhere on the hard drive.
In C:\dev, run
javac blackdragon53\dao\Dao_Stairs.java Dao_ToN.java
You should now have a .class file next to each .java file.
Now, still in C:\dev, run
java -cp . -verbose:class Dao_ToN
jverda at 2007-7-21 17:37:54 >

Ok, I ran the -cp command as follows;
java -cp . -verbose:class Dao_ToN
and like you said the output was too much. I'm not exactly sure what you mean by > File.
Anyway I did this command;
javac blackdragon53\dao\Dao_Stairs.java Dao_ToN.java
And received a 'javac is not recognised as an internal or external command, operation program or batch file' error.
Btw, I've been using the IDE JCreator to compile (but I have also been executing from the command line to make sure it is not because of JCreator).
> Ok, I ran the -cp command as follows;
>
> java -cp . -verbose:class Dao_ToN
>
> and like you said the output was too much. I'm not
> exactly sure what you mean by > File.
some_command > some_file
will redirect the output into the specified file, so you can peruse it with a text editor.
> javac blackdragon53\dao\Dao_Stairs.java Dao_ToN.java
>
> And received a 'javac is not recognised as an
> internal or external command, operation program or
> batch file' error.
You either didn't install the full JDK, or your path (NOT classpath) doesn't include its bin directory.
> Btw, I've been using the IDE JCreator to compile (but
> I have also been executing from the command line to
> make sure it is not because of JCreator).
You should be able to just recompile from JCreator rather than the command line if you don't want to mess around setting your path right now. Just make sure that there are no Dao_Stairs or Dao_ToN .class files before compiling, and that after compiling, they're in the same directories as their respective .java files.
jverda at 2007-7-21 17:37:54 >

Here's something interesting. When I did the -cp command, at the end of the output I got;NoClassFoundException: Dao_StairsThe other output seemed to be loading packages from"C:\Program Files\Java\jre1.5.0_11\lib"
Hm.. I do recall installing the full JDK.My PATH variable is;C:\Program Files\Java\jre1.5.0_11\binIs this correct?
I recompiled both after deleting their class files, but I still get the same outcome.Perhaps there's something wrong with my environment variables?
> Here's something interesting. When I did the -cp
> command, at the end of the output I got;
>
> NoClassFoundException: Dao_Stairs
Wait....
Did you add the dot after the -cp?
Were you in a directory that contained Dao_ToN.class and dragonwhatever\whatever\Dao_Stairs.class?
(Clearly the answer to at least one of the above must be "No.")
> The other output seemed to be loading packages from
>
> "C:\Program Files\Java\jre1.5.0_11\lib"
Ick. Bad. Don't put stuff into the Java directories.
jverda at 2007-7-21 17:37:59 >

> Hm.. I do recall installing the full JDK.
>
> My PATH variable is;
>
> C:\Program Files\Java\jre1.5.0_11\bin
>
> Is this correct?
That's just the JRE, which you need for running java programs, but which is not sufficient for compiling them. It would be something like C:\jdk1.5.0_06\bin or something. Look for a directory with jdk in the name, and there should be a bin underneath that that has javac.exe in it. That bin directory needs to be on your path. It will also contain java.exe, so you can get rid of the JRE bin.
jverda at 2007-7-21 17:37:59 >

> I recompiled both after deleting their class files,
> but I still get the same outcome.
>
> Perhaps there's something wrong with my environment
> variables?
Since you got the error with java -cp, but now you're able to run it, one of the following must be true:
* You fixed up the -cp version.
* You're running from the command line without -cp, and it's using the classpath env var.
* You're running from withing the IDE.
Let's go for getting it working with -cp from the command line. That's the canonical case. If we get that working, then the problem is with the setup or environment for the others.
jverda at 2007-7-21 17:37:59 >

Ok, I added the JKD bin directory to my Path variable. Now I can use the 'javac' command - thanks for the help.
I ran;
javac blackdragon53.dao.Dao_Stairs.java Dao_ToN.java
and received a 'cannot read file Dao_Stairs.java' error.
I ran;
javac Dao_ToN.java
and i received errors for declaring all of my objects, including those that are in the same directory as Dao_ToN.
> Did you add the dot after the -cp?
>
> Were you in a directory that contained Dao_ToN.class
> and dragonwhatever\whatever\Dao_Stairs.class?
>
> (Clearly the answer to at least one of the above must
> be "No.")
Yes I added the dot.
Yes I was in the directory of those.
> Ick. Bad. Don't put stuff into the Java directories.
I didn't, i was loading classes such as java.util.<whatever>...
> Ok, I added the JKD bin directory to my Path
> variable. Now I can use the 'javac' command - thanks
> for the help.
>
> I ran;
>
> javac blackdragon53.dao.Dao_Stairs.java
> Dao_ToN.java
javac blackdragon53\dao\Dao_Stairs.java
> Dao_ToN.java
Backslashes, not dots, to separate directories. You're just telling javac which files to compile--these aren't java package and class names you're passing here.
> I ran;
>
> javac Dao_ToN.java
>
> and i received errors for declaring all of my
> objects, including those that are in the same
> directory as Dao_ToN.
Not sure what you're saying here. Can you paste in the exact error messages?
jverda at 2007-7-21 17:37:59 >

> > Did you add the dot after the -cp?
> >
> > Were you in a directory that contained
> Dao_ToN.class
> > and dragonwhatever\whatever\Dao_Stairs.class?
> >
> > (Clearly the answer to at least one of the above
> must
> > be "No.")
>
> Yes I added the dot.
> Yes I was in the directory of those.
No. You can't have gotten that error in that case.
> > Ick. Bad. Don't put stuff into the Java
> directories.
>
> I didn't, i was loading classes such as
> java.util.<whatever>...
Not sure what you're saying here, but don't worry about it. As long as you're not putting your classes into Java's directories.
jverda at 2007-7-21 17:37:59 >

By the way, if it makes you feel any better, this stuff isn't nearly as complicated as it's seeming now. It can be confusing at first, and it's a major pain to try to explain--especially on a forum when I can't see firsthand what's going on and can't easily tell or show you what to do--but once you get the main points down and get a little used to it, you'll wonder why you ever had trouble with it. :-)
jverda at 2007-7-21 17:37:59 >

>Not sure what you're saying here. Can you paste in the exact error messages?
Such as;
Dao_ToN.java:30: cannot find symbol
symbol: class Dao_Library
location : class Dao_ToN
^ library = new Dao_Library();
^
This is repeated for classes "Dao_Description" and "Dao_DescribingString". These classes are in the same directory as Dao_ToN. I do not get these errors when I compile and execute using JCreator.
Thanks. I'm sure it's something small, and I'm sure it must be as confusing to you as it is to me. I really appreciate your patience and your help, most people would have given up trying to help by now.
>Not sure what you're saying here, but don't worry about it. As long as >you're not putting your classes into Java's directories.Dao_ToN imports java.util.* as well as other standard packages. It seems to load them from the directory I meantioned.
>No. You can't have gotten that error in that case.
I'm sorry if I'm doing something wrong, but I repeated the command and made sure it was correct.
The command worked, since I got a lot of lines such as [loaded] .......
but they were all for 'java.util.<something>'.. When it came time to load Dao_Stairs, it came across that error.
just a guess... Why there is no package declaration for Dao_ToN class ?
> >Not sure what you're saying here, but don't worry
> about it. As long as
> >you're not putting your classes into Java's
> directories.
>
> Dao_ToN imports java.util.* as well as other standard
> packages. It seems to load them from the directory I
> meantioned.
Ah, I see.
It gets them from rt.jar, which is in the lib directory, not ext. You DON'T need the stuff under the Java directory on your classpath though. It will be found relative to the VM or JAVA_HOME.
jverda at 2007-7-21 17:37:59 >

> >Not sure what you're saying here. Can you paste in
> the exact error messages?
>
> Such as;
>
> Dao_ToN.java:30: cannot find symbol
> symbol: class Dao_Library
> location : class Dao_ToN
>^ library = new Dao_Library();
>^
> ses "Dao_Description" and "Dao_DescribingString".
> These classes are in the same directory as Dao_ToN. I
> do not get these errors when I compile and execute
> using JCreator.
So, there are .class files for Description, DescribeString and Library in the same directory as ToN? And none of these have package statements in the .java files?
The cognitive disonance is like to melt my skull.
jverda at 2007-7-21 17:38:03 >

> just a guess... Why there is no package declaration> for Dao_ToN class ?Because it appears to not be in a package.
jverda at 2007-7-21 17:38:03 >

>So, there are .class files for Description, DescribeString and Library in
>the same directory as ToN? And none of these have package
>statements in the .java files?
>The cognitive disonance is like to melt my skull.
That's correct. My skull is already in the process of melting :P
Yes that's right, Dao_ToN, Dao_Description, Dao_DescribingString, and Dao_Library are all in the same directory and are not in a package.
There must be a reason why I can compile and execute using JCreator but am unable to compile using the command line (due to errors).
Perhaps a classpath issue? But then, my classpath is set as it should be (i'm almost certain) and then why can i execute from JCreator but still get an illegal access error?
It's all so strange. This computer is pretty old and hasn't been reformatted in a long time - it is my friend's computer that I am using until I can replace my broken one. In a few days I hope to try on my new computer to see if it works.
Okay, let's start fresh.
1) Pick a brand new directory. Say, C:\wtf
2) Make sure the following exist:
C:\wtf\Dao_ToN.java
C:\wtf\blackdragon53\dao\Dao_Stairs.java
3) Make sure that Dao_Stairs starts with package blackdragon53.dao;
4) Also make sure that the .java files for any other of your classes exist in the appropriate spots for their respective packages in that directory tree.
5) Make sure there are none of your .java or .class files anywhere in your Java install directory.
6) CD to C:\wtf and type javac -cp . blackdragon53\dao\Dao_Stairs.java Dao_ToN.java
and also include on that line all the other .java files that make up your program.
7) Make sure that for every .java file, there's a corresponding .class file in the same directory.
8) Run java -cp . WhateverYouMainClassIs_IncludingTheFullPackageNameIfThereIsOne
One other thing: Make sure that no class that's in a package tries to refer to any class that's not. It shouldn't even compile if they do.
jverda at 2007-7-21 17:38:03 >

> Okay, let's start fresh.0) Forget all the other clases except Dao_ToN and Dao_Stairs. Strip down ToN to just a simple main that references the constants in Stairs. Don't put the other files in the wtf directory. Let's keep this as minimal and simple as possible.
jverda at 2007-7-21 17:38:03 >

I'm done for the night. Good luck with it. I'll check back in tomorrow if I have a chance to see where you are.
jverda at 2007-7-21 17:38:03 >

jverd, thankyou for of your help. Fortunately, I came up with an idea as to what the problem could be. Underscores in my class names (Dao_...).
I've never seen or heard any meantion of underscores being a problem, and though I posted my classes and their names on these forums noone has told me underscores can be troublesome.
The thing is, when I created my first project 'Dao_TileEdtor' (for which all its files became the blackdragon53.dao package), it worked perfectly.
However, I started running into problem when I wanted to use this package in my new project 'Dao_ToN'. I could access the classes and their methods, but when I tried to access their Static Attributes, I came across this problem.
So to make sure it wasn't because of 'Dao_Stairs', I created a 'Test' class and i COULD access its static members. How strange.
Then I decided to go back and remove Test from the package - when I recompiled Dao_TileEditor project, something strange happened - it created 3 files DaoTileEditor, DaoBoard, and DaoTile - 3 BLANK files representing those that were already in the project/package but without underscores. It tried to execute DaoTileEditor.
This could have been a bug in the IDE, so I renamed ALL of my classes so that none of them had underscores (and replaced references).
It worked.
Everything including DaoToN worked.
So Either;
- Java, my IDE, or this OS currently has problems with underscores in package class names when accessing static members.
or
- There was a Dao_Stairs class somewhere, hidden, possibly in a temp file or an IDE reference file, and renaming the classes cleared the reference to Dao_Stairs.
I'm more included to think the latter.
I didn't follow all the details, but I'm glad you got it working.
I was actually going to suggest removing the underscores, but I thought, "Nah, it couldn't be that." I was too lazy to check for sure, but I believe Java allows underscores.
It might be your IDE has a problem with them. I'm inclined to agree with your assessmen that there was another Dao_Stairs lurking somewhre, though.
jverda at 2007-7-21 17:38:03 >
