run java from terminal
//Run this line to compile
javac programName.java
//Run this line to run
java programName
run java from terminal
//Run this line to compile
javac programName.java
//Run this line to run
java programName
how to make java run terminal commands
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ExecuteShellCommandRuntimeExec {
public static void main (String [] args) {
try {
Process process = Runtime.getRuntime().exec("ls -l");
StringBuilder output = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader (process.getInputStream()));
String line;
while((line = reader.readLine()) != null) {
output.append(line + "\n");
}
int exitVal = process.waitFor();
if (exitVal == 0) {
System.out.println("Success");
System.out.println(output);
System.exit(0);
} else {
System.out.println("Something abnormal has haapened :( ");
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
how to compile a java program in terminal
//Change directory into where all the java files are
javac *.java
//Chagne direcotry into com
java com.company.Main
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us