pepoとネットワークを語ろう

40年前からこれまでとこれからのネットワークを語る

PepoLinuxでjavaを動かしてみる

2010-01-30 16:31:10 | Linux

PepoLinuxで学習用にjavaをインストールしてみたあとの確認です

Centos5.4でjavaをコンパイルと実行するには
gcc-java
java-1.4.2-gcj-compat
が必要です
[root@~]#  yum install gcc-java java-1.4.2-gcj-compat
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * addons: www.ftp.ne.jp
 * base: www.ftp.ne.jp
 * extras: www.ftp.ne.jp
 * updates: www.ftp.ne.jp
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package gcc-java.i386 0:4.1.2-46.el5_4.2 set to be updated
--> Processing Dependency: libgcj = 4.1.2-46.el5_4.2 for package: gcc-java
--> Processing Dependency: libgcj-devel = 4.1.2-46.el5_4.2 for package: gcc-java
--> Processing Dependency: libgcj-tools.so.7rh for package: gcc-java
--> Processing Dependency: libgcj.so.7rh for package: gcc-java
--> Processing Dependency: libgij.so.7rh for package: gcc-java
---> Package java-1.4.2-gcj-compat.i386 0:1.4.2.0-40jpp.115 set to be updated
--> Processing Dependency: gjdoc for package: java-1.4.2-gcj-compat
--> Running transaction check
---> Package gjdoc.i386 0:0.7.7-12.el5 set to be updated
--> Processing Dependency: antlr for package: gjdoc
---> Package libgcj.i386 0:4.1.2-46.el5_4.2 set to be updated
---> Package libgcj-devel.i386 0:4.1.2-46.el5_4.2 set to be updated
--> Running transaction check
---> Package antlr.i386 0:2.7.6-4jpp.2 set to be updated
--> Finished Dependency Resolution

Dependencies Resolved

==========================================================================================
 Package                      Arch        Version                    Repository      Size
==========================================================================================
Installing:
 gcc-java                     i386        4.1.2-46.el5_4.2           updates        2.9 M
 java-1.4.2-gcj-compat        i386        1.4.2.0-40jpp.115          base            29 k
Installing for dependencies:
 antlr                        i386        2.7.6-4jpp.2               base           978 k
 gjdoc                        i386        0.7.7-12.el5               base           793 k
 libgcj                       i386        4.1.2-46.el5_4.2           updates         16 M
 libgcj-devel                 i386        4.1.2-46.el5_4.2           updates        1.4 M

Transaction Summary
==========================================================================================
Install      6 Package(s)
Update       0 Package(s)
Remove       0 Package(s)

Total download size: 22 M
Is this ok [y/N]: y
Downloading Packages:
(1/6): java-1.4.2-gcj-compat-1.4.2.0-40jpp.115.i386.rpm            |  29 kB     00:00
(2/6): gjdoc-0.7.7-12.el5.i386.rpm                                 | 793 kB     00:00
(3/6): antlr-2.7.6-4jpp.2.i386.rpm                                 | 978 kB     00:01
(4/6): libgcj-devel-4.1.2-46.el5_4.2.i386.rpm                      | 1.4 MB     00:01
(5/6): gcc-java-4.1.2-46.el5_4.2.i386.rpm                          | 2.9 MB     00:02
(6/6): libgcj-4.1.2-46.el5_4.2.i386.rpm                            |  16 MB     00:15
------------------------------------------------------------------------------------------
javaの環境のインストールが完了する、でお決まりのHello World!

[root@~]# cat hello.java
public class Hello {
  public static void main(String[] args) {
    System.out.println("Hello World!");
  }
}
root@~]#  gcj -C hello.java
hello.java:1: error: Public class ‘Hello’ must be defined in a file called ‘Hello.java’.
public class Hello {
                ^
1 error

おや、errorがでましたがな…
内容からすると、Public class ‘Hello’はHello.javaをファイルで定義する必要がある?

おーと、ファイル名が間違いといわっしゃる

[root@~]# mv hello.java Hello.java
[root@~]# gcj -C Hello.java

[root@~]# ll
-rw-r--r-- 1 root root  400  1月 30 17:26 Hello.class
-rw-r--r-- 1 root root  111  1月 30 17:16 Hello.java
[root@~]# java Hello
Hello World!

おー動いたではないか

ちなみにlinuxでコンパイルされたHello.classをWindowsXPで動くか確かめてみる

WindowsXPでLinuxでコンパイルされたHello.classをコマンドプロンプトで実行させます
C:Documents and SettingsAdministrator>dir
2010/01/30  17:35               400 Hello.class

C:Documents and SettingsAdministrator>java Hello
Hello World!

おー動いたではないか

pepo