@echo off :: This script originally authored by Eric Falsken if [%1]==[] GOTO usage if [%2]==[] GOTO usage ping -n 1 %1 | FIND "TTL=" >NUL IF errorlevel 1 GOTO SystemOffline SC \%1 query %2 | FIND "STATE" >NUL IF errorlevel 1 GOTO SystemOffline :ResolveInitialState SC \%1 query %2 | FIND "STATE" | FIND "RUNNING" >NUL IF errorlevel 0 IF NOT errorlevel 1 GOTO StopService SC \%1 query %2 | FIND "STATE" | FIND "STOPPED" >NUL IF errorlevel 0 IF NOT errorlevel 1 GOTO StartService SC \%1 query %2 | FIND "STATE" | FIND "PAUSED" >NUL IF errorlevel 0 IF NOT errorlevel 1 GOTO SystemOffline echo Service State is changing, waiting for service to resolve its state before making changes sc \%1 query %2 | Find "STATE" timeout /t 2 /nobreak >NUL GOTO ResolveInitialState :StopService echo Stopping %2 on \%1 sc \%1 stop %2 %3 >NUL GOTO StopingService :StopingServiceDelay echo Waiting for %2 to stop timeout /t 2 /nobreak >NUL :StopingService SC \%1 query %2 | FIND "STATE" | FIND "STOPPED" >NUL IF errorlevel 1 GOTO StopingServiceDelay :StopedService echo %2 on \%1 is stopped GOTO StartService :StartService echo Starting %2 on \%1 sc \%1 start %2 >NUL GOTO StartingService :StartingServiceDelay echo Waiting for %2 to start timeout /t 2 /nobreak >NUL :StartingService SC \%1 query %2 | FIND "STATE" | FIND "RUNNING" >NUL IF errorlevel 1 GOTO StartingServiceDelay :StartedService echo %2 on \%1 is started GOTO:eof :SystemOffline echo Server \%1 or service %2 is not accessible or is offline GOTO:eof :usage echo Will restart a remote service, waiting for the service to stop/start (if necessary) echo. echo %0 [system name] [service name] {reason} echo Example: %0 server1 MyService echo. echo For reason codes, run "sc stop" GOTO:eof
try {
System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out), true, "UTF-8"));
} catch (UnsupportedEncodingException e) {
throw new InternalError("VM does not support mandatory encoding UTF-8");
}
ProcessBuilder pb = new ProcessBuilder("cmd.exe", "/c", "chcp", "65001").inheritIO();
Process p = pb.start();
p.waitFor();
protected static void setEnv(Map<String, String> newenv) throws Exception {
try {
Class<?> processEnvironmentClass = Class.forName("java.lang.ProcessEnvironment");
Field theEnvironmentField = processEnvironmentClass.getDeclaredField("theEnvironment");
theEnvironmentField.setAccessible(true);
Map<String, String> env = (Map<String, String>) theEnvironmentField.get(null);
env.putAll(newenv);
Field theCaseInsensitiveEnvironmentField = processEnvironmentClass.getDeclaredField("theCaseInsensitiveEnvironment");
theCaseInsensitiveEnvironmentField.setAccessible(true);
Map<String, String> cienv = (Map<String, String>) theCaseInsensitiveEnvironmentField.get(null);
cienv.putAll(newenv);
} catch (NoSuchFieldException e) {
Class[] classes = Collections.class.getDeclaredClasses();
Map<String, String> env = System.getenv();
for(Class cl : classes) {
if("java.util.Collections$UnmodifiableMap".equals(cl.getName())) {
Field field = cl.getDeclaredField("m");
field.setAccessible(true);
Object obj = field.get(env);
Map<String, String> map = (Map<String, String>) obj;
map.clear();
map.putAll(newenv);
}
}
}
}
public static List<Node> asList(NodeList n) {
return n.getLength()==0?
Collections.<Node>emptyList(): new NodeListWrapper(n);
}
static final class NodeListWrapper extends AbstractList<Node>
implements RandomAccess {
private final NodeList list;
NodeListWrapper(NodeList l) {
list=l;
}
public Node get(int index) {
return list.item(index);
}
public int size() {
return list.getLength();
}
}