【发布时间】:2021-05-08 18:05:51
【问题描述】:
我使用 SpringBoot 创建了一个 Java 项目,并希望通过 Get-Request 显示所有可用的 shell 脚本。 shell 脚本在包内:'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;
}
使用这种方法,我可以在特定包中找到类,但它不适用于 shell 脚本。你知道shell脚本相当于"java.class.path"吗?
String[] classPathEntries =
System.getProperty("java.class.path").split(
System.getProperty("path.separator")
);
【问题讨论】:
-
到目前为止你实现了什么?粘贴您的控制器类。如果还没有实现,请先实现。提示:制作一个简单的 hello world 控制器,向您的请求返回 hello。然后,替换容器内的代码以读取目录中的所有文件并检查文件扩展名。制作一组或列表并显示它们。您也可以使用 POJO 来表示每个文件。
-
@Ashish 再问一个问题。 “shell脚本”的名称结尾是什么。就像 java 一样,它可以是 .jar、.class。
-
@Volnick "shell script" 通常暗示着一个类 unix 的环境。类 Unix 系统并不真正关心扩展。但习惯上使用
.sh。 -
@FedericoklezCulloca 我编辑了我的问题。该方法适用于 java 类,但直到现在我找不到任何 shell 脚本。请查看我问题的最后一部分。
标签: java spring-boot get sh backend