Skip to content

Commit

Permalink
Avoid deprecated Runtime.exec()
Browse files Browse the repository at this point in the history
  • Loading branch information
Mailaender committed Dec 6, 2023
1 parent 4fdd345 commit d152140
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 83 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2014, 2018 Lablicate GmbH.
* Copyright (c) 2014, 2023 Lablicate GmbH.
*
* All rights reserved.
* This program and the accompanying materials are made available under the
Expand All @@ -21,6 +21,7 @@ public class WindowsSupport extends AbstractWindowsSupport implements IExtendedR
private IAmdisSupport amdisSupport;

public WindowsSupport(String application, String parameter) throws FileNotFoundException {

super(application, parameter);
amdisSupport = new AmdisSupport(this);
}
Expand All @@ -40,36 +41,32 @@ public IAmdisSupport getAmdisSupport() {
@Override
public Process executeOpenCommand() throws IOException {

Runtime runtime = Runtime.getRuntime();
return runtime.exec(getOpenCommand());
return getOpenCommand().start();
}

@Override
public Process executeKillCommand() throws IOException {

Runtime runtime = Runtime.getRuntime();
return runtime.exec(getKillCommand());
return getKillCommand().start();
}

private String getOpenCommand() {
private ProcessBuilder getOpenCommand() {

/*
* Returns e.g.: "C:\Programs\NIST\AMDIS32\AMDIS32$.exe
*/
StringBuilder builder = new StringBuilder();
builder.append(getApplication().replace("AMDIS32$.exe", "AMDIS_32.exe")); // run the GUI version
return builder.toString();
String amdis = getApplication().replace("AMDIS32$.exe", "AMDIS_32.exe"); // run the GUI version
return new ProcessBuilder(amdis);
}

private String getKillCommand() {
private ProcessBuilder getKillCommand() {

String command = "";
if(isValidApplicationExecutable()) {
/*
* taskkill kills the e.g. AMDIS application.
*/
command = "taskkill /f /IM AMDIS_32.exe";
return new ProcessBuilder("taskkill", "/f", "/IM", "AMDIS_32.exe");
}
return command;
return new ProcessBuilder();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2008, 2019 Lablicate GmbH.
* Copyright (c) 2008, 2023 Lablicate GmbH.
*
* All rights reserved.
* This program and the accompanying materials are made available under the
Expand All @@ -15,6 +15,7 @@
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Map;

import org.eclipse.chemclipse.msd.identifier.supplier.nist.preferences.PreferenceSupplier;
import org.eclipse.chemclipse.support.runtime.AbstractLinuxWineSupport;
Expand All @@ -24,6 +25,7 @@ public class LinuxWineSupport extends AbstractLinuxWineSupport implements IExten
private final INistSupport nistSupport;

public LinuxWineSupport(File applicationFolder, String parameter) throws FileNotFoundException {

super(PreferenceSupplier.getNistExecutable(applicationFolder).getAbsolutePath(), parameter);
nistSupport = new NistSupport(this);
}
Expand Down Expand Up @@ -53,50 +55,35 @@ public INistSupport getNistSupport() {
@Override
public Process executeKillCommand() throws IOException {

Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(getKillCommand());
return process;
return getKillCommand().start();
}

@Override
public Process executeOpenCommand() throws IOException {

Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(getOpenCommand());
return process;
return getOpenCommand().start();
}

private String getKillCommand() {
private ProcessBuilder getKillCommand() {

/*
* "pkill -f nist"
*/
String command = "";
if(isValidApplicationExecutable()) {
StringBuilder builder = new StringBuilder();
builder.append("pkill -f");
builder.append(" ");
builder.append("nist");
command = builder.toString();
/*
* "pkill -f nist"
*/
return new ProcessBuilder("pkill", "-f", "nist");
}
return command;
return new ProcessBuilder();
}

private String getOpenCommand() {
private ProcessBuilder getOpenCommand() {

/*
* "env WINEPREFIX=/home/eselmeister/.wine wine start C:\\programme\\nist\\MSSEARCH\\nistms.exe"
*/
StringBuilder builder = new StringBuilder();
/*
* LINUX, UNIX
*/
builder.append("env WINEPREFIX=");
builder.append(getWineEnvironment());
builder.append(" ");
builder.append("wine start");
builder.append(" ");
builder.append(getWineApplication().replace("$.exe", ".exe")); // run the GUI version
return builder.toString();
String nistms = getWineApplication().replace("$.exe", ".exe"); // run the GUI version
ProcessBuilder processBuilder = new ProcessBuilder("wine", "start", nistms);
Map<String, String> environment = processBuilder.environment();
environment.put("WINEPREFIX", getWineEnvironment());
return processBuilder;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2008, 2019 Lablicate GmbH.
* Copyright (c) 2008, 2023 Lablicate GmbH.
*
* All rights reserved.
* This program and the accompanying materials are made available under the
Expand All @@ -24,6 +24,7 @@ public class WindowsSupport extends AbstractWindowsSupport implements IExtendedR
private final INistSupport nistSupport;

public WindowsSupport(File applicationFolder, String parameter) throws FileNotFoundException {

super(PreferenceSupplier.getNistExecutable(applicationFolder).getAbsolutePath(), parameter);
nistSupport = new NistSupport(this);
}
Expand All @@ -43,26 +44,23 @@ public INistSupport getNistSupport() {
@Override
public Process executeOpenCommand() throws IOException {

Runtime runtime = Runtime.getRuntime();
return runtime.exec(getApplication());
return new ProcessBuilder(getApplication()).start();
}

@Override
public Process executeKillCommand() throws IOException {

Runtime runtime = Runtime.getRuntime();
return runtime.exec(getKillCommand());
return getKillCommand().start();
}

private String getKillCommand() {
private ProcessBuilder getKillCommand() {

String command = "";
if(isValidApplicationExecutable()) {
/*
* taskkill only kills the NIST-DB application.
*/
command = "taskkill /IM nistms.exe";
return new ProcessBuilder("taskkill", "/IM", "nistms.exe");
}
return command;
return new ProcessBuilder();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2014, 2020 Lablicate GmbH.
* Copyright (c) 2014, 2023 Lablicate GmbH.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
Expand All @@ -13,6 +13,7 @@

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Map;

public abstract class AbstractLinuxWineSupport extends AbstractWineRuntimeSupport implements IWineRuntimeSupport {

Expand All @@ -23,35 +24,25 @@ public abstract class AbstractLinuxWineSupport extends AbstractWineRuntimeSuppor
* @param application
* @param parameter
*/
public AbstractLinuxWineSupport(String application, String parameter) throws FileNotFoundException {
protected AbstractLinuxWineSupport(String application, String parameter) throws FileNotFoundException {

super(application, parameter);
}

@Override
public Process executeRunCommand() throws IOException {

Runtime runtime = Runtime.getRuntime();
return runtime.exec(getRunCommand());
return getRunCommand().start();
}

private String getRunCommand() {
private ProcessBuilder getRunCommand() {

/*
* "env WINEPREFIX=/home/eselmeister/.wine wine start C:\\programme\\nist\\MSSEARCH\\nistms$.exe /INSTRUMENT /PAR=2"
* "env WINEPREFIX=/home/chemclipse/.wine wine start C:\\programme\\nist\\MSSEARCH\\nistms$.exe /INSTRUMENT /PAR=2"
*/
StringBuilder builder = new StringBuilder();
/*
* LINUX, UNIX
*/
builder.append("env WINEPREFIX=");
builder.append(getWineEnvironment());
builder.append(" ");
builder.append("wine start");
builder.append(" ");
builder.append(getWineApplication());
builder.append(" ");
builder.append(getParameter());
return builder.toString();
ProcessBuilder processBuilder = new ProcessBuilder("wine", "start", getWineApplication(), getParameter());
Map<String, String> environment = processBuilder.environment();
environment.put("WINEPREFIX", getWineEnvironment());
return processBuilder;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2014, 2018 Lablicate GmbH.
* Copyright (c) 2014, 2023 Lablicate GmbH.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
Expand Down Expand Up @@ -27,7 +27,8 @@ public abstract class AbstractMacWineSupport extends AbstractWineRuntimeSupport
* @param macWineBinary
* (e.g. "/Applications/Wine.app")
*/
public AbstractMacWineSupport(String application, String parameter, String macWineBinary) throws FileNotFoundException {
protected AbstractMacWineSupport(String application, String parameter, String macWineBinary) throws FileNotFoundException {

super(application, parameter);
this.macWineBinary = macWineBinary;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2014, 2018 Lablicate GmbH.
* Copyright (c) 2014, 2023 Lablicate GmbH.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
Expand All @@ -22,26 +22,22 @@ public abstract class AbstractWindowsSupport extends AbstractRuntimeSupport impl
* @param application
* @param parameter
*/
public AbstractWindowsSupport(String application, String parameter) throws FileNotFoundException {
protected AbstractWindowsSupport(String application, String parameter) throws FileNotFoundException {

super(application, parameter);
}

@Override
public Process executeRunCommand() throws IOException {

Runtime runtime = Runtime.getRuntime();
return runtime.exec(getRunCommand());
return getRunCommand().start();
}

private String getRunCommand() {
private ProcessBuilder getRunCommand() {

/*
* Returns e.g.: "C:\Programs\NIST\MSSEARCH\nistms$.exe /INSTRUMENT /PAR=2
*/
StringBuilder builder = new StringBuilder();
builder.append(getApplication());
builder.append(" ");
builder.append(getParameter());
return builder.toString();
return new ProcessBuilder(getApplication(), getParameter());
}
}

0 comments on commit d152140

Please sign in to comment.