< prev index next >

test/lib/jdk/test/lib/process/ProcessTools.java

Print this page

        

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2013, 2020, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.

@@ -21,15 +21,17 @@
  * questions.
  */
 
 package jdk.test.lib.process;
 
+import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.io.PrintStream;
 import java.nio.charset.Charset;
+import java.nio.file.Paths;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.concurrent.CountDownLatch;
 import java.util.Map;

@@ -43,10 +45,11 @@
 import java.security.AccessController;
 import java.security.PrivilegedActionException;
 import java.security.PrivilegedExceptionAction;
 
 import jdk.test.lib.JDKToolFinder;
+import jdk.test.lib.Platform;
 import jdk.test.lib.Utils;
 
 public final class ProcessTools {
     private static final class LineForwarder extends StreamPumper.LinePump {
         private final PrintStream ps;

@@ -488,10 +491,48 @@
         OutputAnalyzer analyzer = ProcessTools.executeProcess(pb);
         System.out.println(analyzer.getOutput());
         return analyzer;
     }
 
+    /**
+     * Helper method to create a process builder for launching native executable
+     * test that uses/loads JVM.
+     *
+     * @param executableName The name of an executable to be launched.
+     * @param args Arguments for the executable.
+     * @return New ProcessBuilder instance representing the command.
+     */
+    public static ProcessBuilder createNativeTestProcessBuilder(String executableName,
+                                                                String... args) throws Exception {
+        String executable = Paths.get(System.getProperty("test.nativepath"), executableName)
+            .toAbsolutePath()
+            .toString();
+
+        ProcessBuilder pb = new ProcessBuilder(executable);
+        pb.command().addAll(Arrays.asList(args));
+        addJvmLib(pb);
+        return pb;
+    }
+
+    /**
+     * Adds JVM library path to the native library path.
+     *
+     * @param pb ProcessBuilder to be updated with JVM library path.
+     */
+    public static void addJvmLib(ProcessBuilder pb) throws Exception {
+        String jvmLibDir = Platform.jvmLibDir().toString();
+        String libPathVar = Platform.sharedLibraryPathVariableName();
+        String currentLibPath = pb.environment().get(libPathVar);
+
+        String newLibPath = jvmLibDir;
+        if ( (currentLibPath != null) && !currentLibPath.isEmpty() ) {
+            newLibPath = currentLibPath + File.pathSeparator + jvmLibDir;
+        }
+
+        pb.environment().put(libPathVar, newLibPath);
+    }
+
     private static Process privilegedStart(ProcessBuilder pb) throws IOException {
         try {
             return AccessController.doPrivileged(
                 (PrivilegedExceptionAction<Process>) () -> pb.start());
         } catch (PrivilegedActionException e) {
< prev index next >