1

I have problem in accessing VM arguments in My program. I am writing an annotation processor in which i want to access some VM arguments. I am Using NETBeans IDE. I create the jar file of the annotation processor and then use it in another project which has java files with the annotations.

Now in my annotation processor project, In IDE i set the VM arguments as follows

-Dname="hello from VM"

and in the process() function of the annotation processor when i try to access it

String property = System.getProperty("name");

System.out.println(property);

It prints null. Can we access these VM arguments during compile time As both the annotation processor and the annotated class run in the same VM ? Thanks

Andremoniy
  • 34,031
  • 20
  • 135
  • 241
yash6
  • 141
  • 3
  • 14
  • How your problem is related with annotation? – Andremoniy Jan 25 '13 at 11:52
  • Duplicate of http://stackoverflow.com/questions/1490869/how-to-get-vm-arguments-from-inside-of-java-application? – Thirler Jan 25 '13 at 11:57
  • @Thirler - i have seen the link , i tried in those but i was unsuccessful. I am not sure if we can access the VM arguments during compile time , so to give the context of where i am trying to access i have mentioned annotation processing. – yash6 Jan 25 '13 at 12:08
  • @Andremoniy I am trying to access source files in annotation processor. But im unable to get the source path through mirror or reflection api. So i tout of storing the source path and access it in annotation processor. Since both the annotation processor and the annotated class will be run on the same JVM one of my friends suggested using JVM arguments. Hence i am trying to access them like this. – yash6 Jan 26 '13 at 05:29

3 Answers3

1

I do not know of a way to access system properties from the annotation processor, but I think that the annotation processor options would support your use case. Essentially you would want to implement getSupportedOptions in your processor, access the options via processingEnv.getOptions, and pass the options on the command line with -Aname=value. Supported options may also be specified via the @SupportedOptions annotation.

jbunting
  • 886
  • 5
  • 10
0

You can pass options to the JVM that runs javac using -J, so

-J-Dname="hello from VM"

may possibly do what you require.

Ian Roberts
  • 120,891
  • 16
  • 170
  • 183
0

This works for me in Netbeans 8.0 under Windows 8.1 x64 with Java 1.8.0_05. I can only presume its been fixed lately.

    Messager cm = processingEnv.getMessager();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        System.getProperties().storeToXML(baos, "System Properties");
    }
    catch (IOException ex) {
        cm.printMessage(Kind.ERROR, "Exception while getting System properties as XML: " + ex.getMessage());
    }
    cm.printMessage(Kind.NOTE, "\System.properties XML:\n" + baos.toString());

This will give me

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>System Properties</comment>
<entry key="java.runtime.name">Java(TM) SE Runtime Environment</entry>
<entry key="java.vm.vendor">Sun Microsystems Inc.</entry>
...
</properties>

The anwser of jbunting (i.e using processingEnv.getOptions()) is definitely more correct for cleanly passing key-value pairs to your annotation processor.

Atorian
  • 777
  • 10
  • 26