0

Hey all new to Powershell so here goes.

I am wanting to take the code below and convert it into java so that I do not have to run a separate Powershell script outside my program when its running.

[string] $wspaceDir         = "C:\Temp\loadDir",
[string] $scmLoc            = "C:\Local Apps\IBM\SDP\scmtools\eclipse\scm.exe",
[string] $portNumForDaemon  = "61938"

start-job -scriptblock {
    Set-Location "$($args[1])"
    Write-Host "Starting Daemon on port $args[2]" -foregroundcolor "Yellow"
    &"$($args[0])" "daemon" "start" "$($args[1])" "--port" $args[2]
} -ArgumentList $scmLoc, $wspaceDir, $portNumForDaemon

And so far the info I have on what its doing above is running that in a separate thread in order to carry on and not wait on it to finish.

In java I have tried to mimic that:

private static String portNumForDaemon  = "61938";
private static String scmLoc            = "C:\\Local Apps\\IBM\\SDP\\scmtools\\eclipse\\scm.exe";
private static String wspaceDir         = "C:\\TempRQM";

public static void blah() {
    ExecutorService pool = Executors.newSingleThreadExecutor();
    ProcessBuilder processBuilder = new ProcessBuilder();
    processBuilder.command("cmd.exe", "/c", scmLoc + " daemon start " + wspaceDir + " --port " + portNumForDaemon);

    try {

        Process process = processBuilder.start();

        System.out.println("process ping...");
        ProcessReadTask task = new ProcessReadTask(process.getInputStream());

        List<String> supplierNames = new ArrayList<String>();
        Future<list<string>> future = pool.submit(task);

        // no block, can do other tasks here
        System.out.println("process task1...");
        System.out.println("process task2...");

        List<string> result = future.get(5, TimeUnit.SECONDS);
        for (String s : result) {
            System.out.println(s);
        }

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        pool.shutdown();
    }                
}

private static class ProcessReadTask implements Callable<list<string>> {
    private InputStream inputStream;

    public ProcessReadTask(InputStream inputStream) {
        this.inputStream = inputStream;
    }

    @Override
    public List<string> call() {
        return new BufferedReader(new InputStreamReader(inputStream))
            .lines()
            .collect(Collectors.toList());
    }
}

And as much as I would like to run this I am getting an error on all the implements Callable< list >, Future< list >.

I am not sure if I am doing this correctly or not. I don't even know for sure that's equivalent to what the Powershell is doing?

And Powershell/Java guru able to help me out would be great!

StealthRT
  • 10,108
  • 40
  • 183
  • 342
  • The PowerShell command doesn't monitor job output, as you state (and it doesn't create _threads_: background jobs started with `Start-Job` run in a _child process_). But you need to do so in Java? Note that there's no need to invoke via `cmd /c` - just invoke `scmLoc` with its arguments directly. – mklement0 Oct 04 '19 at 19:55
  • Can you show an example please @mklement0 – StealthRT Oct 07 '19 at 18:36
  • I can't help you with the `Callable` and `Future` issues (incidentally, it may help if you edit the question to include the error messages you're getting), but I presume that your `processBuilder` command would work as follows: `processBuilder.command(scmLoc, "daemon", "start", wspaceDir, "--port", portNumForDaemon);` – mklement0 Oct 07 '19 at 18:51

0 Answers0