在运行时将包含 java 源的文件夹添加到类路径

发布于 2024-08-31 06:08:17 字数 494 浏览 1 评论 0原文

是否可以添加包含 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

半葬歌 2024-09-07 06:08:17

我找到了解决问题的方法...我使用了以下肮脏的“hack”将文件夹添加到类路径中...

public static void addUrl(URL u) {
    URLClassLoader sysloader = (URLClassLoader) ClassLoader
            .getSystemClassLoader();
    Class<URLClassLoader> sysclass = URLClassLoader.class;

    try {
        Method method = sysclass.getDeclaredMethod("addURL", parameters);
        method.setAccessible(true);
        method.invoke(sysloader, new Object[] { u });
    } catch (Throwable t) {
        t.printStackTrace();
        try {
            throw new IOException(
                    "Error, could not add URL to system classloader");
        } catch (IOException e) {
            logger.log(Level.SEVERE, e.getMessage());
        }
    }
}

I have found a solution to my problem... I used the following dirty "hack" to add a folder to class path...

public static void addUrl(URL u) {
    URLClassLoader sysloader = (URLClassLoader) ClassLoader
            .getSystemClassLoader();
    Class<URLClassLoader> sysclass = URLClassLoader.class;

    try {
        Method method = sysclass.getDeclaredMethod("addURL", parameters);
        method.setAccessible(true);
        method.invoke(sysloader, new Object[] { u });
    } catch (Throwable t) {
        t.printStackTrace();
        try {
            throw new IOException(
                    "Error, could not add URL to system classloader");
        } catch (IOException e) {
            logger.log(Level.SEVERE, e.getMessage());
        }
    }
}
夏末 2024-09-07 06:08:17

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.

卖梦商人 2024-09-07 06:08:17

对不起。我快速浏览了你的问题。

正如@Daniel所说,JVM无法读取.java文件,只能读取.class文件。

java 文件可以编译成类文件并以编程方式加载到 JVM 中,如下所述:使用 Java 以编程方式编译和执行

关键成分是以下

JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager filemanager = compiler.getStandardFileManager( null, null, null );

try {
    Iterable compilationUnits = filemanager.getJavaFileObjects( file );
    compiler.getTask( null, filemanager, null, null, null, compilationUnits ).call();

    filemanager.close();
} catch ( IOException e ) {
    e.printStackTrace();
}

希望有帮助!

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

JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager filemanager = compiler.getStandardFileManager( null, null, null );

try {
    Iterable compilationUnits = filemanager.getJavaFileObjects( file );
    compiler.getTask( null, filemanager, null, null, null, compilationUnits ).call();

    filemanager.close();
} catch ( IOException e ) {
    e.printStackTrace();
}

Hope that helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文