Posted by Frank Hale on 2000-06-25
Okay here is my problem. Imagine a program that is similar to a web browser. Instead of this program loading webpages it loads class files and shows them. Now imagine this class file changing while its still loaded into the browser, so it needs to be reloaded. But the problem is that the class which is already loaded gets instantiated again and the new class never gets loaded. Here is some very basic code to describe my problem.
If you wanna help me out, run this program and then press the load class button. Now open the source code of the MyPlugin.java and change the System.out.println() line to display something else and recompile that file. Now go back to the program that should still be running and hit load class again. What happens is that the new class isn't loaded, you get the cached memory copy of the old one. Can somebody help me figure out how to load the new class in from disk please. Thank you!
-- CODE --
/* MyWindow.java - main program */
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MyWindow extends JFrame
{
public MyWindow()
{
super("Hello");
Container container = this.getContentPane();
JButton load = new JButton("Load Class");
load.addActionListener(new MyActionListener());
container.add(load, BorderLayout.CENTER);
addWindowListener(new WindowEventHandler());
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
setSize(100,100);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
int w = getSize().width;
int h = getSize().height;
int x = (dim.width-w)/2;
int y = (dim.height-h)/2;
setBounds(x, y, w, h);
show();
}
class MyActionListener implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
String myPlug = "MyPlugin";
try {
FileClassLoader loader = new FileClassLoader("");
Class c = loader.loadClass (myPlug);
Object plug = c.newInstance();
((Plugin) plug).start();
loader=null;
plug=null;
c=null;
} catch (Exception e) {
System.out.println("Caught exception : "+e);
}
}
}
class WindowEventHandler extends WindowAdapter {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
}
public static void main(String[] args)
{
new MyWindow();
}
}
-- CODE --
/* Plugin.java - Interface plugins implement */
public interface Plugin
{
public void start();
}
-- CODE --
/* MyPlugin.java - The one and only plugin */
public class MyPlugin implements Plugin
{
public void start()
{
System.out.println("My Version is 1");
}
}
-- CODE --
/* FileClassLoader.java - I didn't write this */
import java.io.*;
import java.net.*;
import java.util.*;
public class FileClassLoader extends ClassLoader {
private String root;
public FileClassLoader (String rootDir) {
if (rootDir == null)
throw new IllegalArgumentException ("Null root directory");
root = rootDir;
}
protected Class loadClass (String name, boolean resolve)
throws ClassNotFoundException {
// Since all support classes of loaded class use same class loader
// must check subclass cache of classes for things like Object
Class c = findLoadedClass (name);
if (c == null) {
try {
c = findSystemClass (name);
System.out.println("Returning System Class");
} catch (Exception e) {
}
}
if (c == null) {
// Convert class name argument to filename
// Convert package names into subdirectories
String filename = name.replace ('.', File.separatorChar) + ".class";
try {
byte data[] = loadClassData(filename);
c = defineClass (name, data, 0, data.length);
if (c == null)
throw new ClassNotFoundException (name);
} catch (IOException e) {
throw new ClassNotFoundException ("Error reading file: " + filename);
}
}
if (resolve)
resolveClass (c);
return c;
}
private byte[] loadClassData (String filename)
throws IOException {
// Create a file object relative to directory provided
File f = new File (root, filename);
// Get size of class file
int size = (int)f.length();
// Reserve space to read
byte buff[] = new byte[size];
// Get stream to read from
FileInputStream fis = new FileInputStream(f);
DataInputStream dis = new DataInputStream (fis);
// Read in data
dis.readFully (buff);
// close stream
dis.close();
// return data
return buff;
}
}
-- CODE --
Thanks In Advance,
Frank
---
ICQ# 7205161
http://sapphire.sourceforge.net - Yet Another X11 Window Manager
Send FREE Greetings for Father's Day--or any day!
Click here: http://www.whowhere.lycos.com/redirects/fathers_day.rdct
Previous post | Next post | Timeline | Home