要实现在一个Java项目启动另一个Java程序(一些.class文件),需要用类加载器。
Class.forName()是在本项目已经加载了某些类的时候用来动态注册这些类的,跟楼主的问题无关。
类加载器的用法我简单写一下:
import java.io.*
import java.net.URL
import java.util.Hashtable
public class PathClassLoader extends ClassLoader
{
protected final ClassLoader parent
private final Hashtable definedClasses = new Hashtable()
public PathClassLoader()
{
this(PathClassLoader.class.getClassLoader())
}
public PathClassLoader(ClassLoader _parent)
{
parent = _parent
}
protected synchronized Class loadClass(String name, boolean resolve) throws ClassNotFoundException
{
Class cls = findLoadedClass(name)
if (cls == null &&parent != null)
{
try
{
cls = parent.loadClass(name)
} catch (ClassNotFoundException e)
{
}
}
if (cls == null)
{
cls = findClass(name)
}
if (resolve)
{
resolveClass(cls)
}
return cls
}
public Class loadClass(File file) throws ClassNotFoundException, IOException
{
return loadClass(new FileInputStream(file))
}
public Class loadClass(InputStream in) throws ClassNotFoundException, IOException
{
ByteArrayOutputStream out = new ByteArrayOutputStream()
byte[] buf = new byte[1024]
for ()
{
int l = in.read(buf)
if (l <= 0)
{
break
}
out.write(buf, 0, l)
}
in.close()
return loadClass(out.toByteArray())
}
public Class loadClass(byte[] b) throws ClassNotFoundException
{
return loadClass(b, 0, b.length)
}
public Class loadClass(byte[] b, int off, int len) throws ClassNotFoundException
{
Class cls = defineClass(null, b, off, len)
definedClasses.put(cls.getName(), cls)
return loadClass(cls.getName())
}
protected Class findClass(String name) throws ClassNotFoundException
{
Class cls = (Class) definedClasses.get(name)
if (cls == null)
{
throw new ClassNotFoundException(name)
} else
{
return cls
}
}
public void loadDirectory(File dir) throws ClassNotFoundException, IOException
{
if(dir.isFile())
{
if (dir.getName().endsWith("class"))
{
loadClass(dir)
}
return
}
for (File file : dir.listFiles())
{
if (file.isDirectory())
{
loadDirectory(file)
} else
{
if (file.getName().endsWith("class"))
{
loadClass(file)
}
}
}
}
public static void main(String[] args)
{
PathClassLoader dc = new PathClassLoader()
URL[] urls
try
{
dc.loadDirectory(new File("plugins\\rubyframework\\RubyFrameWorkApp.class"))
Class want = dc.loadClass(new File("plugins\\rubyframework\\RubyFrameWorkView.class"))
} catch (Exception err)
{
System.err.println(err)
}
}
}
1. 建好如下文件夹 注意:lib目录下必须有个和你gem名字一样的rb文件。 [ruby] view plaincopy $ cd hola $ tree . ├── hola.gemspec └── lib └── hola.rb 2. 编写代码 . hola.rb [ruby] view plaincopy % cat lib/hola.rb class Hola def self....扩展程序的是需要到使用偏好延长内加载的,可以按CTRL+SHIFT+ALT+Z 进行加载(看下图)而rb格式请拷贝放置安装Sketchup目录下的Plugins文件夹内重开sketchup即可