Accessing the jar inside a jar plugin

Hello,
I am currently trying to build a custom plugin from example plugin git repo. Basic use of this plugin is onces the analysis starts our plugin also trigger an analysis.

I created a processbuilder for the same

ProcessBuilder processBuilder = new ProcessBuilder(
                    "java", "-jar", jarPath_From_Resources_Dir,
                    "--rulesDir",  Some_Rules_from_Resources,
                    "--appPath", jar_path_that_want_to_analyze,
                    "--reportFormat", "SARIF",
                    "--reportPath", output_path);

Problem:
I put all our resources including main_jar and rules inside resources direactory. However, I am not able to access the jar file path.

What could be the problem here ? I already tried to extract the resources in temp directory and it’s working but I think there should be more concreate solution to this problem.

Thanks

Hey there.

What method are you using (for example, of those documented here) to construct the path to your jar?

Yes I have tried this method like below to get path

private String getResourceUrl(String resourceName) throws IOException {
        URL resourceUrl = getClass().getClassLoader().getResource(resourceName);
        if (resourceUrl == null) {
            throw new FileNotFoundException("Resource not found: " + resourceName);
        }

        return resourceUrl.toURI();
    }

but this still gives me error while I tried to run process builder

Error : can not access the file at given path

For what it’s worth, this code let me access a .jar file in the src/main/resources directory of my sample Java project.

package com.example;

import java.io.File;
import java.net.URL;

/**
 * Hello world!
 *
 */
public class App {
    public static void main(String[] args) {
        URL resourceUrl = App.class.getClassLoader().getResource("SSLPoke.jar");
        if (resourceUrl != null) {
            try {
                File jarFile = new File(resourceUrl.toURI());
                ProcessBuilder pb = new ProcessBuilder("java", "-jar", jarFile.getAbsolutePath());
                pb.inheritIO();
                Process p = pb.start();
                int exitCode = p.waitFor();
                System.out.println("Process exited with code: " + exitCode);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
}

I think without a complete sample project from you, it will be hard to debug further. This is probably also a general Java coding question better suited for a forum like StackOverflow (since it’s not involving any specific SonarQube APIs)