如何使java.io.File成为java.lang.Class来运行它? [英] How do I make a java.io.File into a java.lang.Class to run it?

查看:116
本文介绍了如何使java.io.File成为java.lang.Class来运行它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序,让用户选择任何 .class .jar 文件并运行它。问题是,要运行它,我需要使用 java.io.File 之外的其他东西,这是 JFileChooser 返回。如何使 java.io.File 变成 java.lang.Class java.util.jar.JarFile

I have a program that lets a user select any .class or .jar file and run it. The problem is that, to run it, I need to use something other than a java.io.File, which is what a JFileChooser returns. How can I make a java.io.File into a java.lang.Class or java.util.jar.JarFile?

推荐答案

如果你有一个文件要加载的类的对象,可以使用 URLClassLoader 加载类。 文件对象可以提供 URL

If you have a File object of the class you want to load, you can use a URLClassLoader to load the class. The File object can provide the URL.

这篇关于如何使java.io.File成为java.lang.Class来运行它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