1

I am trying to make a customized IntelliJ plugin and run a shell script file stored in src/main/resources folder from a java code file in the main folder.

I need Help with a way to execute and change the contents of files in the resources folder using configured paths.

Tried these ways so far


//Method - 1

String filePath = getClass().getClassLoader().getResource("file.sh").getPath();
System.out.println(filePath);
String[] paths = filePath.split(":");
System.out.println(paths[1]);

//Method-2

URL res = getClass().getClassLoader().getResource("file.sh");
File script = null;
try {
    assert res != null;
    script = Paths.get(res.toURI()).toFile();
} catch (URISyntaxException e) {
    throw new RuntimeException(e);
}
String absolutePath = script.getAbsolutePath();

//Method-3

final ClassLoader classLoader = getClass().getClassLoader();
final File script = new File(classLoader.getResource("file.sh").getFile());
System.out.println(script.getPath());



But all these lead to a path file:/Users/username/projects/projectname/build/idea-sandbox/plugins/project-name/lib/Project-Name-1.0-SNAPSHOT.jar!/file.sh

devblack.exe
  • 428
  • 4
  • 17
  • You are running the application from a jar, therefore the shell script is not a file, it's just a resource, an entry in a jar. You can getResourceAsStream and copy the stream to a temporary file and use that. – k314159 Nov 24 '22 at 09:25
  • I doubt you should be involving shell scripting in a plugin – g00se Nov 24 '22 at 09:55

0 Answers0