在运行时将包含 java 源的文件夹添加到类路径
是否可以添加包含 java 源代码的文件夹作为类路径元素。我尝试了一些方法,似乎类加载器没有拾取 java 源文件?我的尝试之一如下所示......
File uncompressedSrc = new File("uncompressed" + File.separator + "src" + File.separator);
URL uncompressedSrcURL = null;
try {
uncompressedSrcURL = new URL("file://"
+ uncompressedSrc.getAbsolutePath());
} catch (MalformedURLException e) {
e.printStackTrace();
}
URL elements[] = { uncompressedSrcURL };
new URLClassLoader(elements, ClassLoader.getSystemClassLoader());
Is it possible to add a folder which contains java source code as a classpath element. I have tried a few things and it seems that the classloadr is not picking up java soruce files? One of my attempts is shown below....
File uncompressedSrc = new File("uncompressed" + File.separator + "src" + File.separator);
URL uncompressedSrcURL = null;
try {
uncompressedSrcURL = new URL("file://"
+ uncompressedSrc.getAbsolutePath());
} catch (MalformedURLException e) {
e.printStackTrace();
}
URL elements[] = { uncompressedSrcURL };
new URLClassLoader(elements, ClassLoader.getSystemClassLoader());
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我找到了解决问题的方法...我使用了以下肮脏的“hack”将文件夹添加到类路径中...
I have found a solution to my problem... I used the following dirty "hack" to add a folder to class path...
Java 源代码不是 JVM 本身可以处理的。类加载器只能加载已编译的类文件。因此,您只能将 JAR 文件或 CLASS 文件的内容添加到类路径中。
Java source code is nothing the JVM can handle by itself. Only compiled class files may be loaded by the classloader. So you may only add the content of JAR files or CLASS files to the classpath.
对不起。我快速浏览了你的问题。
正如@Daniel所说,JVM无法读取
.java
文件,只能读取.class
文件。java 文件可以编译成类文件并以编程方式加载到 JVM 中,如下所述:使用 Java 以编程方式编译和执行
关键成分是以下
希望有帮助!
Sorry. I skimmed through your question way to fast.
As @Daniel says, the JVM can not read
.java
files, only.class
files.The java-files can be compiled into class-files and loaded in the JVM programatically as described here: Programmatically Compile and Execute with Java
The key ingredient is the following
Hope that helps!