I have created a Java project with SpringBoot and want a Get-Request to display all available shell scripts. The shell scripts are inside the package: 'scripts'.
import org.springframework.web.bind.annotation.*;
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;
@RestController
@RequestMapping("api/overview")
public class DME {
String packageName = "de.osp.scriptrunnerbackend.script";
@GetMapping
public List<Class<?>> getClassesInPackage() {
String path = packageName.replaceAll("\\.", File.separator);
List<Class<?>> classes = new ArrayList<>();
String[] classPathEntries = System.getProperty("java.class.path").split(
System.getProperty("path.separator")
);
String name;
for (String classpathEntry : classPathEntries) {
if (classpathEntry.endsWith(".sh")) {
File file = new File(classpathEntry);
try {
JarInputStream is = new JarInputStream(new FileInputStream(file));
JarEntry entry;
while((entry = is.getNextJarEntry()) != null) {
name = entry.getName();
if (name.endsWith(".sh")) {
if (name.contains(path) && name.endsWith(".sh")) {
String classPath = name.substring(0, entry.getName().length() - 6);
classPath = classPath.replaceAll("[\\|/]", ".");
classes.add(Class.forName(classPath));
}
}
}
} catch (Exception ex) {
// Silence is gold
}
} else {
try {
File base = new File(classpathEntry + File.separatorChar + path);
for (File file : base.listFiles()) {
name = file.getName();
if (name.endsWith(".sh")) {
name = name.substring(0, name.length() - 6);
classes.add(Class.forName(packageName + "." + name));
}
}
} catch (Exception ex) {
// Silence is gold
}
}
}
return classes;
}
With this method i can find classes inside specific packages, but it isnt working for shell scripts. Do u know the equivalent of "java.class.path"
for a shell script?
String[] classPathEntries =
System.getProperty("java.class.path").split(
System.getProperty("path.separator")
);