Class files downloaded from the network and the implementation of
Abstract: The class files downloaded from the network and the implementation of
</ Td> </ tr> <tr> <td height="35" valign="top" class="ArticleTeitle">
Import java.net .*; import java.lang.reflect .*; public class MyURL (static public void main (String args []) throws Exception (URL myurl [] = (/ / type of document addresses, in order to verify, please release to the web server such as: http://127.0.0.1:8080/cwbwebhome/ new URL ( "file: / / / C: / CH3/ClassLoader/web /"), new URL ( "http://127.0.0.1: 8080/cwbwebhome / ")); URLClassLoader x = new URLClassLoader (myurl); Class c = x.loadClass (" TestURL ");// loading class files TestURL.class Class getArg1 [] = ((new String [1]) . getClass ()) / / main () method of the main parameters of type Method m = c.getMethod ( "main", getArg1); / / Get the main method my1 String [] = ( "arg1 passed," "arg2 passed" ) / / Call the main method of the parameters Object myarg1 [] = () my1; m.invoke (null, myarg1) / / call TestURL main method of static Object ob = c.newInstance (); / / object of a TestURL Class arg2 [] = null; Method m2 = c.getMethod (the "tt", arg2); / / call TestURL category of non-parametric method tt () m2.invoke (ob); Class arg3 [] = ((new String ()). getClass (), int.class); Method m3 = c.getMethod (the "tt", arg3); Object myarg2 [] = ( "Arg1" new Integer (100)); m3.invoke (ob, myarg2) / / call TestURL category method tt (String s, int i))) Running Results (jdk1.5 through): C: \ java> java MyURL From Main arg1 passed arg2 passed From tt without args Fron tt with args Arg1 100 C: \ java> of TestURL.java import java.io. *; public class TestURL (static public void main (String args []) throws Exception (System.out.println ( "From Main"); System. out.println (args [0]); System.out.println (args [1]);) public void tt () throws Exception (System.out.println ( "From tt without args"); byte b = [] "How are you!." getBytes ( "UTF8"); FileOutputStream f = new FileOutputStream ( "ssss.txt"); f.write (b);) public void tt (String s, int i) (System.out. println ( "Fron tt with args"); System.out.println (s); System.out.println (i);))
</ Td> </ tr> <tr>
↑ Back
Tags: Class, java class, network






