- Published on
Compile and Run Java Code
- Authors
- Name
- DP Piggy
- @xiaozhudxiaozhu
Why I sometimes compile and run java code manually than using IDE ?
To improve the level of my whiteboard programming.
Let's get started!
Assuming your code file structure is as follows:
/path/to/code
└── sorts
└── BubbleSort.java
Navigate to Code Folder
cd /path/to/code
Compile Code: Compile your Java code with the package statement using the
javac
command:javac sorts/BubbleSort.java
Run Code: Run your compiled Java code
java -cp . sorts.BubbleSort
-cp .
sets the classpath to the current folder.
sorts.BubbleSort
is your complete class name, including the package.
For instance:
- My code file structure
/e/workspace/code/vscode_projects/java-algo-ds
- create a directory in
java-algo-ds
, create a Java file named BubbleSort.java insorts
directory
mkdir sorts
vim BubbleSort.java
- write code like the follows
package sorts;
import java.util.*;
public class BubbleSort {
public static void main(String[] args) {
int[] unorderedArray = {2, 3, 1, 1, 4, 3, 5, 3};
System.out.println("unordered array: " + Arrays.toString(unorderedArray));
bubbleSort(unorderedArray);
System.out.println("ordered array: " + Arrays.toString(unorderedArray));
}
public static void swapElementInArray(int[] array, int firstIndex, int secondIndex) {
if (array == null || array.length == 0) {
throw new IllegalArgumentException("The input array cannot be null or empty");
}
if (firstIndex < 0 || firstIndex >= array.length || secondIndex < 0 || secondIndex >= array.length) {
throw new IndexOutOfBoundsException("Invalid index values");
}
if (firstIndex != secondIndex) {
int tempElement = array[firstIndex];
array[firstIndex] = array[secondIndex];
array[secondIndex] = tempElement;
}
}
public static void bubbleSort(int[] array) {
if (array == null) {
throw new IllegalArgumentException("The input array cannot be null");
}
if (array.length < 2) {
return;
}
int length = array.length;
for (int i = length - 1; i > 0; i--) {
boolean hasSwapped = false;
for (int j = 0; j < i; j++) {
if (array[j] > array[j + 1]) {
swapElementInArray(array, j, j + 1);
hasSwapped = true;
}
}
if (!hasSwapped) {
break;
}
}
}
}
- Compile and run the code
➜ java-algo-ds (master) pwd
/e/workspace/code/vscode_projects/java-algo-ds
➜ java-algo-ds (master) javac sorts/BubbleSort.java
➜ java-algo-ds (master) java -cp . sorts.BubbleSort
unordered array: [2, 3, 1, 1, 4, 3, 5, 3]
ordered array: [1, 1, 2, 3, 3, 3, 4, 5]