I must run a class java by another program java use runtime.exec(). A class in folder diffefent with my program so i cannot run it. I used (java -cp "directory" class) but a my class has import file so it cannot filenotfound? Who can help me?
Process pro = Runtime.getRuntime().exec("java -cp \"E:\DA_Laptrinhmang\" NguyenTo");
Here is program:
import java.io.*;
import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;
public class TestCodeJava {
private static void printLines(String name, InputStream ins) throws Exception {
String line = null;
BufferedReader in = new BufferedReader( new InputStreamReader(ins));
while ((line = in.readLine()) != null) {
System.out.println(name + " " + line);
}
}
private static void runProcess(String command) throws Exception {
Process pro = Runtime.getRuntime().exec(command);
BufferedReader reader = new BufferedReader(new InputStreamReader(pro.getInputStream()));
pro.waitFor();
String line = "";
while((line = reader.readLine())!=null)
{
System.out.println(line);
}
}
private static int toCompile(String fileToCompile)
{
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
int compilationResult = compiler.run(null, null, null, "-verbose", fileToCompile);
return compilationResult;
}
public static void main(String[] args) {
try {
if(toCompile("E:\\Folder\\Prime.java")==0)
{
runProcess("java -cp \"E:\\Folder\" Prime");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
import java.io.*;
public class Prime {
private static boolean isPrime(int n)
{
if(n <= 1) return false;
if(n == 2 || n == 3) return true;
int m = (int) Math.sqrt(n);
for(int i = 5; i <= m; i = i + 5)
if(n % i == 0 || n % (i+2) == 0) return false;
return true;
}
public static void main(String args[]) throws FileNotFoundException, IOException
{
File file = new File("INPUT.TXT");
FileInputStream fis = new FileInputStream(file);
BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
int n = Integer.parseInt(reader.readLine());
File file2 = new File("OUTPUT.TXT");
FileOutputStream fos = new FileOutputStream(file2);
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(fos));
if(isPrime(n))
{
writer.write("YES");
}else writer.write("NO");
writer.flush();
writer.close();
}
}
here is error: [parsing started RegularFileObject[E:\DA_Laptrinhmang\NguyenTo.java]] [parsing completed 42ms] [search path for source files: C:\Users\QUANG HUY\Documents\NetBeansProjects\ChamDiemJava\build\classes,C:\Users\QUANG HUY\Documents\NetBeansProjects\ChamDiemJava\src] [search path for class files: C:\Program Files\Java\jdk1.7.0\jre\lib\resources.jar,C:\Program Files\Java\jdk1.7.0\jre\lib\rt.jar,C:\Program Files\Java\jdk1.7.0\jre\lib\sunrsasign.jar,C:\Program Files\Java\jdk1.7.0\jre\lib\jsse.jar,C:\Program Files\Java\jdk1.7.0\jre\lib\jce.jar,C:\Program Files\Java\jdk1.7.0\jre\lib\charsets.jar,C:\Program Files\Java\jdk1.7.0\jre\classes,C:\Program Files\Java\jdk1.7.0\jre\lib\ext\dnsns.jar,C:\Program Files\Java\jdk1.7.0\jre\lib\ext\localedata.jar,C:\Program Files\Java\jdk1.7.0\jre\lib\ext\sunec.jar,C:\Program Files\Java\jdk1.7.0\jre\lib\ext\sunjce_provider.jar,C:\Program Files\Java\jdk1.7.0\jre\lib\ext\sunmscapi.jar,C:\Program Files\Java\jdk1.7.0\jre\lib\ext\sunpkcs11.jar,C:\Program Files\Java\jdk1.7.0\jre\lib\ext\zipfs.jar,C:\Users\QUANG HUY\Documents\NetBeansProjects\ChamDiemJava\build\classes,C:\Users\QUANG HUY\Documents\NetBeansProjects\ChamDiemJava\src] [loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0\lib\ct.sym(META-INF/sym/rt.jar/java/lang/Object.class)]] [loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0\lib\ct.sym(META-INF/sym/rt.jar/java/lang/String.class)]] [loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0\lib\ct.sym(META-INF/sym/rt.jar/java/io/FileNotFoundException.class)]] [loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0\lib\ct.sym(META-INF/sym/rt.jar/java/io/IOException.class)]] [checking NguyenTo] [loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0\lib\ct.sym(META-INF/sym/rt.jar/java/lang/AutoCloseable.class)]] [loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0\lib\ct.sym(META-INF/sym/rt.jar/java/lang/Math.class)]] [loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0\lib\ct.sym(META-INF/sym/rt.jar/java/lang/Exception.class)]] [loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0\lib\ct.sym(META-INF/sym/rt.jar/java/lang/Throwable.class)]] [loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0\lib\ct.sym(META-INF/sym/rt.jar/java/io/File.class)]] [loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0\lib\ct.sym(META-INF/sym/rt.jar/java/net/URI.class)]] [loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0\lib\ct.sym(META-INF/sym/rt.jar/java/io/FileInputStream.class)]] [loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0\lib\ct.sym(META-INF/sym/rt.jar/java/io/FileDescriptor.class)]] [loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0\lib\ct.sym(META-INF/sym/rt.jar/java/io/BufferedReader.class)]] [loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0\lib\ct.sym(META-INF/sym/rt.jar/java/io/InputStreamReader.class)]] [loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0\lib\ct.sym(META-INF/sym/rt.jar/java/io/InputStream.class)]] [loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0\lib\ct.sym(META-INF/sym/rt.jar/java/io/Reader.class)]] [loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0\lib\ct.sym(META-INF/sym/rt.jar/java/lang/Integer.class)]] [loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0\lib\ct.sym(META-INF/sym/rt.jar/java/lang/Number.class)]] [loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0\lib\ct.sym(META-INF/sym/rt.jar/java/io/FileOutputStream.class)]] [loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0\lib\ct.sym(META-INF/sym/rt.jar/java/io/BufferedWriter.class)]] [loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0\lib\ct.sym(META-INF/sym/rt.jar/java/io/OutputStreamWriter.class)]] [loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0\lib\ct.sym(META-INF/sym/rt.jar/java/io/OutputStream.class)]] [loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0\lib\ct.sym(META-INF/sym/rt.jar/java/io/Writer.class)]] [loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0\lib\ct.sym(META-INF/sym/rt.jar/java/lang/Error.class)]] [loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0\lib\ct.sym(META-INF/sym/rt.jar/java/lang/RuntimeException.class)]] [loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0\lib\ct.sym(META-INF/sym/rt.jar/java/lang/NumberFormatException.class)]] [loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0\lib\ct.sym(META-INF/sym/rt.jar/java/lang/IllegalArgumentException.class)]] [wrote RegularFileObject[E:\DA_Laptrinhmang\NguyenTo.class]] [total 441ms] BUILD SUCCESSFUL (total time: 2 seconds)
"E:\\DA_Laptrinhmang NguyenTo")