changeset 5368:ea022c57bfb5

7142596: RMI JPRT tests are failing Summary: Changed RMI tests to use random port numbers for the RMI Registry and RMID so the tests can be run concurrently without test failures due to tests using the same port numbers. Reviewed-by: smarks, alanb Contributed-by: olivier.lagneau@oracle.com
author andrew
date Wed, 12 Jun 2013 11:48:21 +0100
parents 38fae1f85c50
children 06ee02f5ca2e
files test/ProblemList.txt test/com/sun/jndi/rmi/registry/RegistryContext/ContextWithNullProperties.java test/com/sun/jndi/rmi/registry/RegistryContext/UnbindIdempotent.java test/java/rmi/Naming/LookupNameWithColon.java test/java/rmi/Naming/RmiIsNoScheme.java test/java/rmi/Naming/UnderscoreHost.java test/java/rmi/Naming/legalRegistryNames/LegalRegistryNames.java test/java/rmi/activation/Activatable/checkActivateRef/security.policy test/java/rmi/activation/Activatable/checkAnnotations/security.policy test/java/rmi/activation/Activatable/checkImplClassLoader/security.policy test/java/rmi/activation/Activatable/checkRegisterInLog/security.policy test/java/rmi/activation/Activatable/createPrivateActivable/security.policy test/java/rmi/activation/Activatable/downloadParameterClass/security.policy test/java/rmi/activation/Activatable/elucidateNoSuchMethod/security.policy test/java/rmi/activation/Activatable/extLoadedImpl/security.policy test/java/rmi/activation/Activatable/forceLogSnapshot/security.policy test/java/rmi/activation/Activatable/inactiveGroup/security.policy test/java/rmi/activation/Activatable/lookupActivationSystem/LookupActivationSystem.java test/java/rmi/activation/Activatable/nestedActivate/security.policy test/java/rmi/activation/Activatable/nonExistentActivatable/security.policy test/java/rmi/activation/Activatable/restartCrashedService/security.policy test/java/rmi/activation/Activatable/restartLatecomer/security.policy test/java/rmi/activation/Activatable/restartService/security.policy test/java/rmi/activation/Activatable/shutdownGracefully/security.policy test/java/rmi/activation/Activatable/unregisterInactive/security.policy test/java/rmi/activation/ActivateFailedException/activateFails/security.policy test/java/rmi/activation/ActivationSystem/activeGroup/security.policy test/java/rmi/activation/ActivationSystem/modifyDescriptor/security.policy test/java/rmi/activation/ActivationSystem/stubClassesPermitted/StubClassesPermitted.java test/java/rmi/activation/ActivationSystem/stubClassesPermitted/security.policy test/java/rmi/activation/ActivationSystem/unregisterGroup/UnregisterGroup.java test/java/rmi/activation/ActivationSystem/unregisterGroup/security.policy test/java/rmi/activation/CommandEnvironment/SetChildEnv.java test/java/rmi/activation/CommandEnvironment/security.policy test/java/rmi/activation/rmidViaInheritedChannel/InheritedChannelNotServerSocket.java test/java/rmi/activation/rmidViaInheritedChannel/RmidViaInheritedChannel.java test/java/rmi/activation/rmidViaInheritedChannel/rmid.security.policy test/java/rmi/registry/altSecurityManager/AltSecurityManager.java test/java/rmi/registry/classPathCodebase/ClassPathCodebase.java test/java/rmi/registry/emptyName/EmptyName.java test/java/rmi/registry/interfaceHash/InterfaceHash.java test/java/rmi/registry/multipleRegistries/MultipleRegistries.java test/java/rmi/registry/readTest/readTest.java test/java/rmi/registry/readTest/readTest.sh test/java/rmi/registry/reexport/Reexport.java test/java/rmi/reliability/juicer/AppleUserImpl.java test/java/rmi/reliability/juicer/ApplicationServer.java test/java/rmi/server/RMISocketFactory/useSocketFactory/activatable/EchoImpl.java test/java/rmi/server/RMISocketFactory/useSocketFactory/activatable/UseCustomSocketFactory.java test/java/rmi/server/RMISocketFactory/useSocketFactory/activatable/security.policy test/java/rmi/server/RMISocketFactory/useSocketFactory/registry/HelloImpl.java test/java/rmi/server/RMISocketFactory/useSocketFactory/registry/UseCustomSocketFactory.java test/java/rmi/server/RMISocketFactory/useSocketFactory/registry/security.policy test/java/rmi/server/RMISocketFactory/useSocketFactory/unicast/EchoImpl.java test/java/rmi/server/RMISocketFactory/useSocketFactory/unicast/UseCustomSocketFactory.java test/java/rmi/server/RMISocketFactory/useSocketFactory/unicast/security.policy test/java/rmi/server/RemoteServer/AddrInUse.java test/java/rmi/server/UnicastRemoteObject/keepAliveDuringCall/KeepAliveDuringCall.java test/java/rmi/server/UnicastRemoteObject/keepAliveDuringCall/ShutdownImpl.java test/java/rmi/server/UnicastRemoteObject/unexportObject/UnexportLeak.java test/java/rmi/server/Unreferenced/finiteGCLatency/FiniteGCLatency.java test/java/rmi/server/Unreferenced/leaseCheckInterval/LeaseCheckInterval.java test/java/rmi/server/Unreferenced/leaseCheckInterval/SelfTerminator.java test/java/rmi/server/Unreferenced/unreferencedContext/UnreferencedContext.java test/java/rmi/server/useCustomRef/UseCustomRef.java test/java/rmi/server/useCustomRef/security.policy test/java/rmi/testlibrary/ActivationLibrary.java test/java/rmi/testlibrary/RMID.java test/java/rmi/testlibrary/RegistryRunner.java test/java/rmi/testlibrary/StreamPipe.java test/java/rmi/testlibrary/TestLibrary.java test/java/rmi/transport/checkFQDN/CheckFQDN.java test/java/rmi/transport/checkFQDN/CheckFQDNClient.java test/java/rmi/transport/checkLeaseInfoLeak/CheckLeaseLeak.java test/java/rmi/transport/checkLeaseInfoLeak/LeaseLeakClient.java test/java/rmi/transport/checkLeaseInfoLeak/security.policy test/java/rmi/transport/closeServerSocket/CloseServerSocket.java test/java/rmi/transport/dgcDeadLock/DGCDeadLock.java test/java/rmi/transport/dgcDeadLock/TestImpl.java test/java/rmi/transport/handshakeFailure/HandshakeFailure.java test/java/rmi/transport/handshakeTimeout/HandshakeTimeout.java test/java/rmi/transport/httpSocket/HttpSocketTest.java test/java/rmi/transport/httpSocket/security.policy test/java/rmi/transport/pinClientSocketFactory/PinClientSocketFactory.java test/java/rmi/transport/rapidExportUnexport/RapidExportUnexport.java test/java/rmi/transport/reuseDefaultPort/ReuseDefaultPort.java test/sun/rmi/rmic/newrmic/equivalence/AppleUserImpl.java test/sun/rmi/rmic/newrmic/equivalence/run.sh test/sun/rmi/runtime/Log/6409194/NoConsoleOutput.java test/sun/rmi/runtime/Log/checkLogging/CheckLogging.java test/sun/rmi/transport/proxy/EagerHttpFallback.java test/sun/rmi/transport/tcp/DeadCachedConnection.java
diffstat 92 files changed, 674 insertions(+), 398 deletions(-) [+]
line wrap: on
line diff
--- a/test/ProblemList.txt	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/ProblemList.txt	Wed Jun 12 11:48:21 2013 +0100
@@ -467,7 +467,6 @@
 java/rmi/activation/CommandEnvironment/SetChildEnv.java generic-all
 java/rmi/registry/classPathCodebase/ClassPathCodebase.java generic-all
 java/rmi/registry/reexport/Reexport.java generic-all
-java/rmi/server/Unreferenced/finiteGCLatency/FiniteGCLatency.java generic-all
 java/rmi/server/Unreferenced/leaseCheckInterval/LeaseCheckInterval.java generic-all
 java/rmi/server/Unreferenced/unreferencedContext/UnreferencedContext.java generic-all
 java/rmi/server/useCustomRef/UseCustomRef.java generic-all
--- a/test/com/sun/jndi/rmi/registry/RegistryContext/ContextWithNullProperties.java	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/com/sun/jndi/rmi/registry/RegistryContext/ContextWithNullProperties.java	Wed Jun 12 11:48:21 2013 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2010, 2012, 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
@@ -25,26 +25,22 @@
  * @test
  * @bug 6676075
  * @summary RegistryContext (com.sun.jndi.url.rmi.rmiURLContext) coding problem
+ * @library ../../../../../../java/rmi/testlibrary
+ * @build TestLibrary
+ * @run main ContextWithNullProperties
  */
 
+import com.sun.jndi.rmi.registry.RegistryContextsun.jndi.rmi.RegistryContext;
 import java.rmi.RemoteException;
-import java.rmi.registry.LocateRegistry;
-
-import com.sun.jndi.rmi.registry.*;
+import java.rmi.registry.Registry;
 
 public class ContextWithNullProperties {
-
     public static void main(String[] args) throws Exception {
-
-        // Create registry on port 1099 if one is not already running.
-        try {
-            LocateRegistry.createRegistry(1099);
-        } catch (RemoteException e) {
-        }
-
+        Registry registry = TestLibrary.createRegistryOnUnusedPort();
+        int registryPort = TestLibrary.getRegistryPort(registry);
         System.out.println("Connecting to the default Registry...");
         // Connect to the default Registry.
         // Pass null as the JNDI environment properties (see final argument)
-        RegistryContext ctx = new RegistryContext(null, -1, null);
+        RegistryContext ctx = new RegistryContext(null, registryPort, null);
     }
 }
--- a/test/com/sun/jndi/rmi/registry/RegistryContext/UnbindIdempotent.java	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/com/sun/jndi/rmi/registry/RegistryContext/UnbindIdempotent.java	Wed Jun 12 11:48:21 2013 +0100
@@ -1,26 +1,51 @@
+/*
+ * Copyright (c) 2007, 2012, 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.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
 /*
  * @test
  * @bug 4278121
  * @summary Ensure that calling unbind() on an unbound name returns
  *      successfully.
+ * @library ../../../../../../java/rmi/testlibrary
+ * @build TestLibrary
+ * @run main UnbindIdempotent
  */
 
