Java动态调用jar包

时间:2023-01-29 19:42:09

//-------demo类---

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;

public class Test {
 ClassLoader loader;
 public boolean load(String jar){
  File f=new File(jar);
  if(!(f.exists())){
   System.out.println("jar file /""+jar+"/"not exist!");
   return false;
  }
  try {
   loader=new URLClassLoader(new URL[]{f.toURI().toURL()},this.getClass().getClassLoader());
  } catch (MalformedURLException e) {
   e.printStackTrace();
   return false;
  } 
  return true;
 }
 public Class findClass(String className){
  try {
   return loader.loadClass(className);
  } catch (ClassNotFoundException e) {
   System.out.println("class /""+className+"/" not exist!");
  }
  return null;
 }
 public Object invokeMethodOfClassInExtJarFile(String jarName,String classFullName,String MethodName,Class paramClasses[],Object paramValue[]){
  if(!load(jarName)){
   System.out.println("Exception in Load Jar file!");
   return null;
  }
  Class clazz=findClass(classFullName);
   Object obj;
   try {
    obj = clazz.newInstance();
    Object result=clazz.getMethod(MethodName,paramClasses).invoke(obj,paramValue);
    return result;
   } catch (Exception e) {
    e.printStackTrace();
   }
   return null;
 }
 public static void main(String args[]){
  String stra="MaGic_VV";
  String strb="Grevin";
  Object paramValue[]={stra,strb};
  Class paramClass[]={String.class,String.class};
  new Test().invokeMethodOfClassInExtJarFile("C://Documents and Settings//Administrator//workspace//Discuz//2.jar","Nes.Discuz","kick",paramClass,paramValue);
 }
}

//-------------------------------------------------------------------------------------------------------------------------

//-----以下为jar包内的内容--------

package Nes;
import java.io.*;
import java.net.*;
import java.util.List;
import java.util.Map;

import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.params.HttpMethodParams;


public class Discuz{
 public static void main(String args[]) throws IOException{
  //sendGet("http://www.discuz.net/","t7Hq_auth=6877ZJE2e3b8Ll1PzeQaWRwsco8dCUsTWxdNFJlEK9ESCBYzONr7lYHZ9gzlb%2F6fotKcCgWnpY6j%2B8YSx5hgpbU0PfKw; path=/; domain=.discuz.net; httponly","D://3.html");
  String cookie=sendPost("http://www.discuz.net/member.php?mod=logging&action=login&loginsubmit=yes&floatlogin=yes&inajax=1/","formhash=8ee613bf&referer=http%3A%2F%2Fwww.discuz.net%2F&username=kivcare&password=superbb&questionid=0&answer=","D://2.html");
  System.out.print(cookie);
  sendGet("http://www.discuz.net/thread-1769265-1-1.html",cookie,"D://get.html");
  
 }
 public static String sendPost(String url,String params,String filepath) throws IOException{
  PrintWriter pw;  
  PrintWriter out = null;
  BufferedReader in = null;
  String result = "";
  //------------------------------------------------------------------
  //-------构造post方法并提交-----------------------------------------------------------
  URL realUrl = new URL(url);
  URLConnection conn = realUrl.openConnection();  
  conn.setRequestProperty("accept", "*/*");
  conn.setRequestProperty("connection", "Keep-Alive");
  conn.setRequestProperty("user-agent",
  "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; TheWorld)");
  conn.setDoOutput(true);
  conn.setDoInput(true);
  
  
  out = new PrintWriter(conn.getOutputStream());   
  out.print(params);  
  out.flush();
  in = new BufferedReader(
    new InputStreamReader(conn.getInputStream()));
  //-------获取所需cookie----------------------------------------------------------------
  Map<String, List<String>> CookieMap=conn.getHeaderFields();  
  String strCookie = null;
  List<String>CookieList=CookieMap.get("Set-Cookie");
  for(int i=0;i<CookieList.size();i++)
   if(CookieList.get(i).startsWith("t7Hq_auth"))
    strCookie=CookieList.get(i);
  //------------------------------------------------------------------------
  //--------将网页内容写入文件-----------------------------------------------------------------
  StringBuffer sbf = new StringBuffer();
  String line = null;
  while ((line = in.readLine()) != null)
  {
    sbf.append(line+"/r/n");
  }
  /** 回收资源 */
  in.close(); 
  pw=new PrintWriter(new FileWriter(filepath));
  pw.print(sbf.toString());
  return strCookie;

  
 }
 public static void sendGet(String url,String cookie,String filepath) throws IOException{
  PrintWriter pw; 
  HttpClient hc=new HttpClient();
  GetMethod getHttp=new GetMethod(url);
  getHttp.getParams().setContentCharset("UTF-8");
  //getHttp.setRequestHeader("Host", "www.discuz.net");
  getHttp.setRequestHeader("Accept","*/*");
  getHttp.setRequestHeader("UA-CPU"," x86");
  getHttp.setRequestHeader("Accept-Language", "zh-cn");
  getHttp.setRequestHeader("Referer","http://www.discuz.net/");
  getHttp.setRequestHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; TheWorld)");
  getHttp.setRequestHeader("Connection", "Keep-Alive");
  getHttp.setRequestHeader("Cookie",cookie);
  getHttp.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
  
  try {
   hc.executeMethod(getHttp);
   
  } catch (HttpException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  InputStream ins = getHttp.getResponseBodyAsStream();
  BufferedReader br = new BufferedReader(new InputStreamReader(ins,"GB2312"));
  StringBuffer sbf = new StringBuffer();
  String line = null;
  while ((line = br.readLine()) != null)
  {
    sbf.append(line+"/r/n");
  }
  /** 回收资源 */
  br.close();  
  pw=new PrintWriter(new FileWriter(filepath));
  pw.print(sbf.toString());
 }
 public void kick(String stra,String strb){
  System.out.println("OK,Test in process!"+"---"+stra+"and"+strb);
 }
 }