Eclipse导出可运行的jar包,作为工具发布webService接口

来源:互联网 发布:安卓网络诊断修复工具 编辑:程序博客网 时间:2024/06/06 03:07

最近写了一个图形界面webService接口的发布工具,通过运行jar包的方式可以在用户界面上点击按钮发布

1.工具类

package com.cyq.web;


import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Random;


import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;


@WebService
public class Mywebservicetest {

@WebMethod(exclude=false)  
public String helloWord2(String name){  
return"Hello: "+name;  
}  


public static void publish() {


Endpoint.publish("http://192.168.1.103:8080/helloWord",new Mywebservicetest());  
System.out.println("webService接口发布成功,地址【http://192.168.1.103:8080/helloWord?wsdl】");

Endpoint.publish("http://127.0.0.1:8080/helloWord",new Mywebservicetest());  
System.out.println("webService接口发布成功,地址【http://127.0.0.1:8080/helloWord?wsdl】");
}

public static void main(String []args){
//System.out.println("webservice test!!!!!!");


//Endpoint.publish("http://localhost:8080/helloWord",new Mywebservicetest());
Frame f = new Frame("发布webService接口");
f.setSize(500,400);
f.setLocation(300, 300);


f.setLayout(new FlowLayout());
Button publish_but = new Button("发布");
f.add(publish_but);

Button destroy_but = new Button("退出");
f.add(destroy_but);


Label l = new Label("                        ");

f.add(l);
TextArea ta = new TextArea(20,60);
ta.setVisible(true);
f.add(ta);
f.setVisible(true);

publish_but.addActionListener(new MyAction(f));

destroy_but.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.out.println("发布webService已关闭");
System.exit(0);


}
});

f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
System.exit(0);
}

});
}



}
class MyAction implements ActionListener {

private Frame f;

public MyAction(Frame f){
this.f = f;
}


@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
TextArea ta = (TextArea) f.getComponents()[3];
System.out.println("正在发布...\n");
ta.append("正在发布...\n");

try {
//Endpoint.publish("http://localhost:8080/helloWord",new Mywebservicetest());
Endpoint.publish("http://192.168.20.246:8094/helloWord",new Mywebservicetest());
} catch(Exception ex) {
ta.append("发布失败:"+ex.toString()+"\n");
return;
}
Random ran = new Random();
int ranNumber = ran.nextInt(6);
for (int i=0;i<ranNumber;i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println("...\n");
ta.append("...\n");
}
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

System.out.println("发布webService接口成功\n");
ta.append("发布webService接口成功!\n");



Label l =  (Label)f.getComponents()[2];
l.setText("发布成功!");


}

}

2.导出jar文件

   工程--》Export--》Jar file,看到如下图对话框

  

  点击next



点击next


指定Main class 为入口函数所在的类,点击Finish,即可完成打包


3.通过命令行运行jar文件


输入命令行后,回车


点击发布按钮,可以看到接口发布成功,如下图






总结:上述工程没有用到第三方jar包,如果用到了的话,导出jar包时略有写不同

0 0
原创粉丝点击