简体   繁体   中英

How to include .java files as embedded resources in Eclipse project

I want to include some files as embedded resources in an Eclipse Java project, so that at runtime I can read them using

getClass().getResourceAsStream("somefile")

For most files this works just by adding them to a directory inside the project, but some of the files to have the extension ".java". Eclipse naturally tries to compile them as part of the project. How do I tell it not compile them, but include them as embedded resources?

You could create a separate folder and add it to your classpath as a library. See Add Class Folder in the Java Build Path Page .

For example, I added a folder called res and changed my .classpath file to:

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" path="src"/>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jre7"/>
    <classpathentry kind="lib" path="res"/>
    <classpathentry kind="output" path="bin"/>
</classpath>

You may have to script building the JAR externally to include only the non-compiled sources.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM