您好,我正在嘗試從其他計算機使用LAN線訪問java類文件。 我必須從用戶文件夾的IP 192.168.0.1加載測試儀類文件!請幫助我如何做到這一點,我的代碼如下所述。如何使用其他計算機加載類文件
import java.net.*;
public class MyLoader {
public static void main (String argv[]) throws Exception {
URLClassLoader loader = new URLClassLoader(new URL[] { new URL("\\192.168.0.1\\user") });//ip of other computer where from user folder i use Tester class
// Load class from class loader. argv[0] is the name of the class to be loaded
Class c = loader.loadClass (argv[0]);
// Create an instance of the class just loaded
Object o = c.newInstance();
}
}
在運行時我使用[java MyLoader Tester]。 這裏我的測試Java文件
public class Tester {
public Tester() {
System.out.println ("Hello there");
}
public static void main(String argv[]) {
System.out.println("Network Class Loaders");
}
}
我得到這個異常錯誤
Exception in thread "main" java.net.MalformedURLException: no protocol: \192.168
.0.1\user
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at MyLoader.main(MyLoader.java:7)
(未測試)除非有例外情況,否則請嘗試在URL中使用一個方案,比如說'file:// 192.168.0.1/user' – 2012-03-04 11:14:13