原文:http://hongyegu.iteye.com/blog/619147,謝謝!
import org.apache.cxf.tools.java2ws.JavaToWS; import net.bwda.service.Busi.webservice.FileMarkService; public class Java2WSDL { private Class<?> className; public String[] args1,args2,args3; /** * 構造函數 * @param args 要生成WSDL的Java類 */ public Java2WSDL(Class<?> className) { this.className = className; //根據Hello.class生成Hello.wsdl,生成的wsdl文件放在resource目錄下 args1=new String[]{"-wsdl","-d","./resource",this.className.getName()}; //根據Hello.class生成Hello.wsdl,生成的文件放在根目錄下的.example下 args2=new String[]{"-wsdl","-cp", "./example",this.className.getName()}; // 根據Hello.class生成wsdl文件,文件命名為myHello.wsdl,放在根目錄下 args3=new String[]{"-o","myHello.wsdl","-wsdl",this.className.getName()}; } /** * 構造函數 * @param className 要生成WSDL的Java類 * @param wsdlFileName 要生成WSDL的文件名稱(不包含擴展名) */ public Java2WSDL(Class<?> className,String wsdlFileName) { this.className = className; //根據class生成Hello.wsdl,生成的wsdl文件放在resource目錄下 args1=new String[]{"-wsdl","-d","./resource",this.className.getName()}; //根據class生成Hello.wsdl,生成的文件放在根目錄下的.example下 args2=new String[]{"-wsdl","-cp", "./example",this.className.getName()}; // 根據class生成wsdl文件,文件命名為myHello.wsdl,放在根目錄下 args3=new String[]{"-o",wsdlFileName+".wsdl","-wsdl",this.className.getName()}; } public void java2WSDL(String[] args){ JavaToWS javaToWS = new JavaToWS(args); try { javaToWS.run(); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { Java2WSDL java2WSDL = new Java2WSDL(FileMarkService.class); java2WSDL.java2WSDL(java2WSDL.args1); } }