我想打开我的自定义 Ant 任务的源文件。我认为这很容易,因为类加载器获取资源肯定会立即为我找到源文件。错了!
这是我的代码:
//build the name of the template
StringBuilder sb = new StringBuilder(VersionTemplate.class.getName());
sb.append(".java");
String templateName = sb.toString();
//find the template
InputStream inputStream = VersionTemplate.class.getResourceAsStream(templateName);
inputStream 始终为空。
有什么想法吗?
最佳答案
尝试:
templateName = templateName.replaceAll(".","/");
InputStream inputStream = VersionTemplate.class.getClassLoader().
getResourceAsStream(templateName);
请确保“.java”文件与编译“.class”文件位于同一包中。
关于java - 读取自定义 Ant 任务的源文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12896067/