JAVA加载网络 jar 包 运行类例子

本人最近研究java 加载网络类的方法,做了个简单的例子,我把打包的jar上传到附件了,TTT.java是我的测试类,Test是我的打包类的源代码。
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Properties;


public class Test {
public void setName(){
System.out.println(System.getProperty("USERTIME"));
}

public static void main(String args[]){
System.out.println(new Test().getEnv("USRTIME"));
}

public String getWindowName(){
return System.getProperty("os.name");
}

public static String getEnv(String envKey) {
Properties prop = new Properties();
try {
String OS = System.getProperty("os.name").toLowerCase();
Process p = null;
//get system environment
if (OS.indexOf("windows") > -1) {
p = Runtime.getRuntime().exec("cmd /c set");
}
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
int i = line.indexOf("=");
if (i > -1) {
String key = line.substring(0, i);
String value = line.substring(i + 1);
System.out.println(key+"::::"+value);
prop.setProperty(key, value);
}
}

return prop.getProperty(envKey);
} catch (Exception e) {
return null;
}
}

}


TTT.java 代码:
import java.lang.reflect.Method;
import java.net.*;
public class TTT {
public static void main(String args[]){
try{
URL url = new URL("http://192.168.2.17:8080/webgame/test.jar");
URLClassLoader myClassLoader = new URLClassLoader( new URL[] { url } );
Class myClass = myClassLoader.loadClass("Test");
Object test=myClass.newInstance();
Method m = test.getClass().getMethod("getEnv", String.class);
System.out.println(m.invoke(test, "PATH"));
}catch(Exception e){
e.printStackTrace();
}

}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值