我有一个像下面这样的字符串:
String str = "4*5";
Run Code Online (Sandbox Code Playgroud)
现在我必须20通过使用字符串来获得结果.
我知道在其他一些语言中,这个eval()功能会做到这一点.我怎么能用Java做到这一点?
我被要求构建一个能够在运行时加载新代码(扩展)的java系统.在代码运行时如何重新加载jar文件?或者我如何装一个新的罐子?
显然,由于恒定的正常运行时间很重要,我想添加在其中重新加载现有类的能力(如果它不会使事情复杂化太多).
我应该注意什么?(把它想象成两个不同的问题 - 一个关于在运行时重新加载类,另一个关于添加新类).
是否可以在运行时将文件(不一定是jar文件)添加到java类路径.具体来说,该文件已存在于类路径中,我想要的是是否可以将此文件的修改后的副本添加到类路径中.
谢谢,
我正在尝试创建一个Java工具,它将扫描Java应用程序的结构并提供一些有意义的信息.为此,我需要能够从项目位置(JAR/WAR或只是一个文件夹)扫描所有.class文件,并使用反射来阅读他们的方法.事实证明这几乎是不可能的.
我可以找到许多基于URLClassloader的解决方案,它允许我从目录/存档加载特定的类,但是没有一个允许我加载类而没有关于类名或包结构的任何信息.
编辑:我想我的说法很差.我的问题不是我无法获得所有的类文件,我可以通过递归等来做到这一点并正确定位它们.我的问题是为每个类文件获取一个Class对象.
我正在尝试为我的应用程序创建一个插件系统,我想从简单的东西开始.每个插件都应该打包在.jar文件中并实现SimplePlugin接口:
package plugintest;
public interface SimplePlugin {
public String getName();
}
Run Code Online (Sandbox Code Playgroud)
现在我创建了一个SimplePlugin包装在.jar中的实现,并将其放在主应用程序的plugin /子目录中:
package plugintest;
public class PluginTest implements SimplePlugin {
public String getName() {
return "I'm the plugin!";
}
}
Run Code Online (Sandbox Code Playgroud)
在主应用程序中,我想获得一个实例PluginTest.我尝试了两种方法,都使用了java.util.ServiceLoader.
1.动态扩展类路径
这使用已知的hack在系统类加载器上使用反射来避免封装,以便添加URL类路径.
package plugintest.system;
import plugintest.SimplePlugin;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Iterator;
import java.util.ServiceLoader;
public class ManagePlugins {
public static void main(String[] args) throws IOException {
File loc = new File("plugins");
extendClasspath(loc);
ServiceLoader<SimplePlugin> sl …Run Code Online (Sandbox Code Playgroud) 是否可以将条目添加到netbeans生成的jar的manifest.mf文件中?
例如,构建一个osgi包.
我希望使用java代码动态地将jar文件添加到我的项目的类路径中,如果可能的话,我想使用外部jar文件并加载它们的类,将它们作为Beans稍后执行(Spring框架).
谢谢 :)
我需要为应用程序的某些部分添加插件功能到现有应用程序.我希望能够在运行时添加一个jar,应用程序应该能够从jar加载一个类而无需重新启动应用程序.到现在为止还挺好.我使用URLClassLoader在线发现了一些样本,它工作正常.
我还希望能够在jar的更新版本可用时重新加载同一个类.我再次找到了一些示例和实现这一点的关键,据我所知,我需要为每个新加载使用一个新的类加载器实例.
我写了一些示例代码但是遇到了NullPointerException.首先让我告诉你们代码:
package test.misc;
import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
import plugin.misc.IPlugin;
public class TestJarLoading {
public static void main(String[] args) {
IPlugin plugin = null;
while(true) {
try {
File file = new File("C:\\plugins\\test.jar");
String classToLoad = "jartest.TestPlugin";
URL jarUrl = new URL("jar", "","file:" + file.getAbsolutePath()+"!/");
URLClassLoader cl = new URLClassLoader(new URL[] {jarUrl}, TestJarLoading.class.getClassLoader());
Class loadedClass = cl.loadClass(classToLoad);
plugin = (IPlugin) loadedClass.newInstance();
plugin.doProc();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
Thread.sleep(30000);
} catch (InterruptedException …Run Code Online (Sandbox Code Playgroud) 我的一些代码存在问题,我搜索并尝试了我所知道的一切,没有任何运气.
场景:
如果它不存在,应用程序将下载JDBC驱动程序并将其添加到ClassLoader,如下所示:( 注意:storageDataManager是我用于SQL方法的类)
File h2Driver = new File(directory.toString() + File.separator + "lib" + File.separator + "h2.jar");
if (h2Driver.exists()) {
URL[] url = new URL[0];
try {
url = new URL[]{h2Driver.toURI().toURL()};
storageDataManager.setClassLoader(new URLClassLoader(url));
} catch (MalformedURLException ignore) {}
}
Run Code Online (Sandbox Code Playgroud)当storageDataManager运行第一个查询时,它会尝试连接指定的驱动程序,如果它有一个ClassLoader,它会使用ClassLoader:
if (getClassLoader() != null) {
getLogging().debug("Loading custom class loader for H2 driver: " + getClassLoader().toString());
Driver driver = (Driver) Class.forName("org.h2.Driver", true, getClassLoader()).newInstance();
getLogging().debug("Loaded H2 driver: " + driver.toString() + " - " + driver.getMinorVersion() + " - " …Run Code Online (Sandbox Code Playgroud) 我试图动态地将类加载到组件中.我正在使用文件选择器来选择将要加载的.JAR文件,然后使用选项窗格来获取类的名称.
我已经在互联网上搜索如何将java文件转换为URL以便在URLClassLoader中加载它我已经提出:
File myFile = filechooser.getSelectedFile();
String className = JOptionPane.showInputDialog(
this, "Class Name:", "Class Name", JOptionPane.QUESTION_MESSAGE);
URL myUrl= null;
try {
myUrl = myFile.toURL();
} catch (MalformedURLException e) {
}
URLClassLoader loader = new URLClassLoader(myUrl);
loader.loadClass(className);
Run Code Online (Sandbox Code Playgroud)
我现在收到一个'无法找到符号'错误,将URL加载到URLClassLoader中
java ×9
classloader ×3
classpath ×3
jar ×3
runtime ×2
class ×1
dynamic ×1
eval ×1
h2 ×1
jdbc ×1
load ×1
manifest ×1
manifest.mf ×1
netbeans ×1
netbeans6.7 ×1
reflection ×1
spring ×1
swing ×1