0

I made a windows service from a jar file using WinRun4J, so far it's very basic.

package org.boris.winrun4j.test;

import java.io.BufferedWriter;
import java.io.FileWriter;

import org.boris.winrun4j.Service;
import org.boris.winrun4j.ServiceException;

public class ServiceWrite implements Service
{   
private volatile boolean shutdown = false;

public int serviceMain(String[] args) throws ServiceException {
    int count = 0;
    while (!shutdown) {
        try {
            Thread.sleep(6000);
        } catch (InterruptedException e) {

        }
        try {
            FileWriter fstream = new FileWriter("result.txt");
            BufferedWriter out = new BufferedWriter(fstream);
            out.write("Counts: " + count);
            out.close();
        } catch (Exception e){

        }
        count++;
    }
    return 0;
}
public int serviceRequest(int control) throws ServiceException {
    switch (control) {
    case SERVICE_CONTROL_STOP:
    case SERVICE_CONTROL_SHUTDOWN:
        shutdown = true;
        break;
    }
    return 0;
}
}

When the service is started it just keeps writing every couple of seconds to result.txt located in the root folder.. (Just for trying out WinRun4J)

Now my question is, can I do a method in the service jar, like this

public void write(String s){
 //Write some string s to result.txt
}

And then invoke this method from a different java file on the system, i.e

java WriteToFile SomeString

Where WriteToFile is supposed to invoke write with some argument.

Is it possible? if so, how ?

The overall purpose of this is to have a service running where I can invoke methods via a GUI.

Rayf
  • 451
  • 3
  • 12

1 Answers1

0

to "invoke methods via a GUI", you can't do it with WinRun4J. in general rule, a Windows Service can't have a GUI for security reason (except for special cases).

However, there are other tools to create a windows service from a Java application, with which it will be possible to have a service with GUI and able to interact with the Desktop.

bb67
  • 160
  • 3
  • 10