-import javax.naming.*;
+import java.rmi.registry.Registry;
+import javax.naming.InitialContext;
+import javax.naming.NameNotFoundException;
+import javax.naming.NamingException;
 
 public class UnbindIdempotent {
 
     public static void main(String[] args) throws Exception {
+        Registry registry = TestLibrary.createRegistryOnUnusedPort();
+        int registryPort = TestLibrary.getRegistryPort(registry);
+        InitialContext ictx = new InitialContext();
+        Context rctx;
 
-        // Create registry on port 1099 if one is not already running.
         try {
-            java.rmi.registry.LocateRegistry.createRegistry(1099);
-        } catch (java.rmi.RemoteException e) {
-        }
-
-        Context ictx = new InitialContext();
-        Context rctx;
-        try {
-            rctx = (Context)ictx.lookup("rmi://localhost:1099");
+            rctx = (Context)ictx.lookup("rmi://localhost:" + Integer.toString(registryPort));
         } catch (NamingException e) {
             // Unable to set up for test.
             return;
--- a/test/java/rmi/Naming/LookupNameWithColon.java	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/Naming/LookupNameWithColon.java	Wed Jun 12 11:48:21 2013 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2001, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2001, 2012, 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
@@ -25,10 +25,13 @@
  * @bug 4387038
  * @summary Ensure that java.rmi.Naming.lookup functions properly for names
  *          containing embedded ':' characters.
+ *
+ * @library ../testlibrary
+ * @build TestLibrary
+ * @run main LookupNameWithColon
  */
 
 import java.rmi.Naming;
-import java.rmi.registry.LocateRegistry;
 import java.rmi.registry.Registry;
 
 public class LookupNameWithColon {
@@ -38,15 +41,12 @@
             "multiple:colons:in:name"
         };
 
-        Registry reg;
-        try {
-            reg = LocateRegistry.createRegistry(Registry.REGISTRY_PORT);
-        } catch (Exception ex) {
-            reg = LocateRegistry.getRegistry();
-        }
+        Registry reg = TestLibrary.createRegistryOnUnusedPort();
+        int port = TestLibrary.getRegistryPort(reg);
+
         for (int i = 0; i < names.length; i++) {
             reg.rebind(names[i], reg);
-            Naming.lookup("rmi://localhost/" + names[i]);
+            Naming.lookup("rmi://localhost:" + port + "/" + names[i]);
         }
     }
 }
--- a/test/java/rmi/Naming/RmiIsNoScheme.java	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/Naming/RmiIsNoScheme.java	Wed Jun 12 11:48:21 2013 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2002, 2012, 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
@@ -39,9 +39,6 @@
 import java.rmi.registry.*;
 
 public class RmiIsNoScheme implements Remote, Serializable {
-
-    private static final int REGISTRY_PORT = 2002;
-
     private RmiIsNoScheme() {}
 
     public static void main(String[] args) {
@@ -49,10 +46,11 @@
         System.err.println("\nRegression test for bug 4626311\n");
 
         try {
-            LocateRegistry.createRegistry(REGISTRY_PORT);
-            Naming.rebind("//:" + REGISTRY_PORT + "/RmiIsNoScheme",
+            Registry registry = TestLibrary.createRegistryOnUnusedPort();
+            int registryPort = TestLibrary.getRegistryPort(registry);
+            Naming.rebind("//:" + registryPort + "/RmiIsNoScheme",
                           new RmiIsNoScheme());
-            String name = Naming.list("//:" + REGISTRY_PORT)[0];
+            String name = Naming.list("//:" + registryPort)[0];
             System.err.println("name = " + name);
             if (name.startsWith("rmi:", 0) == false) {
                 System.err.println("TEST PASSED: rmi scheme not present");
--- a/test/java/rmi/Naming/UnderscoreHost.java	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/Naming/UnderscoreHost.java	Wed Jun 12 11:48:21 2013 +0100
@@ -1,37 +1,37 @@
-/*
- * Copyright (c) 2005, 2006, 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.
- *
- * This code is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
- * version 2 for more details (a copy is included in the LICENSE file that
- * accompanied this code).
- *
- * You should have received a copy of the GNU General Public License version
- * 2 along with this work; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
- * or visit www.oracle.com if you need additional information or have any
- * questions.
- */
+ /*
+  * Copyright (c) 2005, 2012, 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.
+  *
+  * This code is distributed in the hope that it will be useful, but WITHOUT
+  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+  * version 2 for more details (a copy is included in the LICENSE file that
+  * accompanied this code).
+  *
+  * You should have received a copy of the GNU General Public License version
+  * 2 along with this work; if not, write to the Free Software Foundation,
+  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+  *
+  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+  * or visit www.oracle.com if you need additional information or have any
+  * questions.
+  */
 
-/*
- * @test
- * @bug 5083594
- * @summary Ensure that Naming.java correctly parses host names with '_' in
- * them.
- * @author Vinod Johnson
- *
- * @library ../testlibrary
- * @build TestLibrary
- * @build UnderscoreHost UnderscoreHost_Stub
- * @run main/othervm UnderscoreHost
+ /*
+  * @test
+  * @bug 5083594
+  * @summary Ensure that Naming.java correctly parses host names with '_' in
+  * them.
+  * @author Vinod Johnson
+  *
+  * @library ../testlibrary
+  * @build TestLibrary
+  * @build UnderscoreHost UnderscoreHost_Stub
+  * @run main/othervm UnderscoreHost
  */
 
 import java.io.IOException;
@@ -77,11 +77,12 @@
         try {
             HostVerifyingSocketFactory hvf = new HostVerifyingSocketFactory();
             RMISocketFactory.setSocketFactory(hvf);
-            Registry r = LocateRegistry.createRegistry(Registry.REGISTRY_PORT);
+            Registry r = TestLibrary.createRegistryOnUnusedPort();
+            int port = TestLibrary.getRegistryPort(r);
             t = new UnderscoreHost();
             r.rebind(NAME, t);
             Naming.lookup("rmi://" + HOSTNAME +
-                          ":" + Registry.REGISTRY_PORT + "/" + NAME);
+                          ":" + port + "/" + NAME);
             /*
              * This test is coded to pass whether java.net.URI obeys
              * RFC 2396 or RFC 3986 (see 5085902, 6394131, etc.).
--- a/test/java/rmi/Naming/legalRegistryNames/LegalRegistryNames.java	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/Naming/legalRegistryNames/LegalRegistryNames.java	Wed Jun 12 11:48:21 2013 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1999, 2004, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1999, 2012, 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
@@ -45,7 +45,9 @@
 
 /**
  * Ensure that all legal forms of Naming URLs operate with the
- * java.rmi.Naming interface
+ * java.rmi.Naming interface.  This test requires using the default RMI Registry
+ * port as it tests all of the RMI naming URL's, including the ones which do not
+ * take a port (and therefore uses the default port).
  */
 public class LegalRegistryNames extends UnicastRemoteObject
     implements Legal
--- a/test/java/rmi/activation/Activatable/checkActivateRef/security.policy	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/activation/Activatable/checkActivateRef/security.policy	Wed Jun 12 11:48:21 2013 +0100
@@ -29,11 +29,12 @@
   permission java.util.PropertyPermission "java.security.policy", "read";
   permission java.util.PropertyPermission "java.security.manager", "read";
 
-  // used by TestLibrary to determine test environment 
+  // used by TestLibrary to determine test environment
   permission java.util.PropertyPermission "test.classes", "read";
   permission java.util.PropertyPermission "test.src", "read";
   permission java.util.PropertyPermission "user.dir", "read";
   permission java.util.PropertyPermission "java.home", "read";
+  permission java.util.PropertyPermission "java.rmi.activation.port", "write";
 
   // Needed to create an activation group
   permission java.lang.RuntimePermission "setFactory";
--- a/test/java/rmi/activation/Activatable/checkAnnotations/security.policy	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/activation/Activatable/checkAnnotations/security.policy	Wed Jun 12 11:48:21 2013 +0100
@@ -21,11 +21,12 @@
   permission java.util.PropertyPermission "java.security.policy", "read";
   permission java.util.PropertyPermission "java.security.manager", "read";
 
-  // used by TestLibrary to determine test environment 
+  // used by TestLibrary to determine test environment
   permission java.util.PropertyPermission "test.classes", "read";
   permission java.util.PropertyPermission "test.src", "read";
   permission java.util.PropertyPermission "user.dir", "read";
   permission java.util.PropertyPermission "java.home", "read";
+  permission java.util.PropertyPermission "java.rmi.activation.port", "write";
 
   // required for test to create an ActivationGroup
   permission java.lang.RuntimePermission "setFactory";
--- a/test/java/rmi/activation/Activatable/checkImplClassLoader/security.policy	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/activation/Activatable/checkImplClassLoader/security.policy	Wed Jun 12 11:48:21 2013 +0100
@@ -32,11 +32,12 @@
   permission java.util.PropertyPermission "java.security.policy", "read";
   permission java.util.PropertyPermission "java.security.manager", "read";
 
-  // used by TestLibrary to determine test environment 
+  // used by TestLibrary to determine test environment
   permission java.util.PropertyPermission "test.classes", "read";
   permission java.util.PropertyPermission "test.src", "read";
   permission java.util.PropertyPermission "user.dir", "read";
   permission java.util.PropertyPermission "java.home", "read";
+  permission java.util.PropertyPermission "java.rmi.activation.port", "write";
 
   // required for test to create an ActivationGroup
   permission java.lang.RuntimePermission "setFactory";
--- a/test/java/rmi/activation/Activatable/checkRegisterInLog/security.policy	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/activation/Activatable/checkRegisterInLog/security.policy	Wed Jun 12 11:48:21 2013 +0100
@@ -21,17 +21,18 @@
   permission java.util.PropertyPermission "java.security.policy", "read";
   permission java.util.PropertyPermission "java.security.manager", "read";
 
-  // used by TestLibrary to determine test environment 
+  // used by TestLibrary to determine test environment
   permission java.util.PropertyPermission "test.classes", "read";
   permission java.util.PropertyPermission "test.src", "read";
   permission java.util.PropertyPermission "user.dir", "read";
   permission java.util.PropertyPermission "java.home", "read";
+  permission java.util.PropertyPermission "java.rmi.activation.port", "write";
 
   // Needed to create an activation group
   permission java.lang.RuntimePermission "setFactory";
 
   // allow exporting of remote objects on an arbitrary port.
-  permission java.net.SocketPermission "*:1024-", "connect,accept";
+  permission java.net.SocketPermission "*:1024-", "connect,accept,listen";
 
   // allow exporting object with non-public remote interface
   permission java.rmi.RMIPermission "exportRemoteInterface.ActivateMe";
--- a/test/java/rmi/activation/Activatable/createPrivateActivable/security.policy	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/activation/Activatable/createPrivateActivable/security.policy	Wed Jun 12 11:48:21 2013 +0100
@@ -21,11 +21,12 @@
   permission java.util.PropertyPermission "java.security.policy", "read";
   permission java.util.PropertyPermission "java.security.manager", "read";
 
-  // used by TestLibrary to determine test environment 
+  // used by TestLibrary to determine test environment
   permission java.util.PropertyPermission "test.classes", "read";
   permission java.util.PropertyPermission "test.src", "read";
   permission java.util.PropertyPermission "user.dir", "read";
   permission java.util.PropertyPermission "java.home", "read";
+  permission java.util.PropertyPermission "java.rmi.activation.port", "write";
 
   // Needed to create an activation group
   permission java.lang.RuntimePermission "setFactory";
--- a/test/java/rmi/activation/Activatable/downloadParameterClass/security.policy	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/activation/Activatable/downloadParameterClass/security.policy	Wed Jun 12 11:48:21 2013 +0100
@@ -31,11 +31,12 @@
   permission java.util.PropertyPermission "java.security.policy", "read";
   permission java.util.PropertyPermission "java.security.manager", "read";
 
-  // used by TestLibrary to determine test environment 
+  // used by TestLibrary to determine test environment
   permission java.util.PropertyPermission "test.classes", "read";
   permission java.util.PropertyPermission "test.src", "read";
   permission java.util.PropertyPermission "user.dir", "read";
   permission java.util.PropertyPermission "java.home", "read";
+  permission java.util.PropertyPermission "java.rmi.activation.port", "write";
 
   // allow exporting of remote objects on an arbitrary port.
   permission java.net.SocketPermission "*:1024-", "connect,accept,listen";
--- a/test/java/rmi/activation/Activatable/elucidateNoSuchMethod/security.policy	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/activation/Activatable/elucidateNoSuchMethod/security.policy	Wed Jun 12 11:48:21 2013 +0100
@@ -30,11 +30,12 @@
   permission java.util.PropertyPermission "java.security.policy", "read";
   permission java.util.PropertyPermission "java.security.manager", "read";
 
-  // used by TestLibrary to determine test environment 
+  // used by TestLibrary to determine test environment
   permission java.util.PropertyPermission "test.classes", "read";
   permission java.util.PropertyPermission "test.src", "read";
   permission java.util.PropertyPermission "user.dir", "read";
   permission java.util.PropertyPermission "java.home", "read";
+  permission java.util.PropertyPermission "java.rmi.activation.port", "write";
 
   // Needed to create an activation group
   permission java.lang.RuntimePermission "setFactory";
--- a/test/java/rmi/activation/Activatable/extLoadedImpl/security.policy	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/activation/Activatable/extLoadedImpl/security.policy	Wed Jun 12 11:48:21 2013 +0100
@@ -17,11 +17,12 @@
   permission java.util.PropertyPermission "java.security.policy", "read";
   permission java.util.PropertyPermission "java.security.manager", "read";
 
-  // used by TestLibrary to determine test environment 
+  // used by TestLibrary to determine test environment
   permission java.util.PropertyPermission "test.classes", "read";
   permission java.util.PropertyPermission "test.src", "read";
   permission java.util.PropertyPermission "user.dir", "read";
   permission java.util.PropertyPermission "java.home", "read";
+  permission java.util.PropertyPermission "java.rmi.activation.port", "write";
 
   // allow exporting of remote objects on an arbitrary port.
   permission java.net.SocketPermission "*:1024-", "connect,accept,listen";
--- a/test/java/rmi/activation/Activatable/forceLogSnapshot/security.policy	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/activation/Activatable/forceLogSnapshot/security.policy	Wed Jun 12 11:48:21 2013 +0100
@@ -21,11 +21,12 @@
   permission java.util.PropertyPermission "java.security.policy", "read";
   permission java.util.PropertyPermission "java.security.manager", "read";
 
-  // used by TestLibrary to determine test environment 
+  // used by TestLibrary to determine test environment
   permission java.util.PropertyPermission "test.classes", "read";
   permission java.util.PropertyPermission "test.src", "read";
   permission java.util.PropertyPermission "user.dir", "read";
   permission java.util.PropertyPermission "java.home", "read";
+  permission java.util.PropertyPermission "java.rmi.activation.port", "write";
 
   // Needed to create an activation group
   permission java.lang.RuntimePermission "setFactory";
--- a/test/java/rmi/activation/Activatable/inactiveGroup/security.policy	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/activation/Activatable/inactiveGroup/security.policy	Wed Jun 12 11:48:21 2013 +0100
@@ -21,17 +21,18 @@
   permission java.util.PropertyPermission "java.security.policy", "read";
   permission java.util.PropertyPermission "java.security.manager", "read";
 
-  // used by TestLibrary to determine test environment 
+  // used by TestLibrary to determine test environment
   permission java.util.PropertyPermission "test.classes", "read";
   permission java.util.PropertyPermission "test.src", "read";
   permission java.util.PropertyPermission "user.dir", "read";
   permission java.util.PropertyPermission "java.home", "read";
+  permission java.util.PropertyPermission "java.rmi.activation.port", "write";
 
   // Needed to create an activation group
   permission java.lang.RuntimePermission "setFactory";
 
   // allow exporting of remote objects on an arbitrary port.
-  permission java.net.SocketPermission "*:1024-", "connect,accept";
+  permission java.net.SocketPermission "*:1024-", "connect,accept,listen";
 
   // allow exporting object with non-public remote interface
   permission java.rmi.RMIPermission "exportRemoteInterface.ActivateMe";
--- a/test/java/rmi/activation/Activatable/lookupActivationSystem/LookupActivationSystem.java	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/activation/Activatable/lookupActivationSystem/LookupActivationSystem.java	Wed Jun 12 11:48:21 2013 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2005, 2012, 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
@@ -58,7 +58,7 @@
 
             System.err.println("look up activation system");
             Registry rmidRegistry =
-                LocateRegistry.getRegistry(ActivationSystem.SYSTEM_PORT);
+                LocateRegistry.getRegistry(rmid.getPort());
             ActivationSystem system = (ActivationSystem)
                 rmidRegistry.lookup(NAME);
 
--- a/test/java/rmi/activation/Activatable/nestedActivate/security.policy	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/activation/Activatable/nestedActivate/security.policy	Wed Jun 12 11:48:21 2013 +0100
@@ -21,17 +21,18 @@
   permission java.util.PropertyPermission "java.security.policy", "read";
   permission java.util.PropertyPermission "java.security.manager", "read";
 
-  // used by TestLibrary to determine test environment 
+  // used by TestLibrary to determine test environment
   permission java.util.PropertyPermission "test.classes", "read";
   permission java.util.PropertyPermission "test.src", "read";
   permission java.util.PropertyPermission "user.dir", "read";
   permission java.util.PropertyPermission "java.home", "read";
+  permission java.util.PropertyPermission "java.rmi.activation.port", "write";
 
   // Needed to create an activation group
   permission java.lang.RuntimePermission "setFactory";
 
   // allow exporting of remote objects on an arbitrary port.
-  permission java.net.SocketPermission "*:1024-", "connect,accept";
+  permission java.net.SocketPermission "*:1024-", "connect,accept,listen";
 
   // allow exporting of non-public remote interface
   permission java.rmi.RMIPermission "exportRemoteInterface.ActivateMe";
--- a/test/java/rmi/activation/Activatable/nonExistentActivatable/security.policy	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/activation/Activatable/nonExistentActivatable/security.policy	Wed Jun 12 11:48:21 2013 +0100
@@ -21,11 +21,12 @@
   permission java.util.PropertyPermission "java.security.policy", "read";
   permission java.util.PropertyPermission "java.security.manager", "read";
 
-  // used by TestLibrary to determine test environment 
+  // used by TestLibrary to determine test environment
   permission java.util.PropertyPermission "test.classes", "read";
   permission java.util.PropertyPermission "test.src", "read";
   permission java.util.PropertyPermission "user.dir", "read";
   permission java.util.PropertyPermission "java.home", "read";
+  permission java.util.PropertyPermission "java.rmi.activation.port", "write";
 
   // Needed to create an activation group
   permission java.lang.RuntimePermission "setFactory";
--- a/test/java/rmi/activation/Activatable/restartCrashedService/security.policy	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/activation/Activatable/restartCrashedService/security.policy	Wed Jun 12 11:48:21 2013 +0100
@@ -21,11 +21,12 @@
   permission java.util.PropertyPermission "java.security.policy", "read";
   permission java.util.PropertyPermission "java.security.manager", "read";
 
-  // used by TestLibrary to determine test environment 
+  // used by TestLibrary to determine test environment
   permission java.util.PropertyPermission "test.classes", "read";
   permission java.util.PropertyPermission "test.src", "read";
   permission java.util.PropertyPermission "user.dir", "read";
   permission java.util.PropertyPermission "java.home", "read";
+  permission java.util.PropertyPermission "java.rmi.activation.port", "write";
 
   // Needed to create an activation group
   permission java.lang.RuntimePermission "setFactory";
--- a/test/java/rmi/activation/Activatable/restartLatecomer/security.policy	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/activation/Activatable/restartLatecomer/security.policy	Wed Jun 12 11:48:21 2013 +0100
@@ -21,11 +21,12 @@
   permission java.util.PropertyPermission "java.security.policy", "read";
   permission java.util.PropertyPermission "java.security.manager", "read";
 
-  // used by TestLibrary to determine test environment 
+  // used by TestLibrary to determine test environment
   permission java.util.PropertyPermission "test.classes", "read";
   permission java.util.PropertyPermission "test.src", "read";
   permission java.util.PropertyPermission "user.dir", "read";
   permission java.util.PropertyPermission "java.home", "read";
+  permission java.util.PropertyPermission "java.rmi.activation.port", "write";
 
   // Used in remote impl of Activator.inactive; by the method
   // "restartThread.dispose()"when it calls thread.interrupt()
--- a/test/java/rmi/activation/Activatable/restartService/security.policy	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/activation/Activatable/restartService/security.policy	Wed Jun 12 11:48:21 2013 +0100
@@ -21,11 +21,12 @@
   permission java.util.PropertyPermission "java.security.policy", "read";
   permission java.util.PropertyPermission "java.security.manager", "read";
 
-  // used by TestLibrary to determine test environment 
+  // used by TestLibrary to determine test environment
   permission java.util.PropertyPermission "test.classes", "read";
   permission java.util.PropertyPermission "test.src", "read";
   permission java.util.PropertyPermission "user.dir", "read";
   permission java.util.PropertyPermission "java.home", "read";
+  permission java.util.PropertyPermission "java.rmi.activation.port", "write";
 
   // Used in remote impl of Activator.inactive; by the method
   // "restartThread.dispose()"when it calls thread.interrupt()
--- a/test/java/rmi/activation/Activatable/shutdownGracefully/security.policy	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/activation/Activatable/shutdownGracefully/security.policy	Wed Jun 12 11:48:21 2013 +0100
@@ -21,11 +21,12 @@
   permission java.util.PropertyPermission "java.security.policy", "read";
   permission java.util.PropertyPermission "java.security.manager", "read";
 
-  // used by TestLibrary to determine test environment 
+  // used by TestLibrary to determine test environment
   permission java.util.PropertyPermission "test.classes", "read";
   permission java.util.PropertyPermission "test.src", "read";
   permission java.util.PropertyPermission "user.dir", "read";
   permission java.util.PropertyPermission "java.home", "read";
+  permission java.util.PropertyPermission "java.rmi.activation.port", "write";
 
   // allow exporting of remote objects on an arbitrary port.
   permission java.net.SocketPermission "*:1024-", "connect,accept,listen";
--- a/test/java/rmi/activation/Activatable/unregisterInactive/security.policy	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/activation/Activatable/unregisterInactive/security.policy	Wed Jun 12 11:48:21 2013 +0100
@@ -21,11 +21,12 @@
   permission java.util.PropertyPermission "java.security.policy", "read";
   permission java.util.PropertyPermission "java.security.manager", "read";
 
-  // used by TestLibrary to determine test environment 
+  // used by TestLibrary to determine test environment
   permission java.util.PropertyPermission "test.classes", "read";
   permission java.util.PropertyPermission "test.src", "read";
   permission java.util.PropertyPermission "user.dir", "read";
   permission java.util.PropertyPermission "java.home", "read";
+  permission java.util.PropertyPermission "java.rmi.activation.port", "write";
 
   // Needed to create an activation group
   permission java.lang.RuntimePermission "setFactory";
--- a/test/java/rmi/activation/ActivateFailedException/activateFails/security.policy	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/activation/ActivateFailedException/activateFails/security.policy	Wed Jun 12 11:48:21 2013 +0100
@@ -21,11 +21,12 @@
   permission java.util.PropertyPermission "java.security.policy", "read";
   permission java.util.PropertyPermission "java.security.manager", "read";
 
-  // used by TestLibrary to determine test environment 
+  // used by TestLibrary to determine test environment
   permission java.util.PropertyPermission "test.classes", "read";
   permission java.util.PropertyPermission "test.src", "read";
   permission java.util.PropertyPermission "user.dir", "read";
   permission java.util.PropertyPermission "java.home", "read";
+  permission java.util.PropertyPermission "java.rmi.activation.port", "write";
 
   // required for test to create an ActivationGroup
   permission java.lang.RuntimePermission "setFactory";
--- a/test/java/rmi/activation/ActivationSystem/activeGroup/security.policy	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/activation/ActivationSystem/activeGroup/security.policy	Wed Jun 12 11:48:21 2013 +0100
@@ -15,11 +15,12 @@
   permission java.util.PropertyPermission "java.security.policy", "read";
   permission java.util.PropertyPermission "java.security.manager", "read";
 
-  // used by TestLibrary to determine test environment 
+  // used by TestLibrary to determine test environment
   permission java.util.PropertyPermission "test.classes", "read";
   permission java.util.PropertyPermission "test.src", "read";
   permission java.util.PropertyPermission "user.dir", "read";
   permission java.util.PropertyPermission "java.home", "read";
+  permission java.util.PropertyPermission "java.rmi.activation.port", "write";
 
   // test needs to export rmid and communicate with objects on arbitrary ports
   permission java.net.SocketPermission "*:1024-", "connect,accept,listen";
--- a/test/java/rmi/activation/ActivationSystem/modifyDescriptor/security.policy	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/activation/ActivationSystem/modifyDescriptor/security.policy	Wed Jun 12 11:48:21 2013 +0100
@@ -21,11 +21,12 @@
   permission java.util.PropertyPermission "java.security.policy", "read";
   permission java.util.PropertyPermission "java.security.manager", "read";
 
-  // used by TestLibrary to determine test environment 
+  // used by TestLibrary to determine test environment
   permission java.util.PropertyPermission "test.classes", "read";
   permission java.util.PropertyPermission "test.src", "read";
   permission java.util.PropertyPermission "user.dir", "read";
   permission java.util.PropertyPermission "java.home", "read";
+  permission java.util.PropertyPermission "java.rmi.activation.port", "write";
 
   // required for test to create an ActivationGroup
   permission java.lang.RuntimePermission "setFactory";
--- a/test/java/rmi/activation/ActivationSystem/stubClassesPermitted/StubClassesPermitted.java	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/activation/ActivationSystem/stubClassesPermitted/StubClassesPermitted.java	Wed Jun 12 11:48:21 2013 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1999, 2008, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1999, 2012, 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
@@ -61,7 +61,7 @@
     extends Activatable implements Runnable, CanCreateStubs
 {
     public static boolean sameGroup = false;
-
+    private static int registryPort = -1;
     private static CanCreateStubs canCreateStubs = null;
     private static Registry registry = null;
 
@@ -76,8 +76,8 @@
         try {
             TestLibrary.suggestSecurityManager("java.lang.SecurityManager");
 
-            registry = java.rmi.registry.LocateRegistry.
-                createRegistry(TestLibrary.REGISTRY_PORT);
+            registry = TestLibrary.createRegistryOnUnusedPort();
+            registryPort = TestLibrary.getRegistryPort(registry);
 
             // must run with java.lang.SecurityManager or the test
             // result will be nullified if running with a build where
@@ -192,7 +192,7 @@
 
         // obtain reference to the test registry
         registry = java.rmi.registry.LocateRegistry.
-            getRegistry(TestLibrary.REGISTRY_PORT);
+            getRegistry(registryPort);
     }
 
     /**
--- a/test/java/rmi/activation/ActivationSystem/stubClassesPermitted/security.policy	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/activation/ActivationSystem/stubClassesPermitted/security.policy	Wed Jun 12 11:48:21 2013 +0100
@@ -21,15 +21,22 @@
   permission java.util.PropertyPermission "java.security.policy", "read";
   permission java.util.PropertyPermission "java.security.manager", "read";
 
-  // used by TestLibrary to determine test environment 
+  // used by TestLibrary to determine test environment
   permission java.util.PropertyPermission "test.classes", "read";
   permission java.util.PropertyPermission "test.src", "read";
   permission java.util.PropertyPermission "user.dir", "read";
   permission java.util.PropertyPermission "java.home", "read";
+  permission java.util.PropertyPermission "java.rmi.activation.port", "write";
 
   // required for test to create an ActivationGroup
   permission java.lang.RuntimePermission "setFactory";
 
+  // required for test to get the registry port
+  permission java.lang.RuntimePermission "accessClassInPackage.sun.rmi.registry";
+  permission java.lang.RuntimePermission "accessClassInPackage.sun.rmi.server";
+  permission java.lang.RuntimePermission "accessClassInPackage.sun.rmi.transport";
+  permission java.lang.RuntimePermission "accessClassInPackage.sun.rmi.transport.tcp";
+
   // test needs to export rmid and communicate with objects on arbitrary ports
   permission java.net.SocketPermission "*:1024-", "connect,accept,listen";
 };
--- a/test/java/rmi/activation/ActivationSystem/unregisterGroup/UnregisterGroup.java	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/activation/ActivationSystem/unregisterGroup/UnregisterGroup.java	Wed Jun 12 11:48:21 2013 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1998, 2008, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1998, 2012, 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
@@ -74,7 +74,7 @@
     private static boolean done = false;
     private static ActivateMe lastResortExitObj = null;
     private static final int NUM_OBJECTS = 10;
-    private static int PORT = 2006;
+    private static int registryPort = -1;
 
     public UnregisterGroup(ActivationID id, MarshalledObject mobj)
         throws Exception
@@ -116,7 +116,7 @@
 
         try {
             CallbackInterface cobj =
-                (CallbackInterface)Naming.lookup("//:" + PORT + "/Callback");
+                (CallbackInterface)Naming.lookup("//:" + registryPort + "/Callback");
             cobj.inc();
         } catch (Exception e) {
             System.err.println("cobj.inc exception");
@@ -235,7 +235,8 @@
             try {
 
                 // create reg and export callback object
-                registry = LocateRegistry.createRegistry(PORT);
+                registry = TestLibrary.createRegistryOnUnusedPort();
+                registryPort = TestLibrary.getRegistryPort(registry);
                 Callback robj = new Callback();
                 registry.bind("Callback", robj);
 
--- a/test/java/rmi/activation/ActivationSystem/unregisterGroup/security.policy	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/activation/ActivationSystem/unregisterGroup/security.policy	Wed Jun 12 11:48:21 2013 +0100
@@ -21,11 +21,12 @@
   permission java.util.PropertyPermission "java.security.policy", "read";
   permission java.util.PropertyPermission "java.security.manager", "read";
 
-  // used by TestLibrary to determine test environment 
+  // used by TestLibrary to determine test environment
   permission java.util.PropertyPermission "test.classes", "read";
   permission java.util.PropertyPermission "test.src", "read";
   permission java.util.PropertyPermission "user.dir", "read";
   permission java.util.PropertyPermission "java.home", "read";
+  permission java.util.PropertyPermission "java.rmi.activation.port", "write";
 
   // required for test to create an ActivationGroup
   permission java.lang.RuntimePermission "setFactory";
--- a/test/java/rmi/activation/CommandEnvironment/SetChildEnv.java	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/activation/CommandEnvironment/SetChildEnv.java	Wed Jun 12 11:48:21 2013 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1998, 2000, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1998, 2012, 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
@@ -53,31 +53,37 @@
     public static void main(String argv[])
         throws Exception
     {
+        int runningPort = TestLibrary.getUnusedRandomPort();
+
         System.out.println("java.compiler=" + System.getProperty("java.compiler"));
         // don't embed spaces in any of the test args/props, because
         // they won't be parsed properly
-        runwith (new String[0], new String[0]);
+        runwith (new String[0], new String[0], runningPort);
 
         runwith (
             new String[] { "-verbosegc" },
             new String[] { "foo.bar=SetChildEnvTest",
-                           "sun.rmi.server.doSomething=true" }
+                           "sun.rmi.server.doSomething=true" },
+            runningPort
             );
 
         runwith (
             new String[] { },
-            new String[] { "parameter.count=zero" }
+            new String[] { "parameter.count=zero" },
+            runningPort
             );
 
         runwith (
             new String[] { "-Xmx32m" },
-            new String[] { }
+            new String[] { },
+            runningPort
             );
     }
 
     private static void runwith(
         String[] params,        // extra args
-        String[] props          // extra system properties
+        String[] props,         // extra system properties
+        int port                // port on which to communicate
     )
         throws Exception
     {
@@ -89,7 +95,8 @@
 
         RMID.removeLog();
         RMID rmid = RMID.createRMID(watcher.otherEnd(), watcher.otherEnd(),
-                                    true); // debugExec turned on
+                                    true,  // debugExec turned on
+                                    true, port);
 
         rmid.start();
 
@@ -195,7 +202,7 @@
         actsys.unregisterGroup(gid);
 
         Thread.sleep(5000);
-        rmid.destroy();
+        ActivationLibrary.rmidCleanup(rmid);
     }
 
     public static class DebugExecWatcher
@@ -243,7 +250,19 @@
                     System.err.println(line);
                 }
             } catch (IOException e) {
-                e.printStackTrace();
+                /* During termination of distant rmid, StreamPipes will be broken when
+                 * distant vm terminates. A "Pipe broken" exception is expected because
+                 * DebugExecWatcher points to the same streams as StreamPipes used by RMID.
+                 * If we get this exception. We just terminate the thread.
+                 */
+                if (e.getMessage().equals("Pipe broken")) {
+                    try {
+                        str.close();
+                    } catch (IOException ioe) {}
+                }
+                else {
+                    e.printStackTrace();
+                }
             }
         }
     }
--- a/test/java/rmi/activation/CommandEnvironment/security.policy	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/activation/CommandEnvironment/security.policy	Wed Jun 12 11:48:21 2013 +0100
@@ -27,6 +27,7 @@
   permission java.util.PropertyPermission "test.src", "read";
   permission java.util.PropertyPermission "user.dir", "read";
   permission java.util.PropertyPermission "java.home", "read";
+  permission java.util.PropertyPermission "java.rmi.activation.port", "write";
 
   // required for test to create an ActivationGroup
   permission java.lang.RuntimePermission "setFactory";
--- a/test/java/rmi/activation/rmidViaInheritedChannel/InheritedChannelNotServerSocket.java	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/activation/rmidViaInheritedChannel/InheritedChannelNotServerSocket.java	Wed Jun 12 11:48:21 2013 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2005, 2012, 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
@@ -31,8 +31,7 @@
  * @library ../../testlibrary
  * @build RMID ActivationLibrary
  * @build InheritedChannelNotServerSocket
- * @run main/othervm/timeout=240 -Djava.rmi.activation.port=5398
- *     InheritedChannelNotServerSocket
+ * @run main/othervm/timeout=240 InheritedChannelNotServerSocket
  */
 
 import java.io.IOException;
@@ -56,8 +55,6 @@
 import java.rmi.server.UnicastRemoteObject;
 
 public class InheritedChannelNotServerSocket {
-
-    private static final int PORT = 5398;
     private static final Object lock = new Object();
     private static boolean notified = false;
 
@@ -80,7 +77,8 @@
 
     public static void main(String[] args) throws Exception {
         System.err.println("\nRegression test for bug 6261402\n");
-
+        System.setProperty("java.rmi.activation.port",
+                           Integer.toString(TestLibrary.INHERITEDCHANNELNOTSERVERSOCKET_ACTIVATION_PORT));
         RMID rmid = null;
         Callback obj = null;
         try {
@@ -92,7 +90,8 @@
             Callback proxy =
                 (Callback) UnicastRemoteObject.exportObject(obj, 0);
             Registry registry =
-                LocateRegistry.createRegistry(TestLibrary.REGISTRY_PORT);
+                LocateRegistry.createRegistry(
+                    TestLibrary.INHERITEDCHANNELNOTSERVERSOCKET_REGISTRY_PORT);
             registry.bind("Callback", proxy);
 
             /*
@@ -100,7 +99,8 @@
              */
             System.err.println("start rmid with inherited channel");
             RMID.removeLog();
-            rmid = RMID.createRMID(System.out, System.err, true, true, PORT);
+            rmid = RMID.createRMID(System.out, System.err, true, true,
+                                   TestLibrary.INHERITEDCHANNELNOTSERVERSOCKET_ACTIVATION_PORT);
             rmid.addOptions(new String[]{
                 "-Djava.nio.channels.spi.SelectorProvider=" +
                 "InheritedChannelNotServerSocket$SP"});
@@ -123,7 +123,7 @@
             if (obj != null) {
                 UnicastRemoteObject.unexportObject(obj, true);
             }
-            ActivationLibrary.rmidCleanup(rmid, PORT);
+            ActivationLibrary.rmidCleanup(rmid);
         }
     }
 
@@ -174,7 +174,7 @@
                 try {
                     System.err.println("notify test...");
                     Registry registry =
-                        LocateRegistry.getRegistry(TestLibrary.REGISTRY_PORT);
+                        LocateRegistry.getRegistry(TestLibrary.INHERITEDCHANNELNOTSERVERSOCKET_REGISTRY_PORT);
                     Callback obj = (Callback) registry.lookup("Callback");
                     obj.notifyTest();
                 } catch (NotBoundException nbe) {
--- a/test/java/rmi/activation/rmidViaInheritedChannel/RmidViaInheritedChannel.java	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/activation/rmidViaInheritedChannel/RmidViaInheritedChannel.java	Wed Jun 12 11:48:21 2013 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2003, 2012, 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
@@ -29,7 +29,8 @@
  * @library ../../testlibrary
  * @build RMID ActivationLibrary
  * @build RmidViaInheritedChannel
- * @run main/othervm/timeout=240 -Djava.rmi.activation.port=5398 RmidViaInheritedChannel
+ * @build TestLibrary
+ * @run main/othervm/timeout=240 RmidViaInheritedChannel
  */
 
 import java.io.IOException;
@@ -49,8 +50,6 @@
 import java.rmi.server.UnicastRemoteObject;
 
 public class RmidViaInheritedChannel implements Callback {
-
-    private static final int PORT = 5398;
     private static final Object lock = new Object();
     private static boolean notified = false;
 
@@ -65,7 +64,8 @@
     }
 
     public static void main(String[] args) throws Exception {
-
+        System.setProperty("java.rmi.activation.port",
+                           Integer.toString(TestLibrary.RMIDVIAINHERITEDCHANNEL_ACTIVATION_PORT));
         RMID rmid = null;
         Callback obj = null;
 
@@ -78,7 +78,8 @@
             Callback proxy = (Callback)
                 UnicastRemoteObject.exportObject(obj, 0);
             Registry registry =
-                LocateRegistry.createRegistry(TestLibrary.REGISTRY_PORT);
+                LocateRegistry.createRegistry(
+                    TestLibrary.RMIDVIAINHERITEDCHANNEL_REGISTRY_PORT);
             registry.bind("Callback", proxy);
 
             /*
@@ -86,7 +87,8 @@
              */
             System.err.println("start rmid with inherited channel");
             RMID.removeLog();
-            rmid = RMID.createRMID(System.out, System.err, true, false, PORT);
+            rmid = RMID.createRMID(System.out, System.err, true, false,
+                                   TestLibrary.RMIDVIAINHERITEDCHANNEL_ACTIVATION_PORT);
             rmid.addOptions(new String[]{
                 "-Djava.nio.channels.spi.SelectorProvider=RmidViaInheritedChannel$RmidSelectorProvider"});
             rmid.start();
@@ -109,7 +111,7 @@
             if (obj != null) {
                 UnicastRemoteObject.unexportObject(obj, true);
             }
-            ActivationLibrary.rmidCleanup(rmid, PORT);
+            ActivationLibrary.rmidCleanup(rmid);
         }
     }
 
@@ -165,7 +167,8 @@
                 channel = ServerSocketChannel.open();
                 ServerSocket serverSocket = channel.socket();
                 serverSocket.bind(
-                     new InetSocketAddress(InetAddress.getLocalHost(), PORT));
+                     new InetSocketAddress(InetAddress.getLocalHost(),
+                     TestLibrary.RMIDVIAINHERITEDCHANNEL_ACTIVATION_PORT));
                 System.err.println("serverSocket = " + serverSocket);
 
                 /*
@@ -174,7 +177,7 @@
                 try {
                     System.err.println("notify test...");
                     Registry registry =
-                        LocateRegistry.getRegistry(TestLibrary.REGISTRY_PORT);
+                        LocateRegistry.getRegistry(TestLibrary.RMIDVIAINHERITEDCHANNEL_REGISTRY_PORT);
                     Callback obj = (Callback) registry.lookup("Callback");
                     obj.notifyTest();
                 } catch (NotBoundException nbe) {
--- a/test/java/rmi/activation/rmidViaInheritedChannel/rmid.security.policy	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/activation/rmidViaInheritedChannel/rmid.security.policy	Wed Jun 12 11:48:21 2013 +0100
@@ -2,4 +2,5 @@
     permission java.lang.RuntimePermission "selectorProvider";
     permission java.lang.RuntimePermission "accessClassInPackage.sun.nio.ch";
     permission java.net.SocketPermission "*", "connect,accept";
+    permission java.util.PropertyPermission "java.rmi.activation.port", "write";
 };
--- a/test/java/rmi/registry/altSecurityManager/AltSecurityManager.java	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/registry/altSecurityManager/AltSecurityManager.java	Wed Jun 12 11:48:21 2013 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1999, 2008, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1999, 2012, 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
@@ -27,7 +27,7 @@
  * @author Laird Dornin
  *
  * @library ../../testlibrary
- * @build StreamPipe TestParams TestLibrary JavaVM
+ * @build StreamPipe TestParams TestLibrary JavaVM RMID
  * @build AltSecurityManager TestSecurityManager
  * @run main/othervm AltSecurityManager
  */
@@ -41,23 +41,41 @@
  * if registry and rmid take too long to exit.
  */
 public class AltSecurityManager implements Runnable {
-
+    private final int regPort;
     // variable to hold registry and rmid children
     static JavaVM vm = null;
 
     // names of utilities
     static String utilityToStart = null;
-    static String registry = "sun.rmi.registry.RegistryImpl";
-    static String rmid = "sun.rmi.server.Activation";
+    static final String REGISTRY_IMPL = "sun.rmi.registry.RegistryImpl";
+    static final String ACTIVATION = "sun.rmi.server.Activation";
 
     // children should exit in at least this time.
     static long TIME_OUT = 15000;
 
+    public AltSecurityManager(int port) {
+        if (port <= 0) {
+            TestLibrary.bomb("Port must be greater then 0.");
+        }
+
+        this.regPort = port;
+    }
+
     public void run() {
         try {
-            vm = new JavaVM(utilityToStart,
-                            " -Djava.security.manager=TestSecurityManager",
-                            "");
+            if (utilityToStart.equals(REGISTRY_IMPL)) {
+                vm = new JavaVM(utilityToStart,
+                        " -Djava.security.manager=TestSecurityManager",
+                        Integer.toString(regPort));
+            } else if (utilityToStart.contains(ACTIVATION)) {
+                vm = new JavaVM(utilityToStart,
+                        " -Djava.security.manager=TestSecurityManager",
+                        "-port " + Integer.toString(regPort));
+            } else {
+                TestLibrary.bomb("Utility to start must be " + REGISTRY_IMPL +
+                        " or " + ACTIVATION);
+            }
+
             System.err.println("starting " + utilityToStart);
             vm.start();
             vm.getVM().waitFor();
@@ -75,7 +93,8 @@
         utilityToStart = utility;
 
         try {
-            Thread thread = new Thread(new AltSecurityManager());
+            int port = TestLibrary.getUnusedRandomPort();
+            Thread thread = new Thread(new AltSecurityManager(port));
             System.err.println("expecting RuntimeException for " +
                                "checkListen in child process");
             long start = System.currentTimeMillis();
@@ -90,8 +109,8 @@
 
                 // dont pollute other tests; increase the likelihood
                 // that rmid will go away if it did not exit already.
-                if (utility.equals(rmid)) {
-                    RMID.shutdown();
+                if (utility.equals(ACTIVATION)) {
+                    RMID.shutdown(port);
                 }
 
                 TestLibrary.bomb(utilityToStart +
@@ -111,10 +130,10 @@
             System.err.println("\nRegression test for bug 4183202\n");
 
             // make sure the registry exits early.
-            ensureExit(registry);
+            ensureExit(REGISTRY_IMPL);
 
             // make sure rmid exits early
-            ensureExit(rmid);
+            ensureExit(ACTIVATION);
 
             System.err.println("test passed");
 
--- a/test/java/rmi/registry/classPathCodebase/ClassPathCodebase.java	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/registry/classPathCodebase/ClassPathCodebase.java	Wed Jun 12 11:48:21 2013 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1999, 2008, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1999, 2012, 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
@@ -30,7 +30,7 @@
  * @author Peter Jones
  *
  * @library ../../testlibrary
- * @build ClassPathCodebase Dummy
+ * @build ClassPathCodebase Dummy TestLibrary
  * @run main/othervm/policy=security.policy
  *     -Djava.rmi.server.useCodebaseOnly=false ClassPathCodebase
  */
@@ -84,11 +84,12 @@
                 System.getProperty("java.home") + File.separator +
                 "bin" + File.separator + "rmiregistry";
 
+            int port = TestLibrary.getUnusedRandomPort();
             String cmdarray[] = new String[] {
                 rmiregistryCommand,
                 "-J-Denv.class.path=.",
                 "-J-Djava.rmi.server.codebase=" + exportCodebaseURL,
-                Integer.toString(TestLibrary.REGISTRY_PORT) };
+                Integer.toString(port) };
 
             System.err.println("\nCommand used to spawn rmiregistry process:");
             System.err.println("\t" + Arrays.asList(cmdarray).toString());
@@ -119,7 +120,7 @@
              * dummy object to it.
              */
             Registry registry = LocateRegistry.getRegistry(
-                "localhost", TestLibrary.REGISTRY_PORT);
+                "localhost", port);
 
             try {
                 registry.bind(dummyBinding, dummyObject);
@@ -134,7 +135,7 @@
                 {
                     System.err.println(
                         "Error: another registry running on port " +
-                        TestLibrary.REGISTRY_PORT + "?");
+                        port + "?");
                 }
                 throw e;
             }
--- a/test/java/rmi/registry/emptyName/EmptyName.java	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/registry/emptyName/EmptyName.java	Wed Jun 12 11:48:21 2013 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2000, 2008, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2000, 2012, 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
@@ -24,6 +24,8 @@
 /* @test
  * @bug 4399304
  * @summary check that registry allows empty names
+ * @library ../../testlibrary
+ * @build TestLibrary
  * @run main/othervm EmptyName
  */
 import java.rmi.registry.LocateRegistry;
@@ -32,7 +34,7 @@
 
 public class EmptyName {
     public static void main(String[] args) throws Exception {
-        Registry impl = LocateRegistry.createRegistry(0);
+        Registry impl = TestLibrary.createRegistryOnUnusedPort();
         Registry stub = (Registry) RemoteObject.toStub(impl);
         stub.bind("", stub);
         stub.lookup("");
--- a/test/java/rmi/registry/interfaceHash/InterfaceHash.java	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/registry/interfaceHash/InterfaceHash.java	Wed Jun 12 11:48:21 2013 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2001, 2008, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2001, 2012, 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
@@ -35,8 +35,10 @@
  * "interface hash": 4905912898345647071L.
  *
  * @author Peter Jones
+ * @library ../../testlibrary
  * @build InterfaceHash
  * @build ReferenceRegistryStub
+ * @build TestLibrary
  * @run main/othervm InterfaceHash
  */
 
@@ -58,7 +60,7 @@
 
 public class InterfaceHash {
 
-    private static final int PORT = 2020;
+    private static final int PORT = TestLibrary.getUnusedRandomPort();
     private static final String NAME = "WMM";
 
     public static void main(String[] args) throws Exception {
--- a/test/java/rmi/registry/multipleRegistries/MultipleRegistries.java	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/registry/multipleRegistries/MultipleRegistries.java	Wed Jun 12 11:48:21 2013 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2003, 2008, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2003, 2012, 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
@@ -26,6 +26,8 @@
  * @summary Can't run multiple registries in the same VM
  * @author Ann Wollrath
  *
+ * @library ../../testlibrary
+ * @build TestLibrary
  * @build MultipleRegistries
  * @run main/othervm/timeout=240 MultipleRegistries
  */
@@ -58,12 +60,13 @@
             System.err.println("proxy = " + proxy);
 
             System.err.println("export registries");
-            Registry registryImpl1 = LocateRegistry.createRegistry(2030);
-            Registry registryImpl2 = LocateRegistry.createRegistry(2040);
-
+            Registry registryImpl1 = TestLibrary.createRegistryOnUnusedPort();
+            int port1 = TestLibrary.getRegistryPort(registryImpl1);
+            Registry registryImpl2 = TestLibrary.createRegistryOnUnusedPort();
+            int port2 = TestLibrary.getRegistryPort(registryImpl2);
             System.err.println("bind remote object in registries");
-            Registry registry1 = LocateRegistry.getRegistry(2030);
-            Registry registry2 = LocateRegistry.getRegistry(2040);
+            Registry registry1 = LocateRegistry.getRegistry(port1);
+            Registry registry2 = LocateRegistry.getRegistry(port2);
 
             registry1.bind(NAME, proxy);
             registry2.bind(NAME, proxy);
--- a/test/java/rmi/registry/readTest/readTest.java	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/registry/readTest/readTest.java	Wed Jun 12 11:48:21 2013 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2011, 2012, 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
@@ -30,18 +30,19 @@
 public class readTest {
 
     public static void main(String args[]) throws Exception {
-        int port = 7491;
         try {
             testPkg.Server obj = new testPkg.Server();
             testPkg.Hello stub = (testPkg.Hello) UnicastRemoteObject.exportObject(obj, 0);
             // Bind the remote object's stub in the registry
-            Registry registry = LocateRegistry.getRegistry(port);
+            Registry registry =
+                LocateRegistry.getRegistry(TestLibrary.READTEST_REGISTRY_PORT);
             registry.bind("Hello", stub);
 
             System.err.println("Server ready");
 
             // now, let's test client
-            testPkg.Client client = new testPkg.Client(port);
+            testPkg.Client client =
+                new testPkg.Client(TestLibrary.READTEST_REGISTRY_PORT);
             String testStubReturn = client.testStub();
             if(!testStubReturn.equals(obj.hello)) {
                 throw new RuntimeException("Test Fails : unexpected string from stub call");
--- a/test/java/rmi/registry/readTest/readTest.sh	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/registry/readTest/readTest.sh	Wed Jun 12 11:48:21 2013 +0100
@@ -1,5 +1,5 @@
 #
-# Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
+# Copyright (c) 2011, 2012, 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
@@ -23,6 +23,8 @@
 
 # @test
 # @bug 7102369 7094468 7100592
+# @library ../../testlibrary
+# @build TestLibrary
 # @summary remove java.rmi.server.codebase property parsing from registyimpl
 # @run shell readTest.sh
 
@@ -52,6 +54,7 @@
 RMIREG_OUT=rmi.out
 #start rmiregistry without any local classes on classpath
 cd rmi_tmp
+# NOTE: This RMI Registry port must match TestLibrary.READTEST_REGISTRY_PORT
 ${TESTJAVA}${FS}bin${FS}rmiregistry -J-Djava.rmi.server.useCodebaseOnly=false \
     ${TESTTOOLVMOPTS} 64005 > ..${FS}${RMIREG_OUT} 2>&1 &
 RMIREG_PID=$!
--- a/test/java/rmi/registry/reexport/Reexport.java	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/registry/reexport/Reexport.java	Wed Jun 12 11:48:21 2013 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1999, 2008, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1999, 2012, 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
@@ -49,22 +49,21 @@
 import java.rmi.server.*;
 
 public class Reexport {
-    static public final int regport = TestLibrary.REGISTRY_PORT;
-
     static public void main(String[] argv) {
 
         Registry reg = null;
+        int regPort = TestLibrary.getUnusedRandomPort();
 
         try {
             System.err.println("\nregression test for 4120329\n");
 
             // establish the registry (we hope)
-            System.err.println("Starting registry on port " + regport);
-            Reexport.makeRegistry(regport);
+            System.err.println("Starting registry on port " + regPort);
+            Reexport.makeRegistry(regPort);
 
             // Get a handle to the registry
             System.err.println("Creating duplicate registry, this should fail...");
-            reg = createReg(true);
+            reg = createReg(true, regPort);
 
             if (reg != null) {
                 TestLibrary.bomb("failed was able to duplicate the registry?!?");
@@ -73,7 +72,7 @@
             // Kill the first registry.
             System.err.println("Bringing down the first registry");
             try {
-                Reexport.killRegistry();
+                Reexport.killRegistry(regPort);
             } catch (Exception foo) {
             }
 
@@ -81,7 +80,7 @@
             System.err.println("Trying again to start our own " +
                                "registry... this should work");
 
-            reg = createReg(false);
+            reg = createReg(false, regPort);
 
             if (reg == null) {
                 TestLibrary.bomb("Could not create registry on second try");
@@ -93,17 +92,17 @@
             TestLibrary.bomb(e);
         } finally {
             // dont leave the registry around to affect other tests.
-            killRegistry();
+            killRegistry(regPort);
 
             reg = null;
         }
     }
 
-    static Registry createReg(boolean remoteOk) {
+    static Registry createReg(boolean remoteOk, int port) {
         Registry reg = null;
 
         try {
-            reg = LocateRegistry.createRegistry(regport);
+            reg = LocateRegistry.createRegistry(port);
         } catch (Throwable e) {
             if (remoteOk) {
                 System.err.println("EXPECTING PORT IN USE EXCEPTION:");
@@ -140,10 +139,10 @@
     }
     private static Process subreg = null;
 
-    public static void killRegistry() {
+    public static void killRegistry(int port) {
         if (Reexport.subreg != null) {
 
-            RegistryRunner.requestExit();
+            RegistryRunner.requestExit(port);
 
             try {
                 Reexport.subreg.waitFor();
--- a/test/java/rmi/reliability/juicer/AppleUserImpl.java	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/reliability/juicer/AppleUserImpl.java	Wed Jun 12 11:48:21 2013 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2003, 2008, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2003, 2012, 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
@@ -59,19 +59,21 @@
  * @build Apple AppleEvent AppleImpl AppleUserImpl
  * @build Orange OrangeEcho OrangeEchoImpl OrangeImpl
  * @build ApplicationServer
+ * @build TestLibrary
  *
  * @run main/othervm/policy=security.policy AppleUserImpl -seconds 30
  *
  * @author Peter Jones, Nigel Daley
  */
 
+import java.rmi.NoSuchObjectException;
 import java.rmi.RemoteException;
-import java.rmi.NoSuchObjectException;
-import java.rmi.server.UnicastRemoteObject;
 import java.rmi.registry.LocateRegistry;
+import java.rmi.registry.Registry;
+import java.rmi.server.UnicastRemoteObject;
 import java.util.Random;
+import java.util.logging.Level;
 import java.util.logging.Logger;
-import java.util.logging.Level;
 
 /**
  * The AppleUserImpl class implements the behavior of the remote
@@ -80,7 +82,7 @@
  * AppleUserThread is created for each apple.
  */
 public class AppleUserImpl extends UnicastRemoteObject implements AppleUser {
-
+    private static int registryPort = -1;
     private static final Logger logger =
         Logger.getLogger("reliability.appleuser");
     private static int threadNum = 0;
@@ -308,8 +310,10 @@
 
             synchronized (user) {
                 // create new registry and bind new AppleUserImpl in registry
-                LocateRegistry.createRegistry(2006);
-                LocateRegistry.getRegistry(2006).rebind("AppleUser",user);
+                Registry registry = TestLibrary.createRegistryOnUnusedPort();
+                registryPort = TestLibrary.getRegistryPort(registry);
+                LocateRegistry.getRegistry(registryPort).rebind("AppleUser",
+                                                                 user);
 
                 // start the other server if applicable
                 if (othervm) {
@@ -318,7 +322,9 @@
                         "started in separate process");
                 } else {
                     Class app = Class.forName("ApplicationServer");
-                    server = new Thread((Runnable) app.newInstance());
+                    java.lang.reflect.Constructor appConstructor =
+                            app.getDeclaredConstructor(new Class[] {Integer.TYPE});
+                    server = new Thread((Runnable) appConstructor.newInstance(registryPort));
                     logger.log(Level.INFO, "Starting application server " +
                         "in same process");
                     server.start();
--- a/test/java/rmi/reliability/juicer/ApplicationServer.java	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/reliability/juicer/ApplicationServer.java	Wed Jun 12 11:48:21 2013 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2003, 2008, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2003, 2012, 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
@@ -38,18 +38,21 @@
     private static final int LOOKUP_ATTEMPTS = 5;
     private static final int DEFAULT_NUMAPPLES = 10;
     private static final String DEFAULT_REGISTRYHOST = "localhost";
+    private static final int DEFAULT_REGISTRYPORT = -1;
     private final int numApples;
     private final String registryHost;
+    private final int registryPort;
     private final Apple[] apples;
     private AppleUser user;
 
-    ApplicationServer() {
-        this(DEFAULT_NUMAPPLES, DEFAULT_REGISTRYHOST);
+    ApplicationServer(int registryPort) {
+        this(DEFAULT_NUMAPPLES, DEFAULT_REGISTRYHOST, registryPort);
     }
 
-    ApplicationServer(int numApples, String registryHost) {
+    ApplicationServer(int numApples, String registryHost, int registryPort) {
         this.numApples = numApples;
         this.registryHost = registryHost;
+        this.registryPort = registryPort;
         apples = new Apple[numApples];
     }
 
@@ -71,7 +74,7 @@
             for (i = 0; i < LOOKUP_ATTEMPTS; i++) {
                 try {
                     Registry registry = LocateRegistry.getRegistry(
-                        registryHost, 2006);
+                           registryHost, registryPort);
                     user = (AppleUser) registry.lookup("AppleUser");
                     user.startTest();
                     break; //successfully obtained AppleUser
@@ -120,16 +123,20 @@
     private static void usage() {
         System.err.println("Usage: ApplicationServer [-numApples <numApples>]");
         System.err.println("                         [-registryHost <host>]");
+        System.err.println("                         -registryPort <port>");
         System.err.println("  numApples  The number of apples (threads) to use.");
         System.err.println("             The default is 10 apples.");
         System.err.println("  host       The host running rmiregistry " +
                                          "which contains AppleUser.");
         System.err.println("             The default is \"localhost\".");
+        System.err.println("  port       The port the rmiregistry is running" +
+                                         "on.");
         System.err.println();
     }
 
     public static void main(String[] args) {
         int num = DEFAULT_NUMAPPLES;
+        int port = -1;
         String host = DEFAULT_REGISTRYHOST;
 
         // parse command line args
@@ -142,17 +149,25 @@
                 } else if (arg.equals("-registryHost")) {
                     i++;
                     host = args[i];
+                } else if (arg.equals("-registryPort")) {
+                    i++;
+                    port = Integer.parseInt(args[i]);
                 } else {
                     usage();
                 }
             }
+
+            if (port == -1) {
+                usage();
+                throw new RuntimeException("Port must be specified.");
+            }
         } catch (Throwable t) {
             usage();
             throw new RuntimeException("TEST FAILED: Bad argument");
         }
 
         // start the client server
-        Thread server = new Thread(new ApplicationServer(num,host));
+        Thread server = new Thread(new ApplicationServer(num,host,port));
         server.start();
         // main should exit once all exported remote objects are gc'd
     }
--- a/test/java/rmi/server/RMISocketFactory/useSocketFactory/activatable/EchoImpl.java	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/server/RMISocketFactory/useSocketFactory/activatable/EchoImpl.java	Wed Jun 12 11:48:21 2013 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1998, 2008, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1998, 2012, 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
@@ -128,8 +128,9 @@
             ActivationGroup.createGroup(groupID, groupDesc, 0);
 
             EchoImpl impl = new EchoImpl(protocol);
-            System.out.println("EchoServer: binding in registry");
-            Naming.rebind("//:" + UseCustomSocketFactory.REGISTRY_PORT +
+            int registryPort = Integer.parseInt(System.getProperty("rmi.registry.port"));
+            System.out.println("EchoServer: binding in registry on port:" + registryPort);
+            Naming.rebind("//:" + registryPort +
                           "/EchoServer", impl);
             System.out.println("EchoServer ready.");
         } catch (Exception e) {
--- a/test/java/rmi/server/RMISocketFactory/useSocketFactory/activatable/UseCustomSocketFactory.java	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/server/RMISocketFactory/useSocketFactory/activatable/UseCustomSocketFactory.java	Wed Jun 12 11:48:21 2013 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1998, 2008, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1998, 2012, 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
@@ -32,6 +32,7 @@
  * @build EchoImpl
  * @build EchoImpl_Stub
  * @build UseCustomSocketFactory
+ * @build TestLibrary
  * @run main/othervm/policy=security.policy/timeout=360 UseCustomSocketFactory
  */
 
@@ -42,8 +43,8 @@
 import java.rmi.registry.*;
 
 public class UseCustomSocketFactory {
+    static final int REGISTRY_PORT = TestLibrary.getUnusedRandomPort();
 
-    final static int REGISTRY_PORT = 2006;
     static String[] protocol = new String[] { "", "compress", "xor" };
 
     public static void main(String[] args) {
@@ -68,7 +69,7 @@
                     " -C-Djava.security.manager=java.rmi.RMISecurityManager "});
             rmid.start();
 
-            Echo[] echo = spawnAndTest();
+            Echo[] echo = spawnAndTest(rmid.getPort());
             reactivateAndTest(echo);
         } catch (IOException e) {
             TestLibrary.bomb("creating rmid", e);
@@ -78,17 +79,20 @@
         }
     }
 
-    private static Echo[] spawnAndTest() {
+    private static Echo[] spawnAndTest(int rmidPort) {
 
         System.err.println("\nCreate Test-->");
 
         Echo[] echo = new Echo[protocol.length];
 
         for (int i = 0; i < protocol.length; i++) {
-
             JavaVM serverVM = new JavaVM("EchoImpl",
                                          "-Djava.security.policy=" +
-                                         TestParams.defaultPolicy,
+                                         TestParams.defaultPolicy +
+                                         " -Drmi.registry.port=" +
+                                         REGISTRY_PORT +
+                                         " -Djava.rmi.activation.port=" +
+                                         rmidPort,
                                          protocol[i]);
 
             System.err.println("\nusing protocol: " +
--- a/test/java/rmi/server/RMISocketFactory/useSocketFactory/activatable/security.policy	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/server/RMISocketFactory/useSocketFactory/activatable/security.policy	Wed Jun 12 11:48:21 2013 +0100
@@ -30,6 +30,9 @@
   permission java.util.PropertyPermission "user.dir", "read";
   permission java.util.PropertyPermission "java.home", "read";
 
+  permission java.util.PropertyPermission "rmi.registry.port", "read";
+  permission java.util.PropertyPermission "java.rmi.activation.port", "write";
+
   // required for test to create an ActivationGroup
   permission java.lang.RuntimePermission "setFactory";
 
--- a/test/java/rmi/server/RMISocketFactory/useSocketFactory/registry/HelloImpl.java	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/server/RMISocketFactory/useSocketFactory/registry/HelloImpl.java	Wed Jun 12 11:48:21 2013 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1998, 2008, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1998, 2012, 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
@@ -65,8 +65,9 @@
             if (args.length >= 1)
                 protocol = args[0];
 
+            int registryPort = Integer.parseInt(System.getProperty("rmi.registry.port"));
             registry = java.rmi.registry.LocateRegistry.
-                getRegistry("localhost", TestLibrary.REGISTRY_PORT,
+                getRegistry("localhost", registryPort,
                             new Compress.CompressRMIClientSocketFactory());
             UseCustomSocketFactory.checkStub(registry, "RMIClientSocket");
             hello = (Hello) registry.lookup("/HelloServer");
--- a/test/java/rmi/server/RMISocketFactory/useSocketFactory/registry/UseCustomSocketFactory.java	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/server/RMISocketFactory/useSocketFactory/registry/UseCustomSocketFactory.java	Wed Jun 12 11:48:21 2013 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1998, 2008, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1998, 2012, 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
@@ -32,6 +32,7 @@
  * @build Hello
  * @build HelloImpl
  * @build HelloImpl_Stub
+ * @build TestLibrary
  * @build UseCustomSocketFactory
  * @build Compress
  * @run main/othervm/policy=security.policy/timeout=240 UseCustomSocketFactory
@@ -58,6 +59,7 @@
         System.out.println("\nRegression test for bug 4148850\n");
 
         TestLibrary.suggestSecurityManager("java.rmi.RMISecurityManager");
+        int registryPort = TestLibrary.getUnusedRandomPort();
 
         try {
             impl = new HelloImpl();
@@ -67,7 +69,7 @@
              * allow the rmiregistry to be secure.
              */
             registry = LocateRegistry.
-                createRegistry(TestLibrary.REGISTRY_PORT,
+                createRegistry(registryPort,
                                new Compress.CompressRMIClientSocketFactory(),
                                new Compress.CompressRMIServerSocketFactory());
             registry.rebind("/HelloServer", impl);
@@ -77,8 +79,12 @@
             TestLibrary.bomb("creating registry", e);
         }
 
-        JavaVM serverVM = new JavaVM("HelloImpl", "-Djava.security.policy=" +
-                                     TestParams.defaultPolicy, "");
+        JavaVM serverVM = new JavaVM("HelloImpl",
+                                     "-Djava.security.policy=" +
+                                     TestParams.defaultPolicy +
+                                     " -Drmi.registry.port=" +
+                                     registryPort,
+                                     "");
 
         try {
 
--- a/test/java/rmi/server/RMISocketFactory/useSocketFactory/registry/security.policy	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/server/RMISocketFactory/useSocketFactory/registry/security.policy	Wed Jun 12 11:48:21 2013 +0100
@@ -22,6 +22,8 @@
   permission java.util.PropertyPermission "user.dir", "read";
   permission java.util.PropertyPermission "java.home", "read";
 
+  permission java.util.PropertyPermission "rmi.registry.port", "read";	
+
   permission java.util.PropertyPermission "java.security.policy", "read";
   permission java.util.PropertyPermission "java.security.manager", "read";
 
--- a/test/java/rmi/server/RMISocketFactory/useSocketFactory/unicast/EchoImpl.java	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/server/RMISocketFactory/useSocketFactory/unicast/EchoImpl.java	Wed Jun 12 11:48:21 2013 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1998, 2008, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1998, 2012, 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
@@ -64,8 +64,9 @@
 
             System.out.println("EchoServer: creating remote object");
             EchoImpl impl = new EchoImpl(protocol);
+            int registryPort = Integer.parseInt(System.getProperty("rmi.registry.port"));
             System.out.println("EchoServer: binding in registry");
-            Naming.rebind("//:" + TestLibrary.REGISTRY_PORT +
+            Naming.rebind("//:" + registryPort +
                           "/EchoServer", impl);
             System.out.println("EchoServer ready.");
         } catch (Exception e) {
--- a/test/java/rmi/server/RMISocketFactory/useSocketFactory/unicast/UseCustomSocketFactory.java	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/server/RMISocketFactory/useSocketFactory/unicast/UseCustomSocketFactory.java	Wed Jun 12 11:48:21 2013 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1998, 2008, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1998, 2012, 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
@@ -46,6 +46,8 @@
 
     public static void main(String[] args) {
 
+        int registryPort = -1;
+
         String[] protocol = new String[] { "", "compress", "xor" };
 
         System.out.println("\nRegression test for bug 4127826\n");
@@ -53,7 +55,8 @@
         TestLibrary.suggestSecurityManager("java.rmi.RMISecurityManager");
 
         try {
-            LocateRegistry.createRegistry(TestLibrary.REGISTRY_PORT);
+            Registry registry = TestLibrary.createRegistryOnUnusedPort();
+            registryPort = TestLibrary.getRegistryPort(registry);
         } catch (Exception e) {
             TestLibrary.bomb("creating registry", e);
         }
@@ -65,7 +68,9 @@
 
             JavaVM serverVM = new JavaVM("EchoImpl",
                                          "-Djava.security.policy=" +
-                                         TestParams.defaultPolicy,
+                                         TestParams.defaultPolicy +
+                                         " -Drmi.registry.port=" +
+                                         registryPort,
                                          protocol[i]);
             System.err.println("\nusing protocol: " +
                                (protocol[i] == "" ? "none" : protocol[i]));
@@ -79,7 +84,7 @@
                 Echo obj = null;
                 do {
                     try {
-                        obj = (Echo) Naming.lookup("//:" + TestLibrary.REGISTRY_PORT +
+                        obj = (Echo) Naming.lookup("//:" + registryPort +
                                                    "/EchoServer");
                         break;
                     } catch (NotBoundException e) {
@@ -109,7 +114,7 @@
             } finally {
                 serverVM.destroy();
                 try {
-                    Naming.unbind("//:" + TestLibrary.REGISTRY_PORT +
+                    Naming.unbind("//:" + registryPort +
                                   "/EchoServer");
                 } catch (Exception e) {
                     TestLibrary.bomb("unbinding EchoServer", e);
--- a/test/java/rmi/server/RMISocketFactory/useSocketFactory/unicast/security.policy	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/server/RMISocketFactory/useSocketFactory/unicast/security.policy	Wed Jun 12 11:48:21 2013 +0100
@@ -22,9 +22,18 @@
   permission java.util.PropertyPermission "user.dir", "read";
   permission java.util.PropertyPermission "java.home", "read";
 
+  permission java.util.PropertyPermission "rmi.registry.port", "read";	
+
   permission java.util.PropertyPermission "java.security.policy", "read";
   permission java.util.PropertyPermission "java.security.manager", "read";
 
+  // used by TestLibrary to get the RMI Registry port
+  permission java.lang.RuntimePermission "accessClassInPackage.sun.rmi.registry";
+  permission java.lang.RuntimePermission "accessClassInPackage.sun.rmi.server";
+  permission java.lang.RuntimePermission "accessClassInPackage.sun.rmi.transport";
+  permission java.lang.RuntimePermission "accessClassInPackage.sun.rmi.transport.proxy";
+  permission java.lang.RuntimePermission "accessClassInPackage.sun.rmi.transport.tcp";
+
   // test needs to export rmid and communicate with objects on arbitrary ports
   permission java.net.SocketPermission "*:1024-", "connect,accept,listen";
 };
--- a/test/java/rmi/server/RemoteServer/AddrInUse.java	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/server/RemoteServer/AddrInUse.java	Wed Jun 12 11:48:21 2013 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1998, 2003, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1998, 2012, 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
@@ -26,6 +26,7 @@
  * @summary retryServerSocket should not retry on BindException
  * @author Ann Wollrath
  *
+ * @library ../../testlibrary
  * @build AddrInUse
  * @run main/othervm AddrInUse
  */
@@ -36,7 +37,7 @@
 
 public class AddrInUse implements Runnable {
 
-    private static final int PORT = 9999;
+    private static int port = -1;
     private static final long TIMEOUT = 10000;
 
     private boolean exportSucceeded = false;
@@ -49,7 +50,7 @@
          * has already been bound, and record the result.
          */
         try {
-            LocateRegistry.createRegistry(PORT);
+            LocateRegistry.createRegistry(port);
             synchronized (this) {
                 exportSucceeded = true;
                 notifyAll();
@@ -68,8 +69,9 @@
         /*
          * Bind a server socket to a port.
          */
-        System.err.println("create a ServerSocket on port " + PORT + "...");
-        ServerSocket server = new ServerSocket(PORT);
+        ServerSocket server = new ServerSocket(0);
+        port = server.getLocalPort();
+        System.err.println("Created a ServerSocket on port " + port + "...");
 
         /*
          * Start a thread that creates a registry on the same port,
--- a/test/java/rmi/server/UnicastRemoteObject/keepAliveDuringCall/KeepAliveDuringCall.java	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/server/UnicastRemoteObject/keepAliveDuringCall/KeepAliveDuringCall.java	Wed Jun 12 11:48:21 2013 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2001, 2008, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2001, 2012, 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
@@ -82,15 +82,17 @@
             UnicastRemoteObject.exportObject(obj);
             System.err.println("exported shutdown monitor");
 
-            Registry localRegistry =
-                LocateRegistry.createRegistry(TestLibrary.REGISTRY_PORT);
+            Registry localRegistry = TestLibrary.createRegistryOnUnusedPort();
+            int registryPort = TestLibrary.getRegistryPort(localRegistry);
             System.err.println("created local registry");
 
             localRegistry.bind(BINDING, obj);
             System.err.println("bound shutdown monitor in local registry");
 
             System.err.println("starting remote ShutdownImpl VM...");
-            (new JavaVM("ShutdownImpl")).start();
+            (new JavaVM("ShutdownImpl",
+                        "-Drmi.registry.port=" +
+                        registryPort, "")).start();
 
             Shutdown s;
             synchronized (obj.lock) {
--- a/test/java/rmi/server/UnicastRemoteObject/keepAliveDuringCall/ShutdownImpl.java	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/server/UnicastRemoteObject/keepAliveDuringCall/ShutdownImpl.java	Wed Jun 12 11:48:21 2013 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2001, 2008, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2001, 2012, 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
@@ -68,8 +68,9 @@
 
     public static void main(String[] args) {
         try {
+            int registryPort = Integer.parseInt(System.getProperty("rmi.registry.port"));
             Registry registry =
-                LocateRegistry.getRegistry("", TestLibrary.REGISTRY_PORT);
+                LocateRegistry.getRegistry("", registryPort);
             ShutdownMonitor monitor = (ShutdownMonitor)
                 registry.lookup(KeepAliveDuringCall.BINDING);
             System.err.println("(ShutdownImpl) retrieved shutdown monitor");
--- a/test/java/rmi/server/UnicastRemoteObject/unexportObject/UnexportLeak.java	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/server/UnicastRemoteObject/unexportObject/UnexportLeak.java	Wed Jun 12 11:48:21 2013 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2000, 2008, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2000, 2012, 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
@@ -28,8 +28,10 @@
  *
  * @author Ann Wollrath
  *
+ * @library ../../../testlibrary
  * @build UnexportLeak
  * @build UnexportLeak_Stub
+ * @build TestLibrary
  * @build Ping
  * @run main/othervm UnexportLeak
  */
@@ -40,20 +42,18 @@
 import java.rmi.registry.*;
 
 public class UnexportLeak implements Ping {
-
-    private static int PORT = 2006;
-
     public void ping() {
     }
 
     public static void main(String[] args) {
         try {
             System.err.println("\nRegression test for bug 4331349\n");
-            LocateRegistry.createRegistry(PORT);
+            Registry registry = TestLibrary.createRegistryOnUnusedPort();
+            int registryPort = TestLibrary.getRegistryPort(registry);
             Remote obj = new UnexportLeak();
             WeakReference wr = new WeakReference(obj);
             UnicastRemoteObject.exportObject(obj);
-            LocateRegistry.getRegistry(PORT).rebind("UnexportLeak", obj);
+            LocateRegistry.getRegistry(registryPort).rebind("UnexportLeak", obj);
             UnicastRemoteObject.unexportObject(obj, true);
             obj = null;
             flushRefs();
--- a/test/java/rmi/server/Unreferenced/finiteGCLatency/FiniteGCLatency.java	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/server/Unreferenced/finiteGCLatency/FiniteGCLatency.java	Wed Jun 12 11:48:21 2013 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1998, 2008, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1998, 2012, 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
@@ -37,6 +37,7 @@
  * @library ../../../testlibrary
  * @build FiniteGCLatency
  * @build FiniteGCLatency_Stub
+ * @build TestLibrary
  * @run main/othervm/timeout=120 FiniteGCLatency
  */
 
@@ -78,11 +79,11 @@
         try {
             UnicastRemoteObject.exportObject(obj);
             System.err.println("exported remote object");
-
-            LocateRegistry.createRegistry(TestLibrary.REGISTRY_PORT);
+            Registry registry1 = TestLibrary.createRegistryOnUnusedPort();
+            int port = TestLibrary.getRegistryPort(registry1);
             System.err.println("created registry");
 
-            Registry registry = LocateRegistry.getRegistry("", TestLibrary.REGISTRY_PORT);
+            Registry registry = LocateRegistry.getRegistry("", port);
             registry.bind(BINDING, obj);
             System.err.println("bound remote object in registry");
 
--- a/test/java/rmi/server/Unreferenced/leaseCheckInterval/LeaseCheckInterval.java	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/server/Unreferenced/leaseCheckInterval/LeaseCheckInterval.java	Wed Jun 12 11:48:21 2013 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2001, 2008, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2001, 2012, 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
@@ -87,8 +87,9 @@
             UnicastRemoteObject.exportObject(obj);
             System.err.println("exported remote object");
 
+            int registryPort = TestLibrary.getUnusedRandomPort();
             Registry localRegistry =
-                LocateRegistry.createRegistry(TestLibrary.REGISTRY_PORT);
+                LocateRegistry.createRegistry(registryPort);
             System.err.println("created local registry");
 
             localRegistry.bind(BINDING, obj);
@@ -96,7 +97,8 @@
 
             synchronized (obj.lock) {
                 System.err.println("starting remote client VM...");
-                (new JavaVM("SelfTerminator")).start();
+                (new JavaVM("SelfTerminator", "-Drmi.registry.port=" +
+                            registryPort, "")).start();
 
                 System.err.println("waiting for unreferenced() callback...");
                 obj.lock.wait(TIMEOUT);
--- a/test/java/rmi/server/Unreferenced/leaseCheckInterval/SelfTerminator.java	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/server/Unreferenced/leaseCheckInterval/SelfTerminator.java	Wed Jun 12 11:48:21 2013 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2001, 2008, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2001, 2012, 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
@@ -33,8 +33,10 @@
 
     public static void main(String[] args) {
         try {
+            int registryPort =
+                Integer.parseInt(System.getProperty("rmi.registry.port"));
             Registry registry =
-                LocateRegistry.getRegistry("", TestLibrary.REGISTRY_PORT);
+                LocateRegistry.getRegistry("", registryPort);
             Remote stub = registry.lookup(LeaseCheckInterval.BINDING);
             Runtime.getRuntime().halt(0);
         } catch (Exception e) {
--- a/test/java/rmi/server/Unreferenced/unreferencedContext/UnreferencedContext.java	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/server/Unreferenced/unreferencedContext/UnreferencedContext.java	Wed Jun 12 11:48:21 2013 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1998, 2008, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1998, 2012, 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
@@ -41,6 +41,7 @@
  * @library ../../../testlibrary
  * @build UnreferencedContext
  * @build UnreferencedContext_Stub
+ * @build TestLibrary
  * @run main/othervm/timeout=120 UnreferencedContext
  */
 
@@ -119,10 +120,11 @@
             UnicastRemoteObject.exportObject(obj);
             System.err.println("exported remote object");
 
-            LocateRegistry.createRegistry(TestLibrary.REGISTRY_PORT);
+            Registry registry1 = TestLibrary.createRegistryOnUnusedPort();
+            int port = TestLibrary.getRegistryPort(registry1);
             System.err.println("created registry");
 
-            Registry registry = LocateRegistry.getRegistry("", TestLibrary.REGISTRY_PORT);
+            Registry registry = LocateRegistry.getRegistry("", port);
             registry.bind(BINDING, obj);
             System.err.println("bound remote object in registry");
 
--- a/test/java/rmi/server/useCustomRef/UseCustomRef.java	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/server/useCustomRef/UseCustomRef.java	Wed Jun 12 11:48:21 2013 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1998, 2007, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1998, 2012, 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
@@ -35,6 +35,7 @@
  * @build Ping
  * @build UseCustomRef_Stub
  * @build UseCustomRef_Skel
+ * @build TestLibrary
  * @run main/othervm/policy=security.policy/secure=java.rmi.RMISecurityManager/timeout=120 UseCustomRef
  *
  * This test was failing to run because the synthetic access
@@ -84,8 +85,9 @@
             TestLibrary.suggestSecurityManager("java.rmi.RMISecurityManager");
 
             System.err.println("creating Registry...");
-            registry = LocateRegistry.createRegistry(TestLibrary.REGISTRY_PORT);
 
+            registry = TestLibrary.createRegistryOnUnusedPort();
+            int port = TestLibrary.getRegistryPort(registry);
             /*
              * create object with custom ref and bind in registry
              */
@@ -97,7 +99,7 @@
                                 "instanceof CustomServerRef");
             }
 
-            String name = "//:" + TestLibrary.REGISTRY_PORT + "/UseCustomRef";
+            String name = "//:" + port + "/UseCustomRef";
             //      String name = "UseCustomRef";
             System.err.println("binding object in registry...");
             Naming.rebind(name, cr);
--- a/test/java/rmi/server/useCustomRef/security.policy	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/server/useCustomRef/security.policy	Wed Jun 12 11:48:21 2013 +0100
@@ -8,8 +8,11 @@
 
 grant {
   // the test uses a class in the package sun.rmi.server
+  permission java.lang.RuntimePermission "accessClassInPackage.sun.rmi.registry";
   permission java.lang.RuntimePermission "accessClassInPackage.sun.rmi.server";
   permission java.lang.RuntimePermission "accessClassInPackage.sun.rmi.transport";
+  permission java.lang.RuntimePermission "accessClassInPackage.sun.rmi.transport.proxy";
+  permission java.lang.RuntimePermission "accessClassInPackage.sun.rmi.transport.tcp";
   permission java.util.PropertyPermission "package.restrict.access.sun", "read";
   permission java.util.PropertyPermission "package.restrict.access.sun.rmi", "read";
 
--- a/test/java/rmi/testlibrary/ActivationLibrary.java	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/testlibrary/ActivationLibrary.java	Wed Jun 12 11:48:21 2013 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1998, 2006, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1998, 2012, 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
@@ -134,37 +134,10 @@
         return false;
     }
 
-    /**
-     * Check to see if an arry of Strings contains a given string.
-     */
-    private static boolean
-        containsString(String[] strings, String contained)
-    {
-        if (strings == null) {
-            if (contained == null) {
-                return true;
-            }
-            return false;
-        }
-
-        for (int i = 0 ; i < strings.length ; i ++ ) {
-            if ((strings[i] != null) &&
-                (strings[i].indexOf(contained) >= 0))
-            {
-                return true;
-            }
-        }
-        return false;
-    }
-
     /** cleanup after rmid */
     public static void rmidCleanup(RMID rmid) {
-        rmidCleanup(rmid, TestLibrary.RMID_PORT);
-    }
-
-    public static void rmidCleanup(RMID rmid, int port) {
         if (rmid != null) {
-            if (!ActivationLibrary.safeDestroy(rmid, port, SAFE_WAIT_TIME)) {
+            if (!ActivationLibrary.safeDestroy(rmid, SAFE_WAIT_TIME)) {
                 TestLibrary.bomb("rmid not destroyed in: " +
                                  SAFE_WAIT_TIME +
                                  " milliseconds");
@@ -180,8 +153,8 @@
      * @return whether or not shutdown completed succesfully in the
      *         timeAllowed
      */
-    private static boolean safeDestroy(RMID rmid, int port, long timeAllowed) {
-        DestroyThread destroyThread = new DestroyThread(rmid, port);
+    private static boolean safeDestroy(RMID rmid, long timeAllowed) {
+        DestroyThread destroyThread = new DestroyThread(rmid);
         destroyThread.start();
 
         try {
@@ -201,9 +174,9 @@
         private final int port;
         private boolean succeeded = false;
 
-        DestroyThread(RMID rmid, int port) {
+        DestroyThread(RMID rmid) {
             this.rmid = rmid;
-            this.port = port;
+            this.port = rmid.getPort();
             this.setDaemon(true);
         }
 
--- a/test/java/rmi/testlibrary/RMID.java	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/testlibrary/RMID.java	Wed Jun 12 11:48:21 2013 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1998, 2006, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1998, 2012, 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
@@ -28,7 +28,6 @@
 import java.io.*;
 import java.rmi.*;
 import java.rmi.activation.*;
-import java.util.Properties;
 
 /**
  * Utility class that creates an instance of rmid with a policy
@@ -136,7 +135,7 @@
                                   boolean debugExec)
     {
         return createRMID(out, err, debugExec, true,
-                          TestLibrary.RMID_PORT);
+                          TestLibrary.getUnusedRandomPort());
     }
 
     public static RMID createRMID(OutputStream out, OutputStream err,
@@ -211,7 +210,7 @@
         // if rmid is already running, then the test will fail with
         // a well recognized exception (port already in use...).
 
-        mesg("starting rmid...");
+        mesg("starting rmid on port #" + port + "...");
         super.start();
 
         int slopFactor = 1;
@@ -232,6 +231,14 @@
 
             // is rmid present?
             if (ActivationLibrary.rmidRunning(port)) {
+                /**
+                 * We need to set the java.rmi.activation.port value as the
+                 * activation system will use the property to determine the
+                 * port #.  The activation system will use this value if set.
+                 * If it isn't set, the activation system will set it to an
+                 * incorrect value.
+                 */
+                System.setProperty("java.rmi.activation.port", Integer.toString(port));
                 mesg("finished starting rmid.");
                 return;
             }
@@ -252,10 +259,6 @@
      * Shutdown does not nullify possible references to the rmid
      * process object (destroy does though).
      */
-    public static void shutdown() {
-        shutdown(TestLibrary.RMID_PORT);
-    }
-
     public static void shutdown(int port) {
 
         try {
@@ -297,9 +300,7 @@
      * if rmid is a child process of the current VM.
      */
     public void destroy() {
-
-        // attempt graceful shutdown of the activation system on
-        // TestLibrary.RMID_PORT
+        // attempt graceful shutdown of the activation system
         shutdown(port);
 
         if (vm != null) {
@@ -324,4 +325,6 @@
             vm = null;
         }
     }
+
+    public int getPort() {return port;}
 }
--- a/test/java/rmi/testlibrary/RegistryRunner.java	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/testlibrary/RegistryRunner.java	Wed Jun 12 11:48:21 2013 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1999, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1999, 2012, 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
@@ -56,12 +56,13 @@
      * Request that the registry process exit and handle
      * related exceptions.
      */
-    public static void requestExit() {
+    public static void requestExit(int port) {
+
         try {
             RemoteExiter exiter =
                 (RemoteExiter)
                 Naming.lookup("rmi://localhost:" +
-                              TestLibrary.REGISTRY_PORT +
+                              port +
                               "/RemoteExiter");
             try {
                 exiter.exit();
@@ -84,7 +85,7 @@
                 System.err.println("Usage: <port>");
                 System.exit(0);
             }
-            int port = TestLibrary.REGISTRY_PORT;
+            int port = -1;
             try {
                 port = Integer.parseInt(args[0]);
             } catch (NumberFormatException nfe) {
--- a/test/java/rmi/testlibrary/StreamPipe.java	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/testlibrary/StreamPipe.java	Wed Jun 12 11:48:21 2013 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1998, 1999, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1998, 2012, 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
--- a/test/java/rmi/testlibrary/TestLibrary.java	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/testlibrary/TestLibrary.java	Wed Jun 12 11:48:21 2013 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1998, 2003, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1998, 2012, 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
@@ -36,37 +36,62 @@
  * not make use of packages.
  */
 
+import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;
-import java.io.OutputStream;
 import java.io.PrintStream;
-import java.net.URL;
 import java.net.MalformedURLException;
-import java.rmi.activation.Activatable;
-import java.rmi.activation.ActivationID;
+import java.net.ServerSocket;
+import java.net.URL;
 import java.rmi.NoSuchObjectException;
+import java.rmi.Remote;
+import java.rmi.RemoteException;
+import java.rmi.registry.LocateRegistry;
 import java.rmi.registry.Registry;
-import java.rmi.Remote;
+import java.rmi.server.RemoteRef;
 import java.rmi.server.UnicastRemoteObject;
 import java.util.Enumeration;
-import java.util.Hashtable;
 import java.util.Properties;
-import java.io.ByteArrayOutputStream;
-import java.security.AccessController;
-import java.security.PrivilegedAction;
+import sun.rmi.registry.RegistryImpl;
+import sun.rmi.server.UnicastServerRef;
+import sun.rmi.transport.Endpoint;
+import sun.rmi.transport.LiveRef;
+import sun.rmi.transport.tcp.TCPEndpoint;
 
 /**
  * Class of utility/library methods (i.e. procedures) that assist with
  * the writing and maintainance of rmi regression tests.
  */
 public class TestLibrary {
-
-    /** standard test port number for registry */
-    public final static int REGISTRY_PORT = 2006;
-    /** port for rmid necessary: not used to actually start rmid */
-    public final static int RMID_PORT = 1098;
+    /**
+     *                       IMPORTANT!
+     *
+     * RMI tests are run concurrently and port conflicts result when a single
+     * port number is used by multiple tests.  When needing a port, use
+     * getUnusedRandomPort() wherever possible.  If getUnusedRandomPort() cannot
+     * be used, reserve and specify a port to use for your test here.   This
+     * will ensure there are no port conflicts amongst the RMI tests.  The
+     * port numbers specified here may also be specified in the respective
+     * tests.  Do not change the reserved port numbers here without also
+     * changing the port numbers in the respective tests.
+     *
+     * When needing an instance of the RMIRegistry, use
+     * createRegistryOnUnusedPort wherever possible to prevent port conflicts.
+     *
+     * Reserved port range: FIXED_PORT_MIN to FIXED_PORT_MAX (inclusive) for
+     * tests which cannot use a random port.  If new fixed ports are added below
+     * FIXED_PORT_MIN or above FIXED_PORT_MAX, then adjust
+     * FIXED_PORT_MIN/MAX appropriately.
+     */
+    public final static int FIXED_PORT_MIN = 64001;
+    public final static int FIXED_PORT_MAX = 64010;
+    public final static int RMIDVIAINHERITEDCHANNEL_ACTIVATION_PORT = 64001;
+    public final static int RMIDVIAINHERITEDCHANNEL_REGISTRY_PORT = 64002;
+    public final static int INHERITEDCHANNELNOTSERVERSOCKET_ACTIVATION_PORT = 64003;
+    public final static int INHERITEDCHANNELNOTSERVERSOCKET_REGISTRY_PORT = 64004;
+    public final static int READTEST_REGISTRY_PORT = 64005;
 
     static void mesg(Object mesg) {
         System.err.println("TEST_LIBRARY: " + mesg.toString());
@@ -340,6 +365,83 @@
     }
 
     /**
+     * Creates an RMI {@link Registry} on a random, un-reserved port.
+     *
+     * @returns an RMI Registry, using a random port.
+     * @throws RemoteException if there was a problem creating a Registry.
+     */
+    public static Registry createRegistryOnUnusedPort() throws RemoteException {
+        return LocateRegistry.createRegistry(getUnusedRandomPort());
+    }
+
+    /**
+     * Returns the port number the RMI {@link Registry} is running on.
+     *
+     * @param registry the registry to find the port of.
+     * @return the port number the registry is using.
+     * @throws RuntimeException if there was a problem getting the port number.
+     */
+    public static int getRegistryPort(Registry registry) {
+        int port = -1;
+
+        try {
+            RemoteRef remoteRef = ((RegistryImpl)registry).getRef();
+            LiveRef liveRef = ((UnicastServerRef)remoteRef).getLiveRef();
+            Endpoint endpoint = liveRef.getChannel().getEndpoint();
+            TCPEndpoint tcpEndpoint = (TCPEndpoint) endpoint;
+            port = tcpEndpoint.getPort();
+        } catch (Exception ex) {
+            throw new RuntimeException("Error getting registry port.", ex);
+        }
+
+        return port;
+    }
+
+    /**
+     * Returns an unused random port number which is not a reserved port.  Will
+     * try up to 10 times to get a random port before giving up and throwing a
+     * RuntimeException.
+     *
+     * @return an unused random port number.
+     * @throws RuntimeException if there was a problem getting a port.
+     */
+    public static int getUnusedRandomPort() {
+        int numTries = 0;
+        int unusedRandomPort = FIXED_PORT_MIN;
+        Exception ex = null;
+
+        while (numTries++ < 10) {
+            ex = null; //reset
+
+            try (ServerSocket ss = new ServerSocket(0)) {
+                unusedRandomPort = ss.getLocalPort();
+            } catch (Exception e) {
+                ex = e;
+            }
+
+            if (!isReservedPort(unusedRandomPort)) {
+                return unusedRandomPort;
+            }
+        }
+
+        // If we're here, then either an exception was thrown or the port is
+        // a reserved port.
+        throw new RuntimeException("Error getting unused random port.", ex);
+    }
+
+    /**
+     * Determines if a port is one of the reserved port numbers.
+     *
+     * @param port the port to test.
+     * @return {@code true} if the port is a reserved port, otherwise
+     *         {@code false}.
+     */
+    public static boolean isReservedPort(int port) {
+        return ((port >= FIXED_PORT_MIN) && (port <= FIXED_PORT_MAX) ||
+                (port == 1099));
+    }
+
+    /**
      * Method to capture the stack trace of an exception and return it
      * as a string.
      */
--- a/test/java/rmi/transport/checkFQDN/CheckFQDN.java	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/transport/checkFQDN/CheckFQDN.java	Wed Jun 12 11:48:21 2013 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1998, 2008, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1998, 2012, 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
@@ -34,6 +34,7 @@
  *
  * @library ../../testlibrary
  * @build CheckFQDN CheckFQDNClient CheckFQDN_Stub TellServerName
+ * @build TestLibrary
  * @run main/othervm/timeout=120 CheckFQDN
  */
 
@@ -63,7 +64,7 @@
  */
 public class CheckFQDN extends UnicastRemoteObject
     implements TellServerName {
-
+    public static int REGISTRY_PORT =-1;
     static String propertyBeingTested = null;
     static String propertyBeingTestedValue = null;
 
@@ -77,8 +78,8 @@
             System.err.println
                 ("\nRegression test for bug/rfe 4115683\n");
 
-            Registry registry = java.rmi.registry.LocateRegistry.
-                createRegistry(TestLibrary.REGISTRY_PORT);
+            Registry registry = TestLibrary.createRegistryOnUnusedPort();
+            REGISTRY_PORT = TestLibrary.getRegistryPort(registry);
             registry.bind("CheckFQDN", checkFQDN);
 
             /* test the host name scheme in different environments.*/
@@ -117,7 +118,9 @@
             JavaVM jvm = new JavaVM("CheckFQDNClient",
                                     propOption + property +
                                     equal +
-                                    propertyValue + extraProp,
+                                    propertyValue + extraProp +
+                                    " -Drmi.registry.port=" +
+                                    REGISTRY_PORT,
                                     "");
 
             propertyBeingTested=property;
--- a/test/java/rmi/transport/checkFQDN/CheckFQDNClient.java	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/transport/checkFQDN/CheckFQDNClient.java	Wed Jun 12 11:48:21 2013 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1998, 2008, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1998, 2012, 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
@@ -58,8 +58,9 @@
             System.err.println("Client host name: " +
                                hostname);
 
+            int registryPort = Integer.parseInt(System.getProperty("rmi.registry.port"));
             tell = (TellServerName) Naming.lookup("rmi://:" +
-                                                  TestLibrary.REGISTRY_PORT
+                                                  registryPort
                                                   + "/CheckFQDN");
             tell.tellServerName(hostname);
             System.err.println("client has exited");
--- a/test/java/rmi/transport/checkLeaseInfoLeak/CheckLeaseLeak.java	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/transport/checkLeaseInfoLeak/CheckLeaseLeak.java	Wed Jun 12 11:48:21 2013 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1998, 2008, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1998, 2012, 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
@@ -29,6 +29,7 @@
  *
  * @library ../../testlibrary
  * @build CheckLeaseLeak CheckLeaseLeak_Stub LeaseLeakClient LeaseLeak
+ * @build TestLibrary
  * @run main/othervm/timeout=240 CheckLeaseLeak
  *
  */
@@ -57,7 +58,6 @@
 import java.rmi.registry.*;
 
 public class CheckLeaseLeak extends UnicastRemoteObject implements LeaseLeak {
-
     public CheckLeaseLeak() throws RemoteException { }
     public void ping () throws RemoteException { }
 
@@ -87,8 +87,8 @@
 
         try {
             Registry registry =
-                java.rmi.registry.LocateRegistry.
-                    createRegistry(TestLibrary.REGISTRY_PORT);
+                TestLibrary.createRegistryOnUnusedPort();
+            int registryPort = TestLibrary.getRegistryPort(registry);
 
             leakServer = new CheckLeaseLeak();
             registry.rebind("/LeaseLeak", leakServer);
@@ -99,7 +99,10 @@
 
                 JavaVM jvm = new JavaVM("LeaseLeakClient",
                                         " -Djava.security.policy=" +
-                                        TestParams.defaultPolicy, "");
+                                        TestParams.defaultPolicy +
+                                        " -Drmi.registry.port=" +
+                                        registryPort,
+                                        "");
                 jvm.start();
 
                 if (jvm.getVM().waitFor() == 1 ) {
--- a/test/java/rmi/transport/checkLeaseInfoLeak/LeaseLeakClient.java	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/transport/checkLeaseInfoLeak/LeaseLeakClient.java	Wed Jun 12 11:48:21 2013 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1998, 2008, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1998, 2012, 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
@@ -31,11 +31,11 @@
 
         try {
             LeaseLeak leaseLeak = null;
+            int registryPort = Integer.parseInt(System.getProperty("rmi.registry.port"));
 
             // put a reference on a remote object.
             Registry registry =
-                java.rmi.registry.LocateRegistry.getRegistry(
-                    TestLibrary.REGISTRY_PORT);
+                java.rmi.registry.LocateRegistry.getRegistry(registryPort);
             leaseLeak = (LeaseLeak) registry.lookup("/LeaseLeak");
             leaseLeak.ping();
 
--- a/test/java/rmi/transport/checkLeaseInfoLeak/security.policy	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/transport/checkLeaseInfoLeak/security.policy	Wed Jun 12 11:48:21 2013 +0100
@@ -19,6 +19,7 @@
   permission java.util.PropertyPermission "user.dir", "read";
   permission java.util.PropertyPermission "java.home", "read";
 
+  permission java.util.PropertyPermission "rmi.registry.port", "read";
   permission java.util.PropertyPermission "java.security.policy", "read";
   permission java.util.PropertyPermission "java.security.manager", "read";
 
--- a/test/java/rmi/transport/closeServerSocket/CloseServerSocket.java	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/transport/closeServerSocket/CloseServerSocket.java	Wed Jun 12 11:48:21 2013 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2005, 2008, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2005, 2012, 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
@@ -30,7 +30,8 @@
  * the local port is released).
  * @author Peter Jones
  *
- * @build CloseServerSocket
+ * @library ../../testlibrary
+ * @build CloseServerSocket TestLibrary
  * @run main/othervm CloseServerSocket
  */
 
@@ -44,8 +45,7 @@
 import java.rmi.server.UnicastRemoteObject;
 
 public class CloseServerSocket implements Remote {
-
-    private static final int PORT = 2020;
+    private static final int PORT = TestLibrary.getUnusedRandomPort();
 
     private CloseServerSocket() { }
 
@@ -88,7 +88,7 @@
     }
 
     private static void verifyPortFree(int port) throws IOException {
-        ServerSocket ss = new ServerSocket(PORT);
+        ServerSocket ss = new ServerSocket(port);
         ss.close();
         System.err.println("- port " + port + " is free");
     }
--- a/test/java/rmi/transport/dgcDeadLock/DGCDeadLock.java	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/transport/dgcDeadLock/DGCDeadLock.java	Wed Jun 12 11:48:21 2013 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1998, 2008, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1998, 2012, 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
@@ -32,6 +32,7 @@
  * @build Test
  * @build TestImpl
  * @build TestImpl_Stub
+ * @build TestLibrary
  * @run main/othervm/policy=security.policy/timeout=360 DGCDeadLock
  */
 
@@ -53,7 +54,7 @@
 import java.io.*;
 
 public class DGCDeadLock implements Runnable {
-
+    private static final int REGISTRY_PORT = TestLibrary.getUnusedRandomPort();
     final static public int HOLD_TARGET_TIME = 25000;
     public static int TEST_FAIL_TIME = HOLD_TARGET_TIME + 30000;
     public static boolean finished = false;
@@ -75,7 +76,9 @@
                 TestParams.defaultPolicy +
                 " -Djava.rmi.dgc.leaseValue=500000" +
                 "  -Dsun.rmi.dgc.checkInterval=" +
-                (HOLD_TARGET_TIME - 5000) + "";
+                (HOLD_TARGET_TIME - 5000) +
+                "   -Drmi.registry.port=" + REGISTRY_PORT +
+                "" ;
 
             testImplVM = new JavaVM("TestImpl", options, "");
             testImplVM.start();
@@ -112,7 +115,7 @@
 
             // create a test client
             Test foo = (Test) Naming.lookup("rmi://:" +
-                                            TestLibrary.REGISTRY_PORT +
+                                            REGISTRY_PORT +
                                             "/Foo");
             echo = foo.echo("Hello world");
             System.err.println("Test object created.");
@@ -131,7 +134,7 @@
 
             //import "Bar"
             Test bar = (Test) Naming.lookup("rmi://:" +
-                                            TestLibrary.REGISTRY_PORT +
+                                            REGISTRY_PORT +
                                             "/Bar");
 
             /* infinite loop to show the liveness of Client,
--- a/test/java/rmi/transport/dgcDeadLock/TestImpl.java	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/transport/dgcDeadLock/TestImpl.java	Wed Jun 12 11:48:21 2013 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1998, 2008, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1998, 2012, 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
@@ -36,7 +36,6 @@
 
 public class TestImpl extends UnicastRemoteObject
     implements Test {
-
     static Thread locker = null;
     static TestImpl foo = null;
     static TestImpl bar = null;
@@ -57,20 +56,21 @@
         Registry registry = null;
 
         try {
+            int registryPort = Integer.parseInt(System.getProperty("rmi.registry.port"));
             registry = java.rmi.registry.LocateRegistry.
-                createRegistry(TestLibrary.REGISTRY_PORT);
+                createRegistry(registryPort);
 
             //export "Foo"
             foo = new TestImpl();
             Naming.rebind("rmi://:" +
-                          TestLibrary.REGISTRY_PORT
+                          registryPort
                           + "/Foo", foo);
 
             try {
                 //export "Bar" after leases have been expired.
                 bar = new TestImpl();
                 Naming.rebind("rmi://localhost:" +
-                              TestLibrary.REGISTRY_PORT
+                              registryPort
                               + "/Bar", bar);
             } catch (Exception e) {
                 throw new RemoteException(e.getMessage());
--- a/test/java/rmi/transport/handshakeFailure/HandshakeFailure.java	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/transport/handshakeFailure/HandshakeFailure.java	Wed Jun 12 11:48:21 2013 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2001, 2008, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2001, 2012, 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
@@ -30,7 +30,8 @@
  * java.rmi.ConnectException or ConnectIOException, not a MarshalException.
  * @author Peter Jones
  *
- * @build HandshakeFailure
+ * @library ../../testlibrary
+ * @build HandshakeFailure TestLibrary
  * @run main/othervm HandshakeFailure
  */
 
@@ -44,7 +45,7 @@
 
 public class HandshakeFailure {
 
-    private static final int PORT = 2020;
+    private static final int PORT = TestLibrary.getUnusedRandomPort();
     private static final int TIMEOUT = 10000;
 
     public static void main(String[] args) throws Exception {
--- a/test/java/rmi/transport/handshakeTimeout/HandshakeTimeout.java	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/transport/handshakeTimeout/HandshakeTimeout.java	Wed Jun 12 11:48:21 2013 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2001, 2008, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2001, 2012, 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
@@ -33,7 +33,8 @@
  * this point (because no data for the invocation has yet been written).
  * @author Peter Jones
  *
- * @build HandshakeTimeout
+ * @library ../../testlibrary
+ * @build HandshakeTimeout TestLibrary
  * @run main/othervm HandshakeTimeout
  */
 
@@ -46,7 +47,7 @@
 
 public class HandshakeTimeout {
 
-    private static final int PORT = 2020;
+    private static final int PORT = TestLibrary.getUnusedRandomPort();
     private static final int TIMEOUT = 10000;
 
     public static void main(String[] args) throws Exception {
--- a/test/java/rmi/transport/httpSocket/HttpSocketTest.java	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/transport/httpSocket/HttpSocketTest.java	Wed Jun 12 11:48:21 2013 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1999, 2008, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1999, 2012, 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
@@ -27,7 +27,7 @@
  * @author Dana Burns
  *
  * @library ../../testlibrary
- * @build HttpSocketTest HttpSocketTest_Stub
+ * @build HttpSocketTest HttpSocketTest_Stub TestLibrary
  * @run main/othervm/policy=security.policy HttpSocketTest
  */
 
@@ -56,10 +56,7 @@
 public class HttpSocketTest extends UnicastRemoteObject
     implements MyRemoteInterface
 {
-
     private static final String NAME = "HttpSocketTest";
-    private static final String REGNAME =
-        "//:" + TestLibrary.REGISTRY_PORT + "/" + NAME;
 
     public HttpSocketTest() throws RemoteException{}
 
@@ -76,21 +73,20 @@
         // Set the socket factory.
         System.err.println("installing socket factory");
         RMISocketFactory.setSocketFactory(new RMIHttpToPortSocketFactory());
+        int registryPort = -1;
 
         try {
-
             System.err.println("Starting registry");
-            registry = LocateRegistry.createRegistry(TestLibrary.REGISTRY_PORT);
-
+            registry = TestLibrary.createRegistryOnUnusedPort();
+            registryPort = TestLibrary.getRegistryPort(registry);
         } catch (Exception e) {
             TestLibrary.bomb(e);
         }
 
         try {
-
             registry.rebind( NAME, new HttpSocketTest() );
             MyRemoteInterface httpTest =
-                (MyRemoteInterface)Naming.lookup( REGNAME );
+                (MyRemoteInterface)Naming.lookup("//:" + registryPort + "/" + NAME);
             httpTest.setRemoteObject( new HttpSocketTest() );
             Remote r = httpTest.getRemoteObject();
 
--- a/test/java/rmi/transport/httpSocket/security.policy	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/transport/httpSocket/security.policy	Wed Jun 12 11:48:21 2013 +0100
@@ -4,6 +4,10 @@
 
 grant {
     permission java.net.SocketPermission "*:1024-", "accept,connect,listen";
+    permission java.lang.RuntimePermission "accessClassInPackage.sun.rmi.registry";
+    permission java.lang.RuntimePermission "accessClassInPackage.sun.rmi.server";
+    permission java.lang.RuntimePermission "accessClassInPackage.sun.rmi.transport";
     permission java.lang.RuntimePermission "accessClassInPackage.sun.rmi.transport.proxy";
+    permission java.lang.RuntimePermission "accessClassInPackage.sun.rmi.transport.tcp";
     permission java.lang.RuntimePermission "setFactory";
 };
--- a/test/java/rmi/transport/pinClientSocketFactory/PinClientSocketFactory.java	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/transport/pinClientSocketFactory/PinClientSocketFactory.java	Wed Jun 12 11:48:21 2013 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2005, 2008, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2005, 2012, 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
@@ -31,6 +31,8 @@
  * should become unreachable too (through the RMI implementation).
  * @author Peter Jones
  *
+ * @library ../../testlibrary
+ * @build TestLibrary
  * @run main/othervm -Dsun.rmi.transport.connectionTimeout=2000
  *     PinClientSocketFactory
  */
@@ -56,7 +58,7 @@
 
 public class PinClientSocketFactory {
 
-    private static final int PORT = 2345;
+    private static final int PORT = TestLibrary.getUnusedRandomPort();
     private static final int SESSIONS = 50;
 
     public interface Factory extends Remote {
--- a/test/java/rmi/transport/rapidExportUnexport/RapidExportUnexport.java	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/transport/rapidExportUnexport/RapidExportUnexport.java	Wed Jun 12 11:48:21 2013 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2005, 2008, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2005, 2012, 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
@@ -31,7 +31,8 @@
  * procedure (which sleeps 10 seconds after 10 rapid failures).
  * @author Peter Jones
  *
- * @build RapidExportUnexport
+ * @library ../../testlibrary
+ * @build TestLibrary RapidExportUnexport
  * @run main/othervm RapidExportUnexport
  */
 
@@ -39,9 +40,7 @@
 import java.rmi.server.UnicastRemoteObject;
 
 public class RapidExportUnexport {
-
-    private static final int PORT = 2055;
-
+    private static final int PORT = TestLibrary.getUnusedRandomPort();
     private static final int REPS = 100;
     private static final long TIMEOUT = 60000;
 
--- a/test/java/rmi/transport/reuseDefaultPort/ReuseDefaultPort.java	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/java/rmi/transport/reuseDefaultPort/ReuseDefaultPort.java	Wed Jun 12 11:48:21 2013 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2005, 2008, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2005, 2012, 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
@@ -33,7 +33,8 @@
  * continue to work because existing applications might depend on it.
  * @author Peter Jones
  *
- * @build ReuseDefaultPort
+ * @library ../../testlibrary
+ * @build ReuseDefaultPort TestLibrary
  * @run main/othervm ReuseDefaultPort
  */
 
@@ -48,7 +49,7 @@
 
 public class ReuseDefaultPort implements Remote {
 
-    private static final int PORT = 2223;
+    private static final int PORT = TestLibrary.getUnusedRandomPort();
 
     private ReuseDefaultPort() { }
 
--- a/test/sun/rmi/rmic/newrmic/equivalence/AppleUserImpl.java	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/sun/rmi/rmic/newrmic/equivalence/AppleUserImpl.java	Wed Jun 12 11:48:21 2013 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2003, 2012, 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
@@ -25,6 +25,7 @@
 import java.rmi.Naming;
 import java.rmi.server.UnicastRemoteObject;
 import java.rmi.registry.LocateRegistry;
+import java.rmi.registry.Registry;
 import java.util.Random;
 import java.util.ArrayList;
 import java.util.Date;
@@ -249,11 +250,12 @@
         }
 
         synchronized (user) {
+            int port = -1;
             // create new registry and bind new AppleUserImpl in registry
             try {
-                LocateRegistry.createRegistry(1099); //TestLibrary.REGISTRY_PORT);
-                Naming.rebind("rmi://localhost:1099/AppleUser",user);
-                              //TestLibrary.REGISTRY_PORT + "/AppleUser", user);
+                Registry registry = TestLibrary.createRegistryOnUnusedPort();
+                port = TestLibrary.getRegistryPort(registry);
+                Naming.rebind("rmi://localhost:" + port + "/AppleUser",user);
             } catch (RemoteException e) {
                 //TestLibrary.bomb("Failed to bind AppleUser", e);
             } catch (java.net.MalformedURLException e) {
@@ -263,10 +265,9 @@
             // start the other server if available
             try {
                 Class app = Class.forName("ApplicationServer");
-                server = new Thread((Runnable) app.newInstance());
-                logger.log(Level.INFO, "Starting application server " +
-                    "in same process");
-                server.start();
+                java.lang.reflect.Constructor appConstructor =
+                        app.getDeclaredConstructor(new Class[] {Integer.TYPE});
+                server = new Thread((Runnable) appConstructor.newInstance(port));
             } catch (ClassNotFoundException e) {
                 // assume the other server is running in a separate process
                 logger.log(Level.INFO, "Application server must be " +
--- a/test/sun/rmi/rmic/newrmic/equivalence/run.sh	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/sun/rmi/rmic/newrmic/equivalence/run.sh	Wed Jun 12 11:48:21 2013 +0100
@@ -1,5 +1,5 @@
 #
-# Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
+# Copyright (c) 2003, 2012, 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
@@ -26,6 +26,8 @@
 # @summary This test verifies that the new implementation of rmic
 # generates equivalent classes as the old implementation, for a set
 # of sample input classes.
+# @library ../../../../../java/rmi/testlibrary
+# @build TestLibrary
 # @author Peter Jones
 #
 # @build AgentServerImpl
--- a/test/sun/rmi/runtime/Log/6409194/NoConsoleOutput.java	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/sun/rmi/runtime/Log/6409194/NoConsoleOutput.java	Wed Jun 12 11:48:21 2013 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2006, 2012, 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
@@ -87,7 +87,6 @@
     }
 
     public static class DoRMIStuff {
-        private static final int PORT = 2020;
         private interface Foo extends Remote {
             Object echo(Object obj) throws RemoteException;
         }
@@ -96,8 +95,9 @@
             public Object echo(Object obj) { return obj; }
         }
         public static void main(String[] args) throws Exception {
-            LocateRegistry.createRegistry(PORT);
-            Registry reg = LocateRegistry.getRegistry("", PORT);
+            Registry registry = TestLibrary.createRegistryOnUnusedPort();
+            int registryPort = TestLibrary.getRegistryPort(registry);
+            Registry reg = LocateRegistry.getRegistry("", registryPort);
             FooImpl fooimpl = new FooImpl();
             UnicastRemoteObject.exportObject(fooimpl, 0);
             reg.rebind("foo", fooimpl);
--- a/test/sun/rmi/runtime/Log/checkLogging/CheckLogging.java	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/sun/rmi/runtime/Log/checkLogging/CheckLogging.java	Wed Jun 12 11:48:21 2013 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2001, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2001, 2012, 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
@@ -77,8 +77,9 @@
  * logger output is non-null.
  */
 public class CheckLogging {
-    private static final String LOCATION =
-        "rmi://localhost:" + TestLibrary.REGISTRY_PORT + "/";
+    private static int REGISTRY_PORT = -1;
+    private static String LOCATION;
+
     private static final ByteArrayOutputStream clientCallOut =
         new ByteArrayOutputStream();
 
@@ -100,7 +101,9 @@
     private static Registry registry;
     static {
         try {
-            registry = LocateRegistry.createRegistry(TestLibrary.REGISTRY_PORT);
+            registry = TestLibrary.createRegistryOnUnusedPort();
+            REGISTRY_PORT = TestLibrary.getRegistryPort(registry);
+            LOCATION = "rmi://localhost:" + REGISTRY_PORT + "/";
         } catch (Exception e) {
             TestLibrary.bomb("could not create registry");
         }
--- a/test/sun/rmi/transport/proxy/EagerHttpFallback.java	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/sun/rmi/transport/proxy/EagerHttpFallback.java	Wed Jun 12 11:48:21 2013 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2002, 2012, 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
@@ -25,6 +25,8 @@
  * @bug 4290727
  * @summary Verify that ConnectException will trigger HTTP fallback if
  *          sun.rmi.transport.proxy.eagerHttpFallback system property is set.
+ * @library ../../../../java/rmi/testlibrary
+ * @build TestLibrary
  * @run main/othervm EagerHttpFallback
  */
 
@@ -33,8 +35,8 @@
 
 public class EagerHttpFallback {
 
-    static final int INITIAL_PORT = 7070;
-    static final int FALLBACK_PORT = 7071;
+    static final int INITIAL_PORT = TestLibrary.getUnusedRandomPort();
+    static final int FALLBACK_PORT = TestLibrary.getUnusedRandomPort();
 
     public static void main(String[] args) throws Exception {
         System.setProperty("http.proxyHost", "127.0.0.1");
--- a/test/sun/rmi/transport/tcp/DeadCachedConnection.java	Wed Jun 12 11:46:50 2013 +0100
+++ b/test/sun/rmi/transport/tcp/DeadCachedConnection.java	Wed Jun 12 11:48:21 2013 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1998, 2004, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1998, 2012, 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
@@ -58,7 +58,7 @@
 import java.rmi.server.*;
 
 public class DeadCachedConnection {
-    static public final int regport = 17340;
+    static public final int regport = TestLibrary.getUnusedRandomPort();
 
     static public void main(String[] argv)
         throws Exception {