Skip to content

Instantly share code, notes, and snippets.

@alexmozaidze
Created November 12, 2025 21:24
Show Gist options
  • Select an option

  • Save alexmozaidze/6208193ee702ba2043052524f9d745f8 to your computer and use it in GitHub Desktop.

Select an option

Save alexmozaidze/6208193ee702ba2043052524f9d745f8 to your computer and use it in GitHub Desktop.
// Discovering the path of JVM (if the server's `java` executable isn't the right version)
import java.io.File;
public class HackyLs {
public static void main(String[] args) {
// Input the directory you want to list here
String jvmDirectoryPath = "/opt/";
File jvmDir = new File(jvmDirectoryPath);
System.out.println("Attempting to list contents of: " + jvmDirectoryPath);
if (!jvmDir.exists()) {
System.err.println("Error: The directory does not exist: " + jvmDirectoryPath);
System.exit(1);
}
if (!jvmDir.isDirectory()) {
System.err.println("Error: The path is not a directory: " + jvmDirectoryPath);
System.exit(1);
}
File[] files = jvmDir.listFiles();
if (files == null) {
// This can happen due to file permission issues
System.err.println("Error: Failed to read directory contents. (Check permissions?)");
System.exit(1);
}
if (files.length == 0) {
System.out.println("The directory is empty: " + jvmDirectoryPath);
} else {
System.out.println("Found the following items:");
for (File file : files) {
// Print the name, adding a / if it's a directory to make it clearer
System.out.println(file.getName() + (file.isDirectory() ? "/" : ""));
}
}
}
}
// Running the AdvancedBackups mod from the right directory and handing over user input to it.
import java.io.File;
import java.io.IOException;
public class RunRestore {
public static void main(String[] args) {
String targetDirectoryPath = "./mods/";
String targetJarName = "AdvancedBackups-fabric-1.20-3.7.1.jar";
String javaPath = "/opt/jdk-17.0.1/bin/java";
// --- 2. Directory Validation ---
File workingDir = new File(targetDirectoryPath);
if (!workingDir.exists() || !workingDir.isDirectory()) {
System.err.println("Error: The specified directory does not exist or is not a directory:");
System.err.println(targetDirectoryPath);
System.exit(1);
}
System.out.println("Attempting to run '" + targetJarName + "' from directory '" + targetDirectoryPath + "'...");
ProcessBuilder pb = new ProcessBuilder(javaPath, "-jar", targetJarName);
pb.directory(workingDir);
try {
pb.inheritIO();
Process process = pb.start();
int exitCode = process.waitFor();
System.out.println("Target program finished with exit code: " + exitCode);
} catch (IOException e) {
// This can happen if 'java' isn't a valid command or the .jar isn't found
System.err.println("Error starting the process: " + e.getMessage());
e.printStackTrace();
} catch (InterruptedException e) {
// This can happen if the waiting thread is interrupted
System.err.println("Process was interrupted: " + e.getMessage());
Thread.currentThread().interrupt(); // Restore the interrupted status
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment