I have the following file structure from Eclipse for my Java app which I turned into a windows service using Apache Commons Daemon.
SubscriberACD
/Maven Dependencies
/src/main/java
/org.SubscriberACD
/Subscriber.java
/src/test/java
/JRE System Librar
/src
/target
config.xml
pom.xml
In Subscriber.java, here is where the string is defined:
private static final String CONFIG_FILE = "config.xml";
Here is the code snippet to read from the xml file (based off of this post: Java: How to read and write xml files?):
Document document;
DocumentBuilderFactory doc_builder_factory = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder doc_builder = doc_builder_factory.newDocumentBuilder();
document = doc_builder.parse(CONFIG_FILE);
My file directory for the service looks like this:
E:\SubscriberACD
\bin
\subscriberACD.exe
\subscriberACDw.exe
\classes
\org
\SubscriberACD
\Subscriber.class
\config.xml
\3rdpartyjarfiles
\SubscriberACD.jar
\logs
Notice how I stuck config.xml under SubscriberACD under classes just to try it out. But it didn't work. Also worth noting that I also exported my project as a SubscriberACD.jar and put that under \classes folder. From eclipse, it does look like config.xml is also being packaged in that jar. I get the following error when I try to start my windows service:
2016-04-16 15:08:27 Commons Daemon procrun stderr initialized
Exception in thread "main" n\config.xml (The system cannot find the file specified)
java.lang.NumberFormatException: For input string: "null"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:492)
at java.lang.Integer.parseInt(Integer.java:527)
at zmq.TcpAddress.resolve(TcpAddress.java:105)
at zmq.Address.resolve(Address.java:98)
at zmq.SocketBase.connect(SocketBase.java:510)
at org.zeromq.ZMQ$Socket.connect(ZMQ.java:1246)
at org.SubscriberACD.Subscriber.start(Subscriber.java:114)
at org.SubscriberACD.Subscriber.windowsService(Subscriber.java:61)
Was there some additional configuration that I missed?
Try this: Put the file config.xml directly into the classes folder:
E:\SubscriberACD\classes\config.xml
Then in Subscriber.java try to load the file like this:
Document document;
DocumentBuilderFactory doc_builder_factory = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder doc_builder = doc_builder_factory.newDocumentBuilder();
try(InputStream instream = Thread.currentThread().getContextClassLoader().getResourceAsStream("config.xml")) {
document = doc_builder.parse(instream);
}
} catch(SAXException | ParserConfigurationException e) {
throw new RuntimeException(e);
}