core_java11_fundamentals/lesson2.md

43 lines
723 B
Markdown

# Lesson 2: The Java Programming Environment
## Run as server
For headless application (run as server), we can use docker.
After create `FILE.java`, create a `Dockerfile`
```
FROM openjdk:slim-buster
COPY <script location> /usr/src/myapp
WORKDIR /usr/src/myapp
RUN javac <script>.java
CMD ["java", "<script name>"]
```
Then run and build the Docker image
```
$ docker build -t my-java-app .
$ docker run -it --rm --name my-running-app my-java-app
```
## If run as desktop application
First, compile Java
```
$ javac <script>.java
```
Then, run Java
```
$ java <script>
```
## Run jshell for interaction
Methods:
* 1: `$ jshell`
* 2: using docker: `docker run --rm -it -v "$(pwd)":/usr/src openjdk:slim-buster`