0

I am implementing the ProcessBuilder program that calls an another java program. However, I am getting class not found.

The program simply produces the following output:

Error: Could not find or load main class HelloWorld

Program complete

public class ProcessBuilderSample { public static void main(String args[]) { try { ProcessBuilder broker = new ProcessBuilder("java.exe", "-cp", "F:\\LunaWorkspace\\ProcessBuilderTest\\bin" ,"com\\hello\\HelloWorld"); Process runBroker = broker.start(); BufferedReader reader = new BufferedReader(new InputStreamReader(runBroker.getInputStream())); BufferedReader reader1 = new BufferedReader(new InputStreamReader(runBroker.getErrorStream())); String str=null; while((str=reader.readLine())!=null){ System.out.println(str); } while((str=reader1.readLine())!=null){ System.out.println(str); } runBroker.waitFor(); System.out.println("Program complete"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } 

This is the java program that I would like to execute. This program produce Hello World as output.

package com.hello; public class HelloWorld { public static void main(String arg[]){ System.out.println("Hello World"); } } 

Now I am using:

ProcessBuilder broker = new ProcessBuilder("java.exe", "-cp", "F:\LunaWorkspace\ProcessBuilderTest\bin" ,"com\hello\HelloWorld");

This command works on command prompt, but not working with processbuilder.

EDIT:

Full classpath:

ProcessBuilderSample.class:

F:\LunaWorkspace\ProcessBuilderExample\bin\com\sample

HelloWorld.class:

F:\LunaWorkspace\ProcessBuilderTest\bin\com\hello Thanks!!

10
  • call runBroker.getErrorStream() and read the error you got Commented Dec 2, 2015 at 19:31
  • Actually I want to execute it Commented Dec 2, 2015 at 19:33
  • cool will delete the comment :) now verify that you can run that command you are passing to Process builder from command line (make sure you are trying to execute from the same place you are executing ProcessBuilderSample Commented Dec 2, 2015 at 19:48
  • yeah, command is working from the command prompt but from processbuilder. Error produced by ProcessBuilder, Error: Could not find or load main class com\hello\HelloWorld Commented Dec 2, 2015 at 19:50
  • are you compiling this from scratch every time? remove your class files and try again. you will see you have unresolved symbol runBroker Commented Dec 2, 2015 at 19:59

1 Answer 1

1

Need to fix you HelloWorld class name in constructing your process builder:

"com\\hello\\HelloWorld" -> "com.hello.HelloWorld"

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.