Java
Java is a high-level, general-purpose, memory-safe, object-oriented programming language.
In LiveCodes, Java runs in the browser using DoppioJVM.
Usage
Demo:
show code
- JS
- TS
- React
- Vue
- Svelte
- Solid
import { createPlayground } from 'livecodes';
const options = {
"config": {
"activeEditor": "script",
"script": {
"language": "java",
"content": "public class BinarySearchSnippet {\n /**\n * Search an item with binarySearch algorithm.\n *\n * @param arr sorted array to search\n * @param item an item to search\n * @return if item is found, return the index position of the array item otherwise return -1\n */\n\n public static int binarySearch(int[] arr, int left, int right, int item) {\n if (right >= left) {\n int mid = left + (right - left) / 2;\n if (arr[mid] == item) {\n return mid;\n }\n\n if (arr[mid] > item) {\n return binarySearch(arr, left, mid - 1, item);\n }\n\n return binarySearch(arr, mid + 1, right, item);\n }\n return -1;\n }\n\n public static void main(String[] args) {\n int[] sortedArray = {1, 3, 5, 7, 9, 11, 13, 15};\n int itemToSearch = 7;\n\n int result = binarySearch(sortedArray, 0, sortedArray.length - 1, itemToSearch);\n\n if (result == -1) {\n System.out.println(\"Result: Item not found in the array.\");\n } else {\n System.out.println(\"Result: Item found at index -> \" + result);\n }\n }\n}\n"
},
"mode": "simple",
"editor": "auto",
"tools": {
"status": "full"
}
}
};
createPlayground('#container', options);
import { createPlayground, type EmbedOptions } from 'livecodes';
const options: EmbedOptions = {
"config": {
"activeEditor": "script",
"script": {
"language": "java",
"content": "public class BinarySearchSnippet {\n /**\n * Search an item with binarySearch algorithm.\n *\n * @param arr sorted array to search\n * @param item an item to search\n * @return if item is found, return the index position of the array item otherwise return -1\n */\n\n public static int binarySearch(int[] arr, int left, int right, int item) {\n if (right >= left) {\n int mid = left + (right - left) / 2;\n if (arr[mid] == item) {\n return mid;\n }\n\n if (arr[mid] > item) {\n return binarySearch(arr, left, mid - 1, item);\n }\n\n return binarySearch(arr, mid + 1, right, item);\n }\n return -1;\n }\n\n public static void main(String[] args) {\n int[] sortedArray = {1, 3, 5, 7, 9, 11, 13, 15};\n int itemToSearch = 7;\n\n int result = binarySearch(sortedArray, 0, sortedArray.length - 1, itemToSearch);\n\n if (result == -1) {\n System.out.println(\"Result: Item not found in the array.\");\n } else {\n System.out.println(\"Result: Item found at index -> \" + result);\n }\n }\n}\n"
},
"mode": "simple",
"editor": "auto",
"tools": {
"status": "full"
}
}
};
createPlayground('#container', options);
import LiveCodes from 'livecodes/react';
export default function App() {
const options = {
"config": {
"activeEditor": "script",
"script": {
"language": "java",
"content": "public class BinarySearchSnippet {\n /**\n * Search an item with binarySearch algorithm.\n *\n * @param arr sorted array to search\n * @param item an item to search\n * @return if item is found, return the index position of the array item otherwise return -1\n */\n\n public static int binarySearch(int[] arr, int left, int right, int item) {\n if (right >= left) {\n int mid = left + (right - left) / 2;\n if (arr[mid] == item) {\n return mid;\n }\n\n if (arr[mid] > item) {\n return binarySearch(arr, left, mid - 1, item);\n }\n\n return binarySearch(arr, mid + 1, right, item);\n }\n return -1;\n }\n\n public static void main(String[] args) {\n int[] sortedArray = {1, 3, 5, 7, 9, 11, 13, 15};\n int itemToSearch = 7;\n\n int result = binarySearch(sortedArray, 0, sortedArray.length - 1, itemToSearch);\n\n if (result == -1) {\n System.out.println(\"Result: Item not found in the array.\");\n } else {\n System.out.println(\"Result: Item found at index -> \" + result);\n }\n }\n}\n"
},
"mode": "simple",
"editor": "auto",
"tools": {
"status": "full"
}
}
};
return (<LiveCodes {...options}></LiveCodes>);
}
<script setup>
import LiveCodes from "livecodes/vue";
const options = {
"config": {
"activeEditor": "script",
"script": {
"language": "java",
"content": "public class BinarySearchSnippet {\n /**\n * Search an item with binarySearch algorithm.\n *\n * @param arr sorted array to search\n * @param item an item to search\n * @return if item is found, return the index position of the array item otherwise return -1\n */\n\n public static int binarySearch(int[] arr, int left, int right, int item) {\n if (right >= left) {\n int mid = left + (right - left) / 2;\n if (arr[mid] == item) {\n return mid;\n }\n\n if (arr[mid] > item) {\n return binarySearch(arr, left, mid - 1, item);\n }\n\n return binarySearch(arr, mid + 1, right, item);\n }\n return -1;\n }\n\n public static void main(String[] args) {\n int[] sortedArray = {1, 3, 5, 7, 9, 11, 13, 15};\n int itemToSearch = 7;\n\n int result = binarySearch(sortedArray, 0, sortedArray.length - 1, itemToSearch);\n\n if (result == -1) {\n System.out.println(\"Result: Item not found in the array.\");\n } else {\n System.out.println(\"Result: Item found at index -> \" + result);\n }\n }\n}\n"
},
"mode": "simple",
"editor": "auto",
"tools": {
"status": "full"
}
}
};
</script>
<template>
<LiveCodes v-bind="options" />
</template>
<script>
import { onMount } from 'svelte';
import { createPlayground } from 'livecodes';
let options = $state({
"config": {
"activeEditor": "script",
"script": {
"language": "java",
"content": "public class BinarySearchSnippet {\n /**\n * Search an item with binarySearch algorithm.\n *\n * @param arr sorted array to search\n * @param item an item to search\n * @return if item is found, return the index position of the array item otherwise return -1\n */\n\n public static int binarySearch(int[] arr, int left, int right, int item) {\n if (right >= left) {\n int mid = left + (right - left) / 2;\n if (arr[mid] == item) {\n return mid;\n }\n\n if (arr[mid] > item) {\n return binarySearch(arr, left, mid - 1, item);\n }\n\n return binarySearch(arr, mid + 1, right, item);\n }\n return -1;\n }\n\n public static void main(String[] args) {\n int[] sortedArray = {1, 3, 5, 7, 9, 11, 13, 15};\n int itemToSearch = 7;\n\n int result = binarySearch(sortedArray, 0, sortedArray.length - 1, itemToSearch);\n\n if (result == -1) {\n System.out.println(\"Result: Item not found in the array.\");\n } else {\n System.out.println(\"Result: Item found at index -> \" + result);\n }\n }\n}\n"
},
"mode": "simple",
"editor": "auto",
"tools": {
"status": "full"
}
}
});
let container = $state(null);
onMount(() => {
createPlayground(container, options);
});
</script>
<div bind:this="{container}"></div>
import { createPlayground, type EmbedOptions } from 'livecodes';
export default function App() {
const options: EmbedOptions = {
"config": {
"activeEditor": "script",
"script": {
"language": "java",
"content": "public class BinarySearchSnippet {\n /**\n * Search an item with binarySearch algorithm.\n *\n * @param arr sorted array to search\n * @param item an item to search\n * @return if item is found, return the index position of the array item otherwise return -1\n */\n\n public static int binarySearch(int[] arr, int left, int right, int item) {\n if (right >= left) {\n int mid = left + (right - left) / 2;\n if (arr[mid] == item) {\n return mid;\n }\n\n if (arr[mid] > item) {\n return binarySearch(arr, left, mid - 1, item);\n }\n\n return binarySearch(arr, mid + 1, right, item);\n }\n return -1;\n }\n\n public static void main(String[] args) {\n int[] sortedArray = {1, 3, 5, 7, 9, 11, 13, 15};\n int itemToSearch = 7;\n\n int result = binarySearch(sortedArray, 0, sortedArray.length - 1, itemToSearch);\n\n if (result == -1) {\n System.out.println(\"Result: Item not found in the array.\");\n } else {\n System.out.println(\"Result: Item found at index -> \" + result);\n }\n }\n}\n"
},
"mode": "simple",
"editor": "auto",
"tools": {
"status": "full"
}
}
};
const onMounted = (container: HTMLElement) => {
createPlayground(container, options);
};
return <div ref={onMounted}></div>;
}
Communication with JavaScript
The Java code runs in the context of the result page.
A few helper properties and methods are available in the browser global livecodes.java object:
livecodes.java.input: the initial standard input that is passed to the Java code.livecodes.java.loaded: A promise that resolves when the Java environment is loaded. Any other helpers should be used after this promise resolves.livecodes.java.output: the standard output.livecodes.java.error: the standard error.livecodes.java.exitCode: the exit code.livecodes.java.run: a function that runs the Java code with new input. This function takes a string as input and returns a promise that resolves when the Java code is done running. The promise resolves with an object containing theinput,output,error, andexitCodeproperties.
Example:
show code
- JS
- TS
- React
- Vue
- Svelte
- Solid
import { createPlayground } from 'livecodes';
const options = {
"template": "java",
"params": {
"activeEditor": "markup"
}
};
createPlayground('#container', options);
import { createPlayground, type EmbedOptions } from 'livecodes';
const options: EmbedOptions = {
"template": "java",
"params": {
"activeEditor": "markup"
}
};
createPlayground('#container', options);
import LiveCodes from 'livecodes/react';
export default function App() {
const options = {
"template": "java",
"params": {
"activeEditor": "markup"
}
};
return (<LiveCodes {...options}></LiveCodes>);
}
<script setup>
import LiveCodes from "livecodes/vue";
const options = {
"template": "java",
"params": {
"activeEditor": "markup"
}
};
</script>
<template>
<LiveCodes v-bind="options" />
</template>
<script>
import { onMount } from 'svelte';
import { createPlayground } from 'livecodes';
let options = $state({
"template": "java",
"params": {
"activeEditor": "markup"
}
});
let container = $state(null);
onMount(() => {
createPlayground(container, options);
});
</script>
<div bind:this="{container}"></div>
import { createPlayground, type EmbedOptions } from 'livecodes';
export default function App() {
const options: EmbedOptions = {
"template": "java",
"params": {
"activeEditor": "markup"
}
};
const onMounted = (container: HTMLElement) => {
createPlayground(container, options);
};
return <div ref={onMounted}></div>;
}
Language Info
Name
java
Extension
.java
Editor
script
Compiler
Version
DoppioJVM: v0.5.0, which runs Java 8 JDK.
Code Formatting
Using Prettier with the Prettier Java plugin.
Live Reload
By default, new code changes are sent to the result page for re-evaluation without a full page reload, to avoid the need to reload the Java environment.
This behavior can be disabled by adding the code comment // __livecodes_reload__ to the code, which will force a full page reload.
This comment can be added in the hiddenContent property of the editor for embedded playgrounds.
Starter Template
https://livecodes.io/?template=java