diff --git a/examples/httpserver-java25-base/.dockerignore b/examples/httpserver-java25-base/.dockerignore new file mode 100644 index 00000000..a154b6ff --- /dev/null +++ b/examples/httpserver-java25-base/.dockerignore @@ -0,0 +1,3 @@ +/Makefile.uk +/.unikraft/ +/.config* diff --git a/examples/httpserver-java25-base/.gitignore b/examples/httpserver-java25-base/.gitignore new file mode 100644 index 00000000..a154b6ff --- /dev/null +++ b/examples/httpserver-java25-base/.gitignore @@ -0,0 +1,3 @@ +/Makefile.uk +/.unikraft/ +/.config* diff --git a/examples/httpserver-java25-base/Dockerfile b/examples/httpserver-java25-base/Dockerfile new file mode 100644 index 00000000..bbcf370a --- /dev/null +++ b/examples/httpserver-java25-base/Dockerfile @@ -0,0 +1,27 @@ +FROM openjdk:25-rc-oraclelinux9 AS build + +ENV JAVA_HOME=/usr/java/openjdk-25 + +RUN ldconfig $JAVA_HOME/lib/server/ + +WORKDIR /src + +COPY ./SimpleHttpServer.java /src/SimpleHttpServer.java + +RUN javac SimpleHttpServer.java + +FROM alpine:3 AS sys + +FROM scratch + +COPY --from=build /usr/lib64/libc.so.6 /usr/lib64/ +COPY --from=build /usr/lib64/libstdc++.so.6 /usr/lib64/ +COPY --from=build /usr/lib64/libm.so.6 /usr/lib64/ +COPY --from=build /usr/lib64/libz.so.1 /usr/lib64/ +COPY --from=build /usr/lib64/libgcc_s.so.1 /usr/lib64/ +COPY --from=build /lib64/ld-linux-x86-64.so.2 /lib64/ +COPY --from=build /etc/ld.so.cache /etc/ld.so.cache + +COPY --from=build $JAVA_HOME/ /usr/lib/jvm/java-25-runtime/ + +COPY --from=build /src/SimpleHttpServer.class /usr/src/SimpleHttpServer.class diff --git a/examples/httpserver-java25-base/Kraftfile b/examples/httpserver-java25-base/Kraftfile new file mode 100644 index 00000000..c1ab787b --- /dev/null +++ b/examples/httpserver-java25-base/Kraftfile @@ -0,0 +1,9 @@ +spec: v0.6 + +name: httpserver-java25-base + +rootfs: ./Dockerfile + +cmd: ["/usr/lib/jvm/java-25-runtime/bin/java", "-classpath", "/usr/src/", "SimpleHttpServer"] + +runtime: base:latest diff --git a/examples/httpserver-java25-base/README.md b/examples/httpserver-java25-base/README.md new file mode 100644 index 00000000..6c6369c3 --- /dev/null +++ b/examples/httpserver-java25-base/README.md @@ -0,0 +1,83 @@ +# Java 25 HTTP Server + +This directory contains the [Java](https://www.java.com/en/) runtime on Unikraft, in binary compatibility mode. +It implements a simple HTTP server running on Unikraft that provides a simple response to each request. + +## Run and Use + +Use `kraft` to run the image and start a Unikraft instance: + +```bash +kraft run --rm -M 1024M -p 8080:8080 --plat qemu --arch x86_64 . +``` + +If the `--plat` argument is left out, it defaults to `qemu`. +If the `--arch` argument is left out, it defaults to your system's CPU architecture. + +Once executed, it will open port `8080` and wait for connections. +To test it, you can use `curl`: + +```bash +curl localhost:8080 +``` + +You should see a "Hello, World!" message. + +## Inspect and Close + +To list information about the Unikraft instance, use: + +```bash +kraft ps -a +``` + +```text +NAME KERNEL ARGS CREATED STATUS MEM PORTS PLAT +upbeat_jenny project://httpserver-java25:qemu/x86_64 /usr/lib/jvm/java-25-runtime[...] 5 seconds ago running 2048M 0.0.0.0:8080->8080/tcp qemu/x86_64 +``` + +The instance name is `upbeat_jenny`. +To close the Unikraft instance, use: + +```bash +kraft rm upbeat_jenny +``` + +Note that closing the `kraft run` command (e.g., via `Ctrl+c`) does not kill the Unikraft instance. +If you want the Unikraft instance closed when closing the `kraft run` command, use the `--rm` option: + +```bash +kraft run --rm -M 1024M -p 8080:8080 --plat qemu --arch x86_64 . +``` + +## Build and Run Locally + +The commands so far used the pre-build Java image available in the Unikraft registry. + +In oder to to build a local Java image, clone this repository and `cd` into this directory. +Then use `kraft` to build an image locally: + +```bash +kraft build --no-cache --no-update --plat qemu --arch x86_64 +``` + +Similar to the `kraft run` command, if the `--plat` argument is left out, it defaults to `qemu`. +If the `--arch` argument is left out, it defaults to your system's CPU architecture. + +In order to run the locally built image, use `.` (_dot_, the current directory) as the final argument to the `kraft run` command: + +```bash +kraft run --rm -M 1024M -p 8080:8080 --plat qemu --arch x86_64 . +``` + +Same as above, it will open port `8080` and wait for connections. + +## `kraft` and `sudo` + +Mixing invocations of `kraft` and `sudo` can lead to unexpected behavior. +Read more about how to start `kraft` without `sudo` at [https://unikraft.org/sudoless](https://unikraft.org/sudoless). + +## Learn More + +- [How to run unikernels locally](https://unikraft.org/docs/cli/running) +- [How to build `Dockerfile` root filesystems with BuildKit](https://unikraft.org/docs/getting-started/integrations/buildkit) diff --git a/examples/httpserver-java25-base/SimpleHttpServer.java b/examples/httpserver-java25-base/SimpleHttpServer.java new file mode 100644 index 00000000..44c2b248 --- /dev/null +++ b/examples/httpserver-java25-base/SimpleHttpServer.java @@ -0,0 +1,29 @@ +// https://www.logicbig.com/tutorials/core-java-tutorial/http-server/http-server-basic.html + +import com.sun.net.httpserver.HttpContext; +import com.sun.net.httpserver.HttpExchange; +import com.sun.net.httpserver.HttpServer; +import java.io.IOException; +import java.io.OutputStream; +import java.net.InetSocketAddress; + +public class SimpleHttpServer { + private static final int listenPort = 8080; + + public static void main(String[] args) throws IOException { + HttpServer server = HttpServer.create(new InetSocketAddress(listenPort), 0); + HttpContext context = server.createContext("/"); + context.setHandler(SimpleHttpServer::handleRequest); + System.out.println("Waiting for HTTP connections on port " + listenPort + " ..."); + server.start(); + } + + private static void handleRequest(HttpExchange exchange) throws IOException { + String response = "Hello, World!\n"; + exchange.sendResponseHeaders(200, response.getBytes().length); // response code and length + OutputStream os = exchange.getResponseBody(); + os.write(response.getBytes()); + os.close(); + } + +} diff --git a/examples/httpserver-java25/.dockerignore b/examples/httpserver-java25/.dockerignore new file mode 100644 index 00000000..a154b6ff --- /dev/null +++ b/examples/httpserver-java25/.dockerignore @@ -0,0 +1,3 @@ +/Makefile.uk +/.unikraft/ +/.config* diff --git a/examples/httpserver-java25/.gitignore b/examples/httpserver-java25/.gitignore new file mode 100644 index 00000000..a154b6ff --- /dev/null +++ b/examples/httpserver-java25/.gitignore @@ -0,0 +1,3 @@ +/Makefile.uk +/.unikraft/ +/.config* diff --git a/examples/httpserver-java25/Dockerfile b/examples/httpserver-java25/Dockerfile new file mode 100644 index 00000000..bbcf370a --- /dev/null +++ b/examples/httpserver-java25/Dockerfile @@ -0,0 +1,27 @@ +FROM openjdk:25-rc-oraclelinux9 AS build + +ENV JAVA_HOME=/usr/java/openjdk-25 + +RUN ldconfig $JAVA_HOME/lib/server/ + +WORKDIR /src + +COPY ./SimpleHttpServer.java /src/SimpleHttpServer.java + +RUN javac SimpleHttpServer.java + +FROM alpine:3 AS sys + +FROM scratch + +COPY --from=build /usr/lib64/libc.so.6 /usr/lib64/ +COPY --from=build /usr/lib64/libstdc++.so.6 /usr/lib64/ +COPY --from=build /usr/lib64/libm.so.6 /usr/lib64/ +COPY --from=build /usr/lib64/libz.so.1 /usr/lib64/ +COPY --from=build /usr/lib64/libgcc_s.so.1 /usr/lib64/ +COPY --from=build /lib64/ld-linux-x86-64.so.2 /lib64/ +COPY --from=build /etc/ld.so.cache /etc/ld.so.cache + +COPY --from=build $JAVA_HOME/ /usr/lib/jvm/java-25-runtime/ + +COPY --from=build /src/SimpleHttpServer.class /usr/src/SimpleHttpServer.class diff --git a/examples/httpserver-java25/Kraftfile b/examples/httpserver-java25/Kraftfile new file mode 100644 index 00000000..86515192 --- /dev/null +++ b/examples/httpserver-java25/Kraftfile @@ -0,0 +1,9 @@ +spec: v0.6 + +name: httpserver-java25-base + +rootfs: ./Dockerfile + +cmd: ["/usr/lib/jvm/java-25-runtime/bin/java", "-classpath", "/usr/src/", "SimpleHttpServer"] + +runtime: java:25 diff --git a/examples/httpserver-java25/README.md b/examples/httpserver-java25/README.md new file mode 100644 index 00000000..6c6369c3 --- /dev/null +++ b/examples/httpserver-java25/README.md @@ -0,0 +1,83 @@ +# Java 25 HTTP Server + +This directory contains the [Java](https://www.java.com/en/) runtime on Unikraft, in binary compatibility mode. +It implements a simple HTTP server running on Unikraft that provides a simple response to each request. + +## Run and Use + +Use `kraft` to run the image and start a Unikraft instance: + +```bash +kraft run --rm -M 1024M -p 8080:8080 --plat qemu --arch x86_64 . +``` + +If the `--plat` argument is left out, it defaults to `qemu`. +If the `--arch` argument is left out, it defaults to your system's CPU architecture. + +Once executed, it will open port `8080` and wait for connections. +To test it, you can use `curl`: + +```bash +curl localhost:8080 +``` + +You should see a "Hello, World!" message. + +## Inspect and Close + +To list information about the Unikraft instance, use: + +```bash +kraft ps -a +``` + +```text +NAME KERNEL ARGS CREATED STATUS MEM PORTS PLAT +upbeat_jenny project://httpserver-java25:qemu/x86_64 /usr/lib/jvm/java-25-runtime[...] 5 seconds ago running 2048M 0.0.0.0:8080->8080/tcp qemu/x86_64 +``` + +The instance name is `upbeat_jenny`. +To close the Unikraft instance, use: + +```bash +kraft rm upbeat_jenny +``` + +Note that closing the `kraft run` command (e.g., via `Ctrl+c`) does not kill the Unikraft instance. +If you want the Unikraft instance closed when closing the `kraft run` command, use the `--rm` option: + +```bash +kraft run --rm -M 1024M -p 8080:8080 --plat qemu --arch x86_64 . +``` + +## Build and Run Locally + +The commands so far used the pre-build Java image available in the Unikraft registry. + +In oder to to build a local Java image, clone this repository and `cd` into this directory. +Then use `kraft` to build an image locally: + +```bash +kraft build --no-cache --no-update --plat qemu --arch x86_64 +``` + +Similar to the `kraft run` command, if the `--plat` argument is left out, it defaults to `qemu`. +If the `--arch` argument is left out, it defaults to your system's CPU architecture. + +In order to run the locally built image, use `.` (_dot_, the current directory) as the final argument to the `kraft run` command: + +```bash +kraft run --rm -M 1024M -p 8080:8080 --plat qemu --arch x86_64 . +``` + +Same as above, it will open port `8080` and wait for connections. + +## `kraft` and `sudo` + +Mixing invocations of `kraft` and `sudo` can lead to unexpected behavior. +Read more about how to start `kraft` without `sudo` at [https://unikraft.org/sudoless](https://unikraft.org/sudoless). + +## Learn More + +- [How to run unikernels locally](https://unikraft.org/docs/cli/running) +- [How to build `Dockerfile` root filesystems with BuildKit](https://unikraft.org/docs/getting-started/integrations/buildkit) diff --git a/examples/httpserver-java25/SimpleHttpServer.java b/examples/httpserver-java25/SimpleHttpServer.java new file mode 100644 index 00000000..44c2b248 --- /dev/null +++ b/examples/httpserver-java25/SimpleHttpServer.java @@ -0,0 +1,29 @@ +// https://www.logicbig.com/tutorials/core-java-tutorial/http-server/http-server-basic.html + +import com.sun.net.httpserver.HttpContext; +import com.sun.net.httpserver.HttpExchange; +import com.sun.net.httpserver.HttpServer; +import java.io.IOException; +import java.io.OutputStream; +import java.net.InetSocketAddress; + +public class SimpleHttpServer { + private static final int listenPort = 8080; + + public static void main(String[] args) throws IOException { + HttpServer server = HttpServer.create(new InetSocketAddress(listenPort), 0); + HttpContext context = server.createContext("/"); + context.setHandler(SimpleHttpServer::handleRequest); + System.out.println("Waiting for HTTP connections on port " + listenPort + " ..."); + server.start(); + } + + private static void handleRequest(HttpExchange exchange) throws IOException { + String response = "Hello, World!\n"; + exchange.sendResponseHeaders(200, response.getBytes().length); // response code and length + OutputStream os = exchange.getResponseBody(); + os.write(response.getBytes()); + os.close(); + } + +} diff --git a/library/java/25/.dockerignore b/library/java/25/.dockerignore new file mode 100644 index 00000000..a154b6ff --- /dev/null +++ b/library/java/25/.dockerignore @@ -0,0 +1,3 @@ +/Makefile.uk +/.unikraft/ +/.config* diff --git a/library/java/25/.gitignore b/library/java/25/.gitignore new file mode 100644 index 00000000..a154b6ff --- /dev/null +++ b/library/java/25/.gitignore @@ -0,0 +1,3 @@ +/Makefile.uk +/.unikraft/ +/.config* diff --git a/library/java/25/Dockerfile b/library/java/25/Dockerfile new file mode 100644 index 00000000..bbcf370a --- /dev/null +++ b/library/java/25/Dockerfile @@ -0,0 +1,27 @@ +FROM openjdk:25-rc-oraclelinux9 AS build + +ENV JAVA_HOME=/usr/java/openjdk-25 + +RUN ldconfig $JAVA_HOME/lib/server/ + +WORKDIR /src + +COPY ./SimpleHttpServer.java /src/SimpleHttpServer.java + +RUN javac SimpleHttpServer.java + +FROM alpine:3 AS sys + +FROM scratch + +COPY --from=build /usr/lib64/libc.so.6 /usr/lib64/ +COPY --from=build /usr/lib64/libstdc++.so.6 /usr/lib64/ +COPY --from=build /usr/lib64/libm.so.6 /usr/lib64/ +COPY --from=build /usr/lib64/libz.so.1 /usr/lib64/ +COPY --from=build /usr/lib64/libgcc_s.so.1 /usr/lib64/ +COPY --from=build /lib64/ld-linux-x86-64.so.2 /lib64/ +COPY --from=build /etc/ld.so.cache /etc/ld.so.cache + +COPY --from=build $JAVA_HOME/ /usr/lib/jvm/java-25-runtime/ + +COPY --from=build /src/SimpleHttpServer.class /usr/src/SimpleHttpServer.class diff --git a/library/java/25/Kraftfile b/library/java/25/Kraftfile new file mode 100644 index 00000000..91bf7904 --- /dev/null +++ b/library/java/25/Kraftfile @@ -0,0 +1,129 @@ +spec: v0.6 + +name: httpserver-java25 + +rootfs: ./Dockerfile + +cmd: ["/usr/lib/jvm/java-25-runtime/bin/java", "-classpath", "/usr/src/", "SimpleHttpServer"] + +template: + source: https://github.com/unikraft/app-elfloader.git + version: staging + +unikraft: + source: https://github.com/unikraft/unikraft.git + version: staging + kconfig: + CONFIG_LIBPOSIX_PROCESS_ARCH_PRCTL: 'y' + CONFIG_APPELFLOADER_BRK: 'y' + CONFIG_APPELFLOADER_CUSTOMAPPNAME: 'y' + CONFIG_APPELFLOADER_STACK_NBPAGES: 128 + CONFIG_APPELFLOADER_VFSEXEC_EXECBIT: 'n' + CONFIG_APPELFLOADER_VFSEXEC: 'y' + CONFIG_APPELFLOADER_AUTOGEN_REPLACEEXIST: 'y' + + CONFIG_LIBPOSIX_PROCESS: 'y' + CONFIG_LIBPOSIX_PROCESS_MULTITHREADING: 'y' + CONFIG_LIBPTHREAD: 'y' + CONFIG_LIBPTHREAD_STACK_SIZE_MEGA: 'y' + CONFIG_LIBPTHREAD_STACK_SIZE: 16 + + CONFIG_HAVE_PAGING_DIRECTMAP: 'y' + CONFIG_HAVE_PAGING: 'y' + CONFIG_I8042: 'y' + CONFIG_LIBDEVFS_AUTOMOUNT: 'y' + CONFIG_LIBDEVFS_DEV_NULL: 'y' + CONFIG_LIBDEVFS_DEV_STDOUT: 'y' + CONFIG_LIBDEVFS_DEV_ZERO: 'y' + CONFIG_LIBDEVFS: 'y' + CONFIG_LIBPOSIX_ENVIRON_ENVP0: "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" + CONFIG_LIBPOSIX_ENVIRON_ENVP1: "LD_LIBRARY_PATH=/usr/local/lib:/usr/lib:/lib" + CONFIG_LIBPOSIX_ENVIRON_ENVP2: "HOME=/" + CONFIG_LIBPOSIX_ENVIRON: 'y' + CONFIG_LIBPOSIX_ENVIRON_LIBPARAM: 'y' + CONFIG_LIBPOSIX_ENVIRON_LIBPARAM_MAXCOUNT: '64' + CONFIG_LIBPOSIX_EVENTFD: 'y' + CONFIG_LIBPOSIX_FDIO: 'y' + CONFIG_LIBPOSIX_FDTAB: 'y' + CONFIG_LIBPOSIX_FUTEX: 'y' + CONFIG_LIBPOSIX_MMAP: 'y' + CONFIG_LIBPOSIX_PIPE: 'y' + CONFIG_LIBPOSIX_POLL: 'y' + CONFIG_LIBPOSIX_SOCKET: 'y' + CONFIG_LIBPOSIX_SYSINFO: 'y' + CONFIG_LIBPOSIX_TIME: 'y' + CONFIG_LIBPOSIX_TIMERFD: 'y' + CONFIG_LIBPOSIX_UNIXSOCKET: 'y' + CONFIG_LIBPOSIX_USER_GID: 0 + CONFIG_LIBPOSIX_USER_GROUPNAME: "root" + CONFIG_LIBPOSIX_USER_UID: 0 + CONFIG_LIBPOSIX_USER_USERNAME: "root" + CONFIG_LIBPOSIX_USER: 'y' + CONFIG_LIBRAMFS: 'y' + CONFIG_LIBSYSCALL_SHIM_HANDLER_ULTLS: 'y' + CONFIG_LIBSYSCALL_SHIM_HANDLER: 'y' + CONFIG_LIBSYSCALL_SHIM_LEGACY_VERBOSE: 'y' + CONFIG_LIBSYSCALL_SHIM: 'y' + CONFIG_LIBUKALLOCPOOL: 'y' + CONFIG_LIBUKBLKDEV_MAXNBQUEUES: '1' + CONFIG_LIBUKBLKDEV_DISPATCHERTHREADS: 'y' + CONFIG_LIBUKBLKDEV_SYNC_IO_BLOCKED_WAITING: 'y' + CONFIG_LIBUKBLKDEV: 'y' + CONFIG_LIBUKBOOT_BANNER_MINIMAL: 'y' + CONFIG_LIBUKBOOT_HEAP_BASE: '0x400000000' + CONFIG_LIBUKBOOT_MAINTHREAD: 'y' + CONFIG_LIBUKBOOT_SHUTDOWNREQ_HANDLER: 'y' + CONFIG_LIBUKCPIO: 'y' + CONFIG_LIBUKDEBUG_CRASH_SCREEN: 'y' + CONFIG_LIBUKDEBUG_ENABLE_ASSERT: 'y' + CONFIG_LIBUKDEBUG_PRINT_SRCNAME: 'n' + CONFIG_LIBUKDEBUG_PRINT_TIME: 'y' + CONFIG_LIBUKDEBUG_PRINTK_ERR: 'y' + CONFIG_LIBUKDEBUG_PRINTK: 'y' + CONFIG_LIBUKDEBUG: 'y' + CONFIG_LIBUKFALLOC: 'y' + CONFIG_LIBUKMPI: 'n' + CONFIG_LIBUKSIGNAL: 'y' + CONFIG_LIBUKRANDOM_DEVFS: 'y' + CONFIG_LIBUKRANDOM: 'y' + CONFIG_LIBUKRANDOM_GETRANDOM: 'y' + CONFIG_LIBUKVMEM_DEFAULT_BASE: '0x0000001000000000' + CONFIG_LIBUKVMEM_DEMAND_PAGE_IN_SIZE: 12 + CONFIG_LIBUKVMEM_PAGEFAULT_HANDLER_PRIO: 4 + CONFIG_LIBUKVMEM: 'y' + CONFIG_LIBVFSCORE_AUTOMOUNT_CI: 'y' + CONFIG_LIBVFSCORE_AUTOMOUNT_CI_EINITRD: 'y' + CONFIG_LIBVFSCORE_NONLARGEFILE: 'y' + CONFIG_LIBVFSCORE: 'y' + CONFIG_OPTIMIZE_DEADELIM: 'y' + CONFIG_OPTIMIZE_LTO: 'y' + CONFIG_PAGING: 'y' + CONFIG_STACK_SIZE_PAGE_ORDER: 4 + CONFIG_UKPLAT_MEMREGION_MAX_COUNT: 64 + +libraries: + lwip: + source: https://github.com/unikraft/lib-lwip.git + version: staging + kconfig: + CONFIG_LWIP_TCP: 'y' + CONFIG_LWIP_UDP: 'y' + CONFIG_LWIP_RAW: 'y' + CONFIG_LWIP_WND_SCALE: 'y' + CONFIG_LWIP_TCP_KEEPALIVE: 'y' + CONFIG_LWIP_THREADS: 'y' + CONFIG_LWIP_HEAP: 'y' + CONFIG_LWIP_SOCKET: 'y' + CONFIG_LWIP_AUTOIFACE: 'y' + CONFIG_LWIP_NUM_TCPCON: 64 + CONFIG_LWIP_NUM_TCPLISTENERS: 64 + CONFIG_LWIP_ICMP: 'y' + CONFIG_LWIP_DHCP: 'y' + CONFIG_LWIP_DNS: 'n' + libelf: + source: https://github.com/unikraft/lib-libelf.git + version: staging + +targets: +- fc/x86_64 +- qemu/x86_64 diff --git a/library/java/25/README.md b/library/java/25/README.md new file mode 100644 index 00000000..6c6369c3 --- /dev/null +++ b/library/java/25/README.md @@ -0,0 +1,83 @@ +# Java 25 HTTP Server + +This directory contains the [Java](https://www.java.com/en/) runtime on Unikraft, in binary compatibility mode. +It implements a simple HTTP server running on Unikraft that provides a simple response to each request. + +## Run and Use + +Use `kraft` to run the image and start a Unikraft instance: + +```bash +kraft run --rm -M 1024M -p 8080:8080 --plat qemu --arch x86_64 . +``` + +If the `--plat` argument is left out, it defaults to `qemu`. +If the `--arch` argument is left out, it defaults to your system's CPU architecture. + +Once executed, it will open port `8080` and wait for connections. +To test it, you can use `curl`: + +```bash +curl localhost:8080 +``` + +You should see a "Hello, World!" message. + +## Inspect and Close + +To list information about the Unikraft instance, use: + +```bash +kraft ps -a +``` + +```text +NAME KERNEL ARGS CREATED STATUS MEM PORTS PLAT +upbeat_jenny project://httpserver-java25:qemu/x86_64 /usr/lib/jvm/java-25-runtime[...] 5 seconds ago running 2048M 0.0.0.0:8080->8080/tcp qemu/x86_64 +``` + +The instance name is `upbeat_jenny`. +To close the Unikraft instance, use: + +```bash +kraft rm upbeat_jenny +``` + +Note that closing the `kraft run` command (e.g., via `Ctrl+c`) does not kill the Unikraft instance. +If you want the Unikraft instance closed when closing the `kraft run` command, use the `--rm` option: + +```bash +kraft run --rm -M 1024M -p 8080:8080 --plat qemu --arch x86_64 . +``` + +## Build and Run Locally + +The commands so far used the pre-build Java image available in the Unikraft registry. + +In oder to to build a local Java image, clone this repository and `cd` into this directory. +Then use `kraft` to build an image locally: + +```bash +kraft build --no-cache --no-update --plat qemu --arch x86_64 +``` + +Similar to the `kraft run` command, if the `--plat` argument is left out, it defaults to `qemu`. +If the `--arch` argument is left out, it defaults to your system's CPU architecture. + +In order to run the locally built image, use `.` (_dot_, the current directory) as the final argument to the `kraft run` command: + +```bash +kraft run --rm -M 1024M -p 8080:8080 --plat qemu --arch x86_64 . +``` + +Same as above, it will open port `8080` and wait for connections. + +## `kraft` and `sudo` + +Mixing invocations of `kraft` and `sudo` can lead to unexpected behavior. +Read more about how to start `kraft` without `sudo` at [https://unikraft.org/sudoless](https://unikraft.org/sudoless). + +## Learn More + +- [How to run unikernels locally](https://unikraft.org/docs/cli/running) +- [How to build `Dockerfile` root filesystems with BuildKit](https://unikraft.org/docs/getting-started/integrations/buildkit) diff --git a/library/java/25/SimpleHttpServer.java b/library/java/25/SimpleHttpServer.java new file mode 100644 index 00000000..44c2b248 --- /dev/null +++ b/library/java/25/SimpleHttpServer.java @@ -0,0 +1,29 @@ +// https://www.logicbig.com/tutorials/core-java-tutorial/http-server/http-server-basic.html + +import com.sun.net.httpserver.HttpContext; +import com.sun.net.httpserver.HttpExchange; +import com.sun.net.httpserver.HttpServer; +import java.io.IOException; +import java.io.OutputStream; +import java.net.InetSocketAddress; + +public class SimpleHttpServer { + private static final int listenPort = 8080; + + public static void main(String[] args) throws IOException { + HttpServer server = HttpServer.create(new InetSocketAddress(listenPort), 0); + HttpContext context = server.createContext("/"); + context.setHandler(SimpleHttpServer::handleRequest); + System.out.println("Waiting for HTTP connections on port " + listenPort + " ..."); + server.start(); + } + + private static void handleRequest(HttpExchange exchange) throws IOException { + String response = "Hello, World!\n"; + exchange.sendResponseHeaders(200, response.getBytes().length); // response code and length + OutputStream os = exchange.getResponseBody(); + os.write(response.getBytes()); + os.close(); + } + +}