[英]Loading a class in a .jar file created programmatically
Here's my goal: Use SableCC to generate several parsers/lexers at runtime and load instances of them for parsing and lexing. 我的目标是:使用SableCC在运行时生成多个解析器/词法分析器,并加载它们的实例进行解析和词法分析。
So I figured my steps would be: 所以我想我的步骤将是:
Generate the .java
files using SableCC. 使用SableCC生成.java
文件。 I think I've done this, fine. 我想我已经做到了,很好。
Compile the .java
file to .class
files. 将.java
文件编译为.class
文件。 I did this using JavaCompiler
. 我使用JavaCompiler
做到了这一点。 I think this worked fine, as well. 我认为这也很好。
Put the generated files in a .jar
file, preserving the file structure. 将生成的文件放在.jar
文件中,保留文件结构。 I did this using this as a template. 我使用此模板作为模板。 Examining the .jar
file using JarBrowser showed that the file structure of the packages was preserved. 使用JarBrowser检查.jar
文件显示软件包的文件结构已保留。 Using it, the Jar Catalog looks the same as a regular .jar
file. 使用它,Jar目录看起来与常规的.jar
文件相同。 However, using JarBrowser, my jar doesn't show up under "CLASSPATH catalog." 但是,使用JarBrowser,我的jar不会显示在“ CLASSPATH目录”下。 This leads me to believe that my .jar
file isn't being created correctly. 这使我相信我的.jar
文件未正确创建。
Load the .jar
file into an instance of URLClassLoader
. 将.jar
文件加载到URLClassLoader
的实例中。 I'm trying to load it using Class.forName()
but it doesn't seem to be working. 我正在尝试使用Class.forName()
加载它,但似乎无法正常工作。
Whenever I do step 4., I get a java.lang.ClassNotFoundException
. 每当执行步骤4时,我都会收到java.lang.ClassNotFoundException
。 Does anyone have any ideas as to why this is? 有人对这是为什么有任何想法吗?
It turns out that I had a slash as a prefix to the file/directory names. 原来,我在文件/目录名称的前面加了一个斜杠。 I was truncating the parent directory from the file name, but didn't take into account the trailing slash from the parent directory, so /dir1/file1
was being done instead of dir1/file1
. 我从文件名中截断了父目录,但是没有考虑到父目录的尾部斜杠,所以/dir1/file1
代替了dir1/file1
。 I guess JAR
files are picky. 我猜JAR
文件很挑剔。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.