Reputation: 141
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
Upvotes: 1
Views: 1302
Reputation: 817
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.
Upvotes: 0
Reputation: 122364
You can pass options to the JVM that runs javac
using -J
, so
-J-Dname="hello from VM"
may possibly do what you require.
Upvotes: 0
Reputation: 901
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.
Upvotes: 1