Issue executing a java compiled file from java source

42 views Asked by At

I am trying to execute a java compiled file from another java program, and I am having some issues.

When I run from my terminal the command java -cp ".:lib/MyLib.jar" javaFiles/g1/MyCompiledProgram I can execute MyCompiledProgram without any issue.

But when I try to execute the same command from code using the following method:

Process process = Runtime.getRuntime().exec(String.format("java -cp  \".:%s\" javaFiles/g1/MyCompiledProgram",Path.of(PATH_MYLIB)));
String error = null;
process.waitFor();
if(process.exitValue() != 0){
    try(Scanner scanner = new Scanner(process.getErrorStream())){
        error = scanner.useDelimiter("\\A").next();
    }
    System.out.println(error);
}

I get a ClassNotFoundException error. I checked that the directory java was using to execute the command was correct (running the pwd command) and it is the correct one.

Does anyone has any idea why is not finding the class? Thanks :)

0

There are 0 answers