Aug 6, 2002
onlsbc1, Red Hat Linux 7.3 のシステムでのJavaRMIの実行
--- hello world実行#2
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
(http://www-online.kek.jp/~inoue/para-CAMAC/
Test/SBC-Hello-JavaRMI.html)
高エネルギー加速器研究機構
素粒子原子核研究所
物理、オンライングループ
井上 栄二
(1). 現状確認
(A). SBC、onlsbc1にjava version "1.4.0_01-b03" がインストールされて
いる。
(2). ここでやるべきこと
Red Hat Linux 7.3、onlsbc1のマシン上でJavaRMIでのhello worldを実行する。
(3). コンパイル
(3-1). Javaのチェック
[inoue@onlsbc1 inoue]$ java -version
java version "1.4.0_01"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0_01-b03)
Java HotSpot(TM) Client VM (build 1.4.0_01-b03, mixed mode)
[inoue@onlsbc1 inoue]$
(3-2). 実行環境
[inoue@onlsbc1 inoue]$ env
PWD=/home/inoue
HOSTNAME=onlsbc1.kek.jp
QTDIR=/usr/lib/qt3-gcc2.96
LESSOPEN=|/usr/bin/lesspipe.sh %s
USER=inoue
LS_COLORS=no=00:fi=00:di=01;34:ln=01;36:pi=40;33:so=01;35:bd=40;33;01:cd=40;33;01:or=01;05;37;41:mi=01;05;37;41:ex=01;32:*.cmd=01;32:*.exe=01;32:*.com=01;32:*.btm=01;32:*.bat=01;32:*.sh=01;32:*.csh=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.gz=01;31:*.bz2=01;31:*.bz=01;31:*.tz=01;31:*.rpm=01;31:*.cpio=01;31:*.jpg=01;35:*.gif=01;35:*.bmp=01;35:*.xbm=01;35:*.xpm=01;35:*.png=01;35:*.tif=01;35:
MAIL=/var/spool/mail/inoue
INPUTRC=/etc/inputrc
LANG=ja_JP.eucJP
SSH_CLIENT=130.87.153.10 32955 22
LOGNAME=inoue
SHLVL=1
SHELL=/bin/bash
HISTSIZE=1000
HOME=/home/inoue
TERM=dtterm
SSH_ASKPASS=/usr/libexec/openssh/gnome-ssh-askpass
PATH=/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/home/inoue/bin:/usr/java/bin
JLESSCHARSET=japanese
SSH_TTY=/dev/pts/2
_=/usr/bin/env
OLDPWD=/home/inoue/PI
[inoue@onlsbc1 inoue]$
(3-3). サーバ側の準備
この実行で使用するソースコードは、以下のURLから入手した。
http://java.sun.com/j2se/1.4/ja/docs/ja/guide/rmi/archives/getStart.zip
パッケージ名は examples.hello、ソースディレクトリは
~/JavaRMI/getStart/examples/hello である。
[inoue@onlsbc1 hello]$ pwd
/home/inoue/JavaRMI/getStart/examples/hello
[inoue@onlsbc1 hello]$ ls -l
合計 20
-rw-r--r-- 1 inoue inoue 175 7月 30 14:06 Hello.java
-rw-r--r-- 1 inoue inoue 751 7月 30 14:06 HelloApplet.java
-rw-r--r-- 1 inoue inoue 956 7月 30 14:06 HelloImpl.java
-rw-r--r-- 1 inoue inoue 245 7月 30 14:06 hello.html
-rw-r--r-- 1 inoue inoue 81 7月 30 14:06 policy
[inoue@onlsbc1 hello]$
*.java ソースファイルをコンパイルして、.class ファイルを作成する。 次に
rmic コンパイラを実行してスタブとスケルトンを作成する。
javac や rmic コンパイラを使うときは、生成されるクラスファイルをどの
ディレクトリに置くかを指定しなければならない。 アプレットの場合には、
すべてのファイルをアプレットのコードベースディレクトリに置く必要がある。
この例では、コードベースディレクトリは、
onlsbc1:$HOME/public_html/myclasses
になる。
(3-3-1). ランタイムパスを設定する
[inoue@onlsbc1 inoue]$ export CLASSPATH=.:$HOME/public_html/myclasses:$HOME/JavaRMI/getStart/examples/hello
[inoue@onlsbc1 inoue]$ printenv CLASSPATH
.:/home/inoue/public_html/myclasses:/home/inoue/JavaRMI/getStart/examples/hello
[inoue@onlsbc1 inoue]$
[inoue@onlsbc1 myclasses]$ pwd
/home/inoue/public_html/myclasses
[inoue@onlsbc1 myclasses]$ ls -l
合計 0
[inoue@onlsbc1 myclasses]$
(3-3-2). ソースファイルをコンパイルする
[inoue@onlsbc1 hello]$ pwd
/home/inoue/JavaRMI/getStart/examples/hello
[inoue@onlsbc1 hello]$ ls -l
合計 20
-rw-r--r-- 1 inoue inoue 175 7月 30 14:06 Hello.java
-rw-r--r-- 1 inoue inoue 751 7月 30 14:06 HelloApplet.java
-rw-r--r-- 1 inoue inoue 956 7月 30 14:06 HelloImpl.java
-rw-r--r-- 1 inoue inoue 245 7月 30 14:06 hello.html
-rw-r--r-- 1 inoue inoue 81 7月 30 14:06 policy
[inoue@onlsbc1 hello]$
[inoue@onlsbc1 hello]$ cat Hello.java
package examples.hello;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Hello extends Remote {
String sayHello() throws RemoteException;
}
[inoue@onlsbc1 hello]$
[inoue@onlsbc1 hello]$ cat HelloApplet.java
package examples.hello;
import java.applet.Applet;
import java.awt.Graphics;
import java.rmi.Naming;
import java.rmi.RemoteException;
public class HelloApplet extends Applet {
String message = "blank";
// "obj" is the identifier that we'll use to refer
// to the remote object that implements the "Hello"
// interface
Hello obj = null;
public void init() {
try {
obj = (Hello)Naming.lookup("//" +
getCodeBase().getHost() + "/HelloServer");
message = obj.sayHello();
} catch (Exception e) {
System.out.println("HelloApplet exception: " +
e.getMessage());
e.printStackTrace();
}
}
public void paint(Graphics g) {
g.drawString(message, 25, 50);
}
}
[inoue@onlsbc1 hello]$
[inoue@onlsbc1 hello]$ cat HelloImpl.java
package examples.hello;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.RMISecurityManager;
import java.rmi.server.UnicastRemoteObject;
public class HelloImpl extends UnicastRemoteObject
implements Hello {
public HelloImpl() throws RemoteException {
super();
}
public String sayHello() {
return "Hello World!";
}
public static void main(String args[]) {
// Create and install a security manager
if (System.getSecurityManager() == null) {
System.setSecurityManager(new RMISecurityManager());
}
try {
HelloImpl obj = new HelloImpl();
// Bind this object instance to the name "HelloServer"
Naming.rebind("HelloServer", obj);
System.out.println("HelloServer bound in registry");
} catch (Exception e) {
System.out.println("HelloImpl err: " + e.getMessage());
e.printStackTrace();
}
}
}
[inoue@onlsbc1 hello]$
[inoue@onlsbc1 hello]$ cat hello.html
<HTML>
<title>Hello World</title>
<center> <h1>Hello World</h1> </center>
The message from the HelloServer is:
<p>
<applet codebase="myclasses/"
code="examples.hello.HelloApplet"
width=500 height=120>
</applet>
</HTML>
[inoue@onlsbc1 hello]$
[inoue@onlsbc1 hello]$ javac -d $HOME/public_html/myclasses Hello.java HelloImpl.java HelloApplet.java
[inoue@onlsbc1 hello]$
[inoue@onlsbc1 hello]$ ls -lR $HOME/public_html/myclasses
/home/inoue/public_html/myclasses:
合計 4
drwxrwxr-x 3 inoue inoue 4096 7月 30 16:07 examples
/home/inoue/public_html/myclasses/examples:
合計 4
drwxrwxr-x 2 inoue inoue 4096 7月 30 16:07 hello
/home/inoue/public_html/myclasses/examples/hello:
合計 12
-rw-rw-r-- 1 inoue inoue 224 7月 30 16:07 Hello.class
-rw-rw-r-- 1 inoue inoue 1281 7月 30 16:07 HelloApplet.class
-rw-rw-r-- 1 inoue inoue 1240 7月 30 16:07 HelloImpl.class
[inoue@onlsbc1 hello]$
(3-3-3). rmic を使ってスタブまたはスケルトンを生成する
[inoue@onlsbc1 hello]$ pwd
/home/inoue/JavaRMI/getStart/examples/hello
[inoue@onlsbc1 hello]$ ls -l
合計 20
-rw-r--r-- 1 inoue inoue 175 7月 30 14:06 Hello.java
-rw-r--r-- 1 inoue inoue 751 7月 30 14:06 HelloApplet.java
-rw-r--r-- 1 inoue inoue 956 7月 30 14:06 HelloImpl.java
-rw-r--r-- 1 inoue inoue 245 7月 30 14:06 hello.html
-rw-r--r-- 1 inoue inoue 81 7月 30 14:06 policy
[inoue@onlsbc1 hello]$
[inoue@onlsbc1 hello]$ rmic -d $HOME/public_html/myclasses examples.hello.HelloImpl
[inoue@onlsbc1 hello]$
[inoue@onlsbc1 hello]$ ls -lR $HOME/public_html/myclasses
/home/inoue/public_html/myclasses:
合計 4
drwxrwxr-x 3 inoue inoue 4096 7月 30 16:07 examples
/home/inoue/public_html/myclasses/examples:
合計 4
drwxrwxr-x 2 inoue inoue 4096 7月 30 16:11 hello
/home/inoue/public_html/myclasses/examples/hello:
合計 20
-rw-rw-r-- 1 inoue inoue 224 7月 30 16:07 Hello.class
-rw-rw-r-- 1 inoue inoue 1281 7月 30 16:07 HelloApplet.class
-rw-rw-r-- 1 inoue inoue 1240 7月 30 16:07 HelloImpl.class
-rw-rw-r-- 1 inoue inoue 1448 7月 30 16:11 HelloImpl_Skel.class
-rw-rw-r-- 1 inoue inoue 2920 7月 30 16:11 HelloImpl_Stub.class
[inoue@onlsbc1 hello]$
(3-3-4). HTML ファイルを配置ディレクトリに移す
[inoue@onlsbc1 hello]$ pwd
/home/inoue/JavaRMI/getStart/examples/hello
[inoue@onlsbc1 hello]$ ls -l
合計 20
-rw-r--r-- 1 inoue inoue 175 7月 30 14:06 Hello.java
-rw-r--r-- 1 inoue inoue 751 7月 30 14:06 HelloApplet.java
-rw-r--r-- 1 inoue inoue 956 7月 30 14:06 HelloImpl.java
-rw-r--r-- 1 inoue inoue 245 7月 30 14:06 hello.html
-rw-r--r-- 1 inoue inoue 81 7月 30 14:06 policy
[inoue@onlsbc1 hello]$ cp hello.html $HOME/public_html
[inoue@onlsbc1 hello]$ ls -l $HOME/public_html/hello.html
-rw-r--r-- 1 inoue inoue 245 7月 30 16:14 /home/inoue/public_html/hello.html
[inoue@onlsbc1 hello]$
(4). RMI レジストリ、サーバ、およびアプレットを起動する
(4-1). Apache Webサーバを使う場合
(4-1-1). Apache Webサーバの起動
Apache の設定を行う。
[root@onlsbc1 conf]# apacheconf
[root@onlsbc1 conf]#
Apache Webサーバを起動する。
[root@onlsbc1 conf]# /sbin/service httpd start
httpdを起動中: [ OK ]
[root@onlsbc1 conf]#
Red Hat Linux 7.3 の標準では、ユーザのホームディレクトリのパーミッション
は、ユーザのみが読み書きおよび実行可能になっている。 これでは、ユーザ
ディレクトリ下の public_htmlディレクトリーに webブラウザでアクセスした
時にアクセス権がないと言われてしまう。 グループ、アザーズに読み出し、
実行のパーミッションを付ける。
[inoue@onlsbc1 home]$ pwd
/home
[inoue@onlsbc1 home]$ ls -l
合計 8
drwx------ 18 inoue inoue 4096 8月 5 13:34 inoue
drwx------ 3 yasu yasu 4096 7月 24 01:46 yasu
[inoue@onlsbc1 home]$ chmod go+rx inoue
[inoue@onlsbc1 home]$ ls -l
合計 8
drwxr-xr-x 18 inoue inoue 4096 8月 5 13:34 inoue
drwx------ 3 yasu yasu 4096 7月 24 01:46 yasu
[inoue@onlsbc1 home]$
[inoue@onlsbc1 getStart]$ pwd
/home/inoue/JavaRMI/getStart
[inoue@onlsbc1 getStart]$
(4-1-2). rmiregistry の起動
rmiregistry を起動する。
[inoue@onlsbc1 getStart]$ rmiregistry &
[1] 1778
[inoue@onlsbc1 getStart]$
[inoue@onlsbc1 getStart]$ ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 10:12 ? 00:00:06 init
root 2 1 0 10:12 ? 00:00:00 [keventd]
root 3 1 0 10:12 ? 00:00:00 [kapmd]
root 4 1 0 10:12 ? 00:00:00 [ksoftirqd_CPU0]
root 5 1 0 10:12 ? 00:00:00 [kswapd]
root 6 1 0 10:12 ? 00:00:00 [bdflush]
root 7 1 0 10:12 ? 00:00:00 [kupdated]
root 8 1 0 10:13 ? 00:00:00 [mdrecoveryd]
root 12 1 0 10:13 ? 00:00:00 [kjournald]
root 71 1 0 10:13 ? 00:00:00 [khubd]
root 621 1 0 10:14 ? 00:00:00 syslogd -m 0
root 626 1 0 10:14 ? 00:00:00 klogd -x
rpc 640 1 0 10:14 ? 00:00:00 portmap
rpcuser 662 1 0 10:14 ? 00:00:00 rpc.statd
root 749 1 0 10:14 ? 00:00:00 /usr/sbin/apmd -p 10 -w 5 -W -P
root 791 1 0 10:14 ? 00:00:01 /usr/sbin/sshd
root 812 1 0 10:14 ? 00:00:00 xinetd -stayalive -reuse -pidfil
lp 832 1 0 10:14 ? 00:00:00 lpd Waiting
root 858 1 0 10:14 ? 00:00:00 sendmail: accepting connections
root 871 1 0 10:14 ? 00:00:01 gpm -t ps/2 -m /dev/mouse
wnn 885 1 0 10:14 ? 00:00:00 /usr/bin/jserver
bin 898 1 0 10:14 ? 00:00:00 cannaserver -syslog -u bin -inet
root 910 1 0 10:14 ? 00:00:00 crond
xfs 962 1 0 10:14 ? 00:00:01 xfs -droppriv -daemon
daemon 986 1 0 10:14 ? 00:00:00 /usr/sbin/atd
root 995 1 0 10:14 tty1 00:00:00 /sbin/mingetty tty1
root 996 1 0 10:14 tty2 00:00:00 /sbin/mingetty tty2
root 997 1 0 10:14 tty3 00:00:00 /sbin/mingetty tty3
root 998 1 0 10:14 tty4 00:00:00 /sbin/mingetty tty4
root 999 1 0 10:14 tty5 00:00:00 /sbin/mingetty tty5
root 1000 1 0 10:14 tty6 00:00:00 /sbin/mingetty tty6
root 1001 1 0 10:14 ? 00:00:00 /usr/bin/gdm -nodaemon
root 1009 1001 0 10:14 ? 00:00:00 /usr/bin/gdm -nodaemon
root 1010 1009 0 10:14 ? 00:00:06 /usr/bin/X11/X :0 -auth /var/gdm
gdm 1017 1009 0 10:15 ? 00:00:03 /usr/bin/gdmlogin --disable-soun
gdm 1018 1017 0 10:15 ? 00:00:01 /usr/bin/xsri --redhat-login --r
root 1019 791 0 10:15 ? 00:00:03 /usr/sbin/sshd
inoue 1020 1019 0 10:15 pts/0 00:00:00 -bash
root 1222 791 0 10:53 ? 00:00:00 /usr/sbin/sshd
inoue 1223 1222 0 10:53 pts/1 00:00:00 -bash
root 1735 1 0 13:10 ? 00:00:02 /usr/sbin/httpd -DHAVE_ACCESS -D
apache 1738 1735 0 13:10 ? 00:00:00 /usr/sbin/httpd -DHAVE_ACCESS -D
apache 1739 1735 0 13:10 ? 00:00:00 /usr/sbin/httpd -DHAVE_ACCESS -D
apache 1740 1735 0 13:10 ? 00:00:00 /usr/sbin/httpd -DHAVE_ACCESS -D
apache 1741 1735 0 13:10 ? 00:00:00 /usr/sbin/httpd -DHAVE_ACCESS -D
apache 1742 1735 0 13:10 ? 00:00:00 /usr/sbin/httpd -DHAVE_ACCESS -D
apache 1743 1735 0 13:10 ? 00:00:00 /usr/sbin/httpd -DHAVE_ACCESS -D
apache 1744 1735 0 13:10 ? 00:00:00 /usr/sbin/httpd -DHAVE_ACCESS -D
apache 1745 1735 0 13:10 ? 00:00:00 /usr/sbin/httpd -DHAVE_ACCESS -D
inoue 1778 1020 46 13:25 pts/0 00:00:06 rmiregistry
inoue 1780 1778 0 13:25 pts/0 00:00:00 rmiregistry
inoue 1781 1780 3 13:25 pts/0 00:00:00 rmiregistry
inoue 1782 1780 0 13:25 pts/0 00:00:00 rmiregistry
inoue 1783 1780 0 13:25 pts/0 00:00:00 rmiregistry
inoue 1784 1780 0 13:25 pts/0 00:00:00 rmiregistry
inoue 1785 1780 0 13:25 pts/0 00:00:00 rmiregistry
inoue 1786 1780 0 13:25 pts/0 00:00:00 rmiregistry
inoue 1787 1780 52 13:25 pts/0 00:00:03 rmiregistry
inoue 1789 1780 0 13:25 pts/0 00:00:00 rmiregistry
inoue 1790 1780 2 13:25 pts/0 00:00:00 rmiregistry
inoue 1791 1020 0 13:25 pts/0 00:00:00 ps -ef
[inoue@onlsbc1 getStart]$
(4-1-3). サーバ の起動
サーバを起動する
[inoue@onlsbc1 getStart]$ java -Djava.rmi.server.codebase=http://onlsbc1.kek.jp/~inoue/myclasses/ -Djava.security.policy=$HOME/JavaRMI/getStart/examples/hello/policy examples.hello.HelloImpl
HelloServer bound in registry
(4-1-4). サーバへのアクセス
webブラウザ、IEを使ってサーバにアクセスした結果
Webブラウザ、IEで "http://onlsbc1.kek.jp/~inoue/hello.html" に
アクセスすると、"Hello World!"と表示される。
appletviewerを使ってサーバにアクセスした結果
[inoue@onlsbc1 public_html]$ export DISPLAY=onlsun2:0
[inoue@onlsbc1 public_html]$ which appletviewer
/usr/java/bin/appletviewer
[inoue@onlsbc1 public_html]$ appletviewer http://onlsbc1.kek.jp/~inoue/hello.htm
l
警告: AppletViewer プロパティファイルが読み込まれません: /home/inoue/.hotjava/pr
operties デフォルトを使用します。
Warning: Cannot convert string "-watanabe-mincho-medium-r-normal--*-140-*-*-c-*-
jisx0208.1983-0" to type FontStruct
2002/08/05 13:34:05 java.util.prefs.FileSystemPreferences$4 run
警告: Recreated user preferences directory.
[inoue@onlsbc1 public_html]$
appletviewerで "http://onlsbc1.kek.jp/~inoue/hello.html" に
アクセスすると、"Hello World!" と表示される。