JavaでHello World

(20)
https://qiita.com/terappy/items/537c069923144a9d9755
https://eng-entrance.com/java-hello-world
https://www.net.t-labs.tu-berlin.de/teaching/computer_networking/02.08.htm

apt install openjdk-8-jdk
java -version
javac -version

vim hello.java
public class hello{
public static void main(String args){
System.out.println("Hello World!");
}
}

javac hello.java
java hello

--

vim web.java

import java.io.*;
import java.net.*;
import java.util.*;

class web{
public static void main(String argv) throws Exception {
ServerSocket listenSocket = new ServerSocket(8080);
while(true){
Socket connectionSocket = listenSocket.accept();
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream() ) );
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream() );
String requestMessageLine = inFromClient.readLine();
String strAry = requestMessageLine.split(" ");
if (strAry[0].equals("GET") ){
outToClient.writeBytes("HTTP/1.0 200\r\n");
outToClient.writeBytes("Content-Type: text/html\r\n");
outToClient.writeBytes("\r\n");
outToClient.writeBytes("Hello World!\n");
connectionSocket.close();
}
else {
System.out.println("Bad Request Message");
}
}
}
}

 

javac web.java
java web

 

 

  • Debian

(10)
apt install default-jre
apt install default-jdk

java -version
javac -version

vim hello.java
public class hello{
public static void main(String args){
System.out.println("Hello World!");
}
}

javac hello.java
java hello

--

vim web.java

import java.io.*;
import java.net.*;
import java.util.*;

class web{
public static void main(String argv) throws Exception {
ServerSocket listenSocket = new ServerSocket(8080);
while(true){
Socket connectionSocket = listenSocket.accept();
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream() ) );
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream() );
String requestMessageLine = inFromClient.readLine();
String
strAry = requestMessageLine.split(" ");
if (strAry[0].equals("GET") ){
outToClient.writeBytes("HTTP/1.0 200\r\n");
outToClient.writeBytes("Content-Type: text/html\r\n");
outToClient.writeBytes("\r\n");
outToClient.writeBytes("Hello World!\n");
connectionSocket.close();
}
else {
System.out.println("Bad Request Message");
}
}
}
}

 

javac web.java
java web

 

 

  • CentOS

(8)
dnf install -y java-1.8.0-openjdk
dnf install -y java-1.8.0-openjdk-devel

java -version
javac -version

vim hello.java
public class hello{
public static void main(String args){
System.out.println("Hello World!");
}
}

javac hello.java
java hello

--

vim web.java

import java.io.*;
import java.net.*;
import java.util.*;

class web{
public static void main(String argv) throws Exception {
ServerSocket listenSocket = new ServerSocket(8080);
while(true){
Socket connectionSocket = listenSocket.accept();
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream() ) );
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream() );
String requestMessageLine = inFromClient.readLine();
String strAry = requestMessageLine.split(" ");
if (strAry[0].equals("GET") ){
outToClient.writeBytes("HTTP/1.0 200\r\n");
outToClient.writeBytes("Content-Type: text/html\r\n");
outToClient.writeBytes("\r\n");
outToClient.writeBytes("Hello World!\n");
connectionSocket.close();
}
else {
System.out.println("Bad Request Message");
}
}
}
}

 

javac web.java
java web

 

 

  • Windows Server

(2019)

http://jdk.java.net/14/

cd C:\jdk-14.0.2\bin

java -version
javac -version

notepad hello.java
public class hello{
public static void main(String args){
System.out.println("Hello World!");
}
}


javac hello.java
java hello

--

notepad web.java

import java.io.*;
import java.net.*;
import java.util.*;

class web{
public static void main(String argv) throws Exception {
ServerSocket listenSocket = new ServerSocket(8080);
while(true){
Socket connectionSocket = listenSocket.accept();
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream() ) );
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream() );
String requestMessageLine = inFromClient.readLine();
String
strAry = requestMessageLine.split(" ");
if (strAry[0].equals("GET") ){
outToClient.writeBytes("HTTP/1.0 200\r\n");
outToClient.writeBytes("Content-Type: text/html\r\n");
outToClient.writeBytes("\r\n");
outToClient.writeBytes("Hello World!\n");
connectionSocket.close();
}
else {
System.out.println("Bad Request Message");
}
}
}
}

 

javac web.java
java web