JXTA HelloWorld on JXSE 2.5 【一】

首先感叹一下关于JXTA的书籍实在是太少见了,我手头的《JXTA技术手册》中文版是清华大学出版社2004年4月第一版的4000册之一,到如今,2008年7月,仍然在JXTA陈旧的版本下指导我们在JXTA 2.5中摸索,实在有些……于是我的Hello World就花费了我一整天的时间!按照书里面的来尝试真的有点困难,所以重新把我做的过程整理在这里:

1. 书中给出的第一个Hello World范例

import net.jxta.peergroup.PeerGroup; 
import net.jxta.peergroup.PeerGroupFactory;
import net.jxta.exception.PeerGroupException; 

public class HelloWorld { 

    static PeerGroup group = null; 

    public static void main(String args[]) { 
        HelloWorld myapp = new HelloWorld();
        myapp.startJxta();
        System.exit(0);
    } 

    private void startJxta() 
    { 
        try 
        {
            // Create and start the default JXTA NetPeerGroup
            group = PeerGroupFactory.newNetPeerGroup();
        } catch (PeerGroupException e) 
        { 
            // Could not instantiate the group; print the stack and exit
            System.out.println("fatal error: creating the net PeerGroup"); 
            System.exit(1); 
        } 
        System.out.println("Started Hello World");
    } 
} 

这段代码有什么问题呢。根据JXTA v2.5的api,自JXTA JSE v2.4起,类PeerGroupFactory已经被NetPeerGroupFactory和WorldPeerGroupFactory代替了。

NetPeerGroupFactory介绍: A factory for instantiating a Network Peer Group instances. The Network Peer Group is the base peer group for applications and services within the JXTA network. Most applications and services will instantiate their own peer groups using the Network Peer Group as a base. 也就是说,所有用户对等组实际上都是网络对等组的子集,任何一个对等体都是在网络对等组中,任何用户对等组都是以网络对等组为基础。

因此我把程序更新为

import net.jxta.peergroup.PeerGroup; 
import net.jxta.peergroup.NetPeerGroupFactory; 
// import net.jxta.peergroup.WorldPeerGroupFactory;
import net.jxta.exception.PeerGroupException; 

public class HelloWorld { 

    static PeerGroup group = null; 

    public static void main(String args[]) { 
        HelloWorld myapp = new HelloWorld();
        myapp.startJxta();
        System.exit(0);
    } 

    private void startJxta() 
    { 
        try 
        {
            // Create and start the default JXTA NetPeerGroup
            group =(new NetPeerGroupFactory()).getWeakInterface(); 
        } catch(PeerGroupException e) 
        { 
            // Could not instantiate the group; print the stack and exit
            System.out.println("fatal error: creating the net PeerGroup"); 
            System.exit(1); 
        } 
        System.out.println("Started Hello World");
    } 
} 

2. 运行Hello World

我是在Eclipse下运行的(因为想把JXTA类库import进去还是用IDE比较方便)。在Project Property中,加入JXTA lib解压后的全部jar文件。像这样:

这样,Eclipse中的Package Explorer就会变成如下所示:

现在就可以运行啦~运行的时候,红字会不停的一行一样显示在Console当中,比如说:“警告: Platform Config not found : file:/********/HelloWorld/.jxta/PlatformConfig”。因为是首次运行,没有JXTA的配置文件是正常的,耐心等待,一会儿,JXTA Configurator就会蹦出来:

配置方法这里就不详述啦,要注意的是,如果偷懒按照默认值直接点击OK是不行的~如果暂时不想管里面到底有哪些配置,不妨学我先把最后一个选项卡中的use a relay的勾选去掉,然后全用默认OK^_^

之间Eclipse的Console中又蹦出一系列的红字(有空要仔细看看这些红字啊,不是没用的废话),再一看自己的HelloWorld文件夹,多出了个.jxta的文件夹,里面就是这一次的配置数据啦。重新运行你的Hello World,是不是没再出现配置界面,而只是显示了一系列红字和黑色的"Started Hello World"了呢?恭喜恭喜,JXTA的第一步总算是迈出来了:)

3. JXTA技术手册Hello World升级

JXTA手册里面的Hello World是步步升级,我们也要一步一步更新我们的Hello World,随后即将详细报道!

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值