作为用于学术目的的 HTTP WebServer 项目的一部分,我正在尝试为 Web 应用程序类编写自己的自定义类加载器,但似乎无法正确完成。
一般来说,Web 应用程序位于它们自己的文件夹中,Web 应用程序的“.class”文件与其直接的父文件夹名称相同。(例如 Web1/Web1.class)。下面的代码可以正常工作,直到我到达该defineClass()方法,然后它会抛出以下异常:
java.io.FileNotFoundException: C:\inetpub\javawwwroot\WebApps\java\lang\Object\.Object.class (The system cannot find the path specified)
值得一提的是,下面代码中的C:\inetpub\javawwwroot\WebApps\部分等于m_WebAppsFullPath变量。
此外,当尝试使用
InputStream in = getResourceAsStream(clsFile);
代替 InputStream in = new FileInputStream(clsFile);
我得到一个空返回值...
更新:简而言之,如何加载既不在“CLASSPATH”中也不在我的项目的任何包中的特定类?
protected synchronized Class loadClass(String className, boolean resolve)
throws ClassNotFoundException
{
log("Loading class: " + className + ", resolve: " + resolve);
// 1. is this class already loaded?
Class cls = findLoadedClass(className);
if (cls != null)
{
return cls;
} …Run Code Online (Sandbox Code Playgroud) 我正在尝试从字节流编写类加载器,但出现此错误。我已经阅读了文档,但我很困惑,因为这不应该发生。这是我要加载的内容:
class LoadedClass {
public void out(String msg) {
System.out.println(msg);
}
}
Run Code Online (Sandbox Code Playgroud)
它存储在一个字节数组中
这是我的类加载器代码:
package byteclassloader;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
class PrintClass extends ClassLoader {
private String LoadedClass = "cafebabe00000033001e0a0005001309001400150a001600170700180700190100063c696e69743e010003282956010004436f646501000f4c696e654e756d6265725461626c650100124c6f63616c5661726961626c655461626c650100047468697301000d4c4c6f61646564436c6173733b0100036f7574010015284c6a6176612f6c616e672f537472696e673b29560100036d73670100124c6a6176612f6c616e672f537472696e673b01000a536f7572636546696c650100104c6f61646564436c6173732e6a6176610c0006000707001a0c000d001b07001c0c001d000e01000b4c6f61646564436c6173730100106a6176612f6c616e672f4f626a6563740100106a6176612f6c616e672f53797374656d0100154c6a6176612f696f2f5072696e7453747265616d3b0100136a6176612f696f2f5072696e7453747265616d0100077072696e746c6e002000040005000000000002000000060007000100080000002f00010001000000052ab70001b100000002000900000006000100000001000a0000000c000100000005000b000c00000001000d000e00010008000000400002000200000008b200022bb60003b10000000200090000000a00020000000300070004000a00000016000200000008000b000c000000000008000f0010000100010011000000020012";
private String name = "LoadedClass";
public Class findClass() {
byte[] ba = hexStringToByteArray(LoadedClass);
return defineClass(name, ba, 0, ba.length);
}
private static byte[] hexStringToByteArray(String s) {
int len = s.length();
byte data[] = new byte[len / 2];
for( int i = 0; i < len; i+= 2 ) {
data[i / …Run Code Online (Sandbox Code Playgroud) 我正在尝试替换所有使用 Files 和 FileReaders 的实例,并用 InputStreams 和适当的读取器替换它们,以准备将应用程序打包到 jar 中 - 但是,当我尝试使用ClassLoader.getSystemClassLoader().getResourceAsStream()File 方法设法找到的相同路径时,它由于找不到文件而失败。
我使用和输出的代码:
文件中的文本:
e:Easy
definitions/easy_level_definitions.txt
h:Hard
definitions/hard_level_definitions.txt
d:Derp
definitions/derp_level_definitions.txt
Run Code Online (Sandbox Code Playgroud)
代码:
String line;
File f = new File("src/resources/level_sets.txt");
BufferedReader reader1 = null;
try {
reader1 = new BufferedReader(new FileReader(f));
while ((line = reader1.readLine()) != null) {
System.out.println(line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (reader1 != null) {
reader1.close();
}
} catch (IOException e) {
e.printStackTrace(); …Run Code Online (Sandbox Code Playgroud) 我想修补我在 war 项目中使用的库的 java 类。
我已使用具有相同规范名称的新类覆盖了该类,但在 Web 应用程序中,仍首先加载原始类。如何控制类加载顺序?
我从这篇文章开始,阅读了 JAR Hell: Override class in java
并使用了这里找到的maven示例: http://owenou.com/2010/07/20/patching-with-class-shadowing-and-maven.html
基本上是一个辅助项目,依赖于带有覆盖类和原始库的新修补 jar,以及 MANIFEST.MF 以及这些库在类路径中的正确顺序。所以其他项目可以使用这个辅助项目。
这在独立应用程序中工作得很好,但在 Web 项目中,原始 jar 仍然首先加载。
有什么帮助吗?
我有 nio 通道,我的客户端应该从服务器计算机加载类文件。它们的IP范围相同。我有两个在服务器和客户端计算机上常见的接口。以及在服务器计算机上实现接口的类。我在我的客户端机器上使用以下代码,但是当我运行它时会出现ClassNotFoundException。
URL url = new URL("file:///E:/Computing/Master/classes/" );
URLClassLoader ucl = new URLClassLoader(new URL[]{url});
Class clazz = ucl.loadClass("com.counter.controller.Action");
ProcessAlgorithm iAction = (ProcessAlgorithm) clazz.newInstance();
Run Code Online (Sandbox Code Playgroud)
本例中类加载的完整流程是怎样的?
我正在探索 Java 类加载器,然后我遇到了SecureClassLoader.
在查看其源代码并阅读一些文章后,我意识到我无法理解它的安全功能和使用范围。
谁能解释一下它的SecureClassLoader用途是什么?
为什么它“安全”?
谢谢。
我有以下 Scala 类层次结构:
abstract class BaseModule(val appConf : AppConfig) {
// ...
}
class SimpleModule(appConf : AppConfig) extends BaseModule(appConf) {
// ...
}
class FairlyComplexModule(appConf : AppConfig) extends BaseModule(appConf) {
// ...
}
// dozens of other BaseModule subclasses...
Run Code Online (Sandbox Code Playgroud)
在运行时,我的应用程序将接受BaseModule要实例化的子类的完全限定类名的 String 输入参数,但代码不知道它将是哪个具体子类。所以我有:
val moduleFQCN = loadFromInputArgs() // ex: "com.example.myapp.SimpleModule"
val moduleClass = Class.forName(moduleFQCN)
println(s"Found ${moduleFQCN} on the runtime classpath.")
val module = Class.forName(moduleFQCN).getConstructor(classOf[AppConfig]).newInstance(appConf).asInstanceOf[BaseModule]
Run Code Online (Sandbox Code Playgroud)
因此,通过这种方式,输入指定BaseModule要在类路径上查找的子类,然后再进行实例化。上面的前三行执行得很好,我看到了println火。但是上面的最后一行抛出了一个异常:
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) …Run Code Online (Sandbox Code Playgroud) scala 是否有类似于类加载器对 java 中的静态块所做的事情?
例如,scala 中类似于下面的内容:
class A{
static{
System.out.println("This gets called at the time of loading a class by class loader.")
}
}
Run Code Online (Sandbox Code Playgroud)
我正在使用 Scala 2.x 和 Apache Spark 2.x
PS:我已经读过什么是Scala相当于Java的静态块?这个答案,但我不想创建伴生对象,然后通过类的构造函数调用它。
编辑:[我的用例]
考虑java的一个场景,我们在静态块中编写Class.forName(“some.jdbc.driver”),然后将jdbc驱动程序放在类路径中。之后类加载器加载我们提到的类。我想做一些完全一样的事情
我们计划将 tomcat 服务器(我们当前使用的版本是 7)迁移到版本 9,目前我们正在使用 VirtualWebAppLoader 类来创建自定义类路径,即从外部文件夹加载 jars。但是在 Tomcat 9 中,这个加载器类不再可用。如何在 tomcat 9 中实现这一目标?
每当我在 Eclipse 中部署 Liferay portlet 的 war 文件时,我都会收到以下错误。任何人都可以帮助我知道为什么会发生这种情况以及如何解决它。
java.lang.LinkageError: loader constraint violation: when resolving overridden method "com.liferay.portal.spring.extender.internal.context.ModuleBeanFactoryPostProcessor.postProcessBeanFactory(Lorg/springframework/beans/factory/config/ConfigurableListableBeanFactory;)V" the class loader (instance of org/eclipse/osgi/internal/loader/EquinoxClassLoader) of the current class, com/liferay/portal/spring/extender/internal/context/ModuleBeanFactoryPostProcessor, and its superclass loader (instance of org/apache/catalina/loader/WebappClassLoader), have different Class objects for the type org/springframework/beans/factory/config/ConfigurableListableBeanFactory used in the signature
at com.liferay.portal.spring.extender.internal.context.ModuleApplicationContextRegistrator._createApplicationContext(ModuleApplicationContextRegistrator.java:138)
at com.liferay.portal.spring.extender.internal.context.ModuleApplicationContextRegistrator.start(ModuleApplicationContextRegistrator.java:60)
at sun.reflect.GeneratedMethodAccessor756.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.felix.dm.InvocationUtil.invokeMethod(InvocationUtil.java:111)
at org.apache.felix.dm.InvocationUtil.invokeCallbackMethod(InvocationUtil.java:66)
at org.apache.felix.dm.impl.ComponentImpl.invokeCallbackMethod(ComponentImpl.java:769)
at org.apache.felix.dm.impl.ComponentImpl.invoke(ComponentImpl.java:760)
at org.apache.felix.dm.impl.ComponentImpl.bindService(ComponentImpl.java:705)
at org.apache.felix.dm.impl.ComponentImpl.access$400(ComponentImpl.java:54)
at org.apache.felix.dm.impl.ComponentImpl$7.run(ComponentImpl.java:202)
at org.apache.felix.dm.impl.SerialExecutor.runTask(SerialExecutor.java:137)
at org.apache.felix.dm.impl.SerialExecutor.runTasks(SerialExecutor.java:119)
at org.apache.felix.dm.impl.SerialExecutor.execute(SerialExecutor.java:85)
at org.apache.felix.dm.impl.ComponentImpl.calculateStateChanges(ComponentImpl.java:252) …Run Code Online (Sandbox Code Playgroud) classloader ×10
java ×8
scala ×2
apache-spark ×1
classpath ×1
inputstream ×1
liferay-7 ×1
overriding ×1
sockets ×1
static-block ×1
tomcat ×1
tomcat7 ×1
war ×1