I run this method in normal java program (eclipse and cmd) successfully, but when I run this method in Tomcat 8.5.9, it shows error(Error: Could not find or load main class Main). Why that happened? Thanks for help.
public synchronized static String run(SourceCode sourceCode) {
try {
ProcessBuilder java = new ProcessBuilder("java", "Main");
File file = new File("C:\\apache-tomcat-8.5.9\\webapps\\onlineJudge\\data\\Piers\\1\\1483703529849");
java = java.directory(file);
java = java.redirectErrorStream(true);
Process process = java.start();
List<String> list = java.command();
try (Scanner in = new Scanner(process.getInputStream())) {
System.out.println(in.nextLine());
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
Dir: C:\apache-tomcat-8.5.9\webapps\onlineJudge\data\Piers\1\148370352984
File in Dir: Main.class, Main.java(code below)
public class Main{
public static void main(String args[]){
System.out.println("Hello World.");
}
}
PS:
How I test:
Copy this method to another normal Java Project and test. It shows (Hello World.).
Something Wired
After I post this question, I run Tomcat in Eclipse. Before, I run Tomcat in cmd(use startup command). When load the method(public synchronized static String run(SourceCode sourceCode)) it runs successfully! What happened?
Thanks!
I am the author. I find that Tomcat changed my classpath for compling java! It uses its own classpath for security.