【问题标题】:Can't load Properties even though ClassLoader.getResource found the .properties file即使 ClassLoader.getResource 找到了 .properties 文件,也无法加载属性
【发布时间】:2013-01-07 22:55:59
【问题描述】:

我一直在努力解决这个问题: 我正在尝试编写一个注释处理器(使用 Eclipse IDE)
1 - 识别注释并处理它 - 写入发现注释的消息
2 - 使用速度模板引擎生成新的源文件

(使用本教程:http://deors.wordpress.com/2011/10/31/annotation-generators/

我将它导出到 .jar 并与另一个客户端类一起使用。第 1 步是成功的,但在解决第 2 步时,我无法通过这些行:

Properties props = new Properties();  
URL url = this.getClass().getClassLoader().getResource("velocity.properties");
messager.printMessage(Diagnostic.Kind.NOTE, url.toString());  
props.load(url.openStream());  
messager.printMessage(Diagnostic.Kind.NOTE,"Properties loaded.");

url.toString() 工作正常,它接缝发现“velocity.properties”:

jar:file:/C:/Users/Zuz/workspace/Procesor_B/zh.procesor.b.jar!/velocity.properties

但第二条消息(“已加载属性。”)从未出现。之后也没有任何效果。不生成文件。我想一定是出了什么问题

props.load(url.openStream());

我在客户端类中有三个注释,当我处理它们时,它是这样的:
找到第一个注释 - 找到 velocity.properties - 从 props.load(url.openStream()) 跳过直到循环结束。
找到第二个注释 - 相同的重复。
找到第三个注释 - 相同的重复。

编辑:这是我得到的例外:

JAR entry velocity.properties not found in C:\Users\Zuz\workspace\Procesor_B\zh.procesor.b.jar  
java.io.FileNotFoundException: JAR entry velocity.properties not found in C:\Users\Zuz\workspace\Procesor_B\zh.procesor.b.jar

我也试过这个:

props.load(this.getClass().getClassLoader().getResourceAsStream("velocity.properties"));  

但我得到了: java.lang.NullPointerException

您将在下面找到有关处理器和客户端类的更多信息。提前感谢您的帮助!

这是我的注释处理器源代码 - 目前没有任何用处:

    package zh.procesor.b;    

all necessary java, javax imports ommitted for length
import org.apache.log4j.Logger;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.exception.ResourceNotFoundException;

import zh.anotacia.b.Anotacia_B;

@SupportedAnnotationTypes("zh.anotacia.b.Anotacia_B")
@SupportedSourceVersion(SourceVersion.RELEASE_6)
public class Procesor_B extends AbstractProcessor{

private static final Logger LOG =
        Logger.getLogger(Procesor_B.class.getName());

private Filer file; 
private Messager messager;


@Override
public void init(ProcessingEnvironment env){    
    file = env.getFiler();
    messager = env.getMessager();

}

@Override
public boolean process(Set<? extends TypeElement> annotations,
        RoundEnvironment roundEnv)  {
    String className = "Trieda"; 
    String packageName = "Package";
    int i = 1; 

    for (Element elem: roundEnv.getElementsAnnotatedWith(Anotacia_B.class)){
        i++;
        Anotacia_B anot = elem.getAnnotation(Anotacia_B.class);
        String message = "Annotation found in : " + elem.getSimpleName();
        messager.printMessage(Diagnostic.Kind.NOTE, message);

        try{
        Properties props = new Properties();
        URL url = this.getClass().getClassLoader()
                .getResource("velocity.properties");

        messager.printMessage(Diagnostic.Kind.NOTE, url.toString());

        //from here it is not working
        props.load(url.openStream());
        messager.printMessage(Diagnostic.Kind.NOTE, "Properties loaded.");

        VelocityEngine ve = new VelocityEngine(props);
        ve.init(); 

        VelocityContext vc = new VelocityContext();
        vc.put("className", className+i);
        vc.put("packageName", packageName+i);

        Template vt = ve.getTemplate("testtemplate.vm");

        JavaFileObject jfo = file.createSourceFile(
                packageName + "." + className + "Info");

        messager.printMessage(
                Diagnostic.Kind.NOTE,
                "creating source file: " + jfo.toUri());

        Writer writer = jfo.openWriter();

        messager.printMessage(
                Diagnostic.Kind.NOTE,
                "applying velocity template: " + vt.getName());

        vt.merge(vc, writer);

        writer.close();

        } catch (Exception ex) {
            messager.printMessage(Diagnostic.Kind.ERROR, ex.getMessage());
            messager.printMessage(Diagnostic.Kind.ERROR,
                                  "Stacktrace: " + ex.toString());  
            messager.printMessage(Diagnostic.Kind.ERROR,  
                                  ex.getStackTrace().toString());
        }
    }         
    return true;
  }
}

这是客户端类:

    package zh.trieda.b;  

import zh.anotacia.b.Anotacia_B;

@Anotacia_B(vstup = "50", vystup = "50")
public class Rad {  

@Anotacia_B(vstup = "10", vystup = "15")
public void basic(){
    // do something
    }  

@Anotacia_B(vstup = "2", vystup = "30")
public void premium(){
    // do something
    }
}  

我是新来的,所以不能放图片,但是这里有一个带图片的链接
Procesor_B 结构体,Client 类 Trieda_B 结构体,Trieda_B 的 Factory Path

http://bkmbj.wz.cz/structures.jpg

velocity.properties 中的内容如下:

runtime.log.logsystem.class = org.apache.velocity.runtime.log.SystemLogChute
resource.loader = classpath
classpath.resource.loader.class =     org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader

【问题讨论】:

  • props.load(this.getClass().getClassLoader().getResourceAsStream("velocity.properties"); 应该可以工作。结果是什么?例外?
  • ex.getMessage(); ex.getStackTrace(); 不打印异常,这些行没有做任何有用的事情。您可以打印它们以查看实际的异常吗?
  • @MarcelStör @SergiuDumitriu props.load(this.getClass().getClassLoader().getResourceAsStream("velocity.prope‌​rties"); 运作良好!谢谢!但是,当尝试继续时,它仍然会跳过 Velocity Engine init() 的执行代码,直到 cyclus 结束。我得到的例外是这个:org.apache.velocity.exception.VelocityException: 'Error initializing log: Failed to initialize an instance of org.apache.velocity.runtime.log.Log4JLogChute with the current runtime configuration. 对此有什么想法吗?
  • @MarcelStör 我无法接受答案,因为当我再次尝试时,我开始出现 NullpointerException(使用 ResourceAsStream)或 FileNotFound(使用 getResource 时)。我不知道为什么。其他一切都一样。

标签: eclipse properties annotations velocity processor


【解决方案1】:
props.load(this.getClass().getClassLoader().getResourceAsStream("velocity.prope‌​rties");

应该可以。

【讨论】:

    猜你喜欢
    • 2019-10-09
    • 2022-07-25
    • 1970-01-01
    • 1970-01-01
    • 2021-09-03
    • 1970-01-01
    • 2013-11-04
    • 1970-01-01
    • 2019-09-14
    相关资源
    最近更新 更多