0

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)

6
  • 1
    And what errors are you getting? Why isn't it working? Have you tried running it from the command line from the same context as you application would be...? Commented Mar 19, 2014 at 2:41
  • I try run code java in E disk by cmd, but i can't because my class has import file. I need exect code java by another programe with runtime.exect(). Commented Mar 19, 2014 at 2:43
  • I see what appears to be a part of a classpath (improperly escaped) but no main class or jar file. Commented Mar 19, 2014 at 2:43
  • Can you use a ProcessBuilder? That makes escaping the commandline a lot less tedious (e.g. "E:\\DA_Laptrinhmang NguyenTo") Commented Mar 19, 2014 at 2:45
  • Alright, that statement made no sense. If you can't execute the class from the command line, what makes you think you'll be able to do it from your program? Commented Mar 19, 2014 at 2:45

1 Answer 1

2

this works for me

package mypackage;

import java.io.IOException;

public class RunSomeJava {

    public static void main(String[] args) throws IOException {
        Process pro = Runtime.getRuntime().exec("java -cp E:\\workspaceIndigo2\\StackOverflow\\bin mypackage.SomeOtherProgram");
    }

}

and

package mypackage;

import java.io.File;
import java.io.IOException;

public class SomeOtherProgram {

    public static void main(String[] args) throws IOException {
        File f = new File(System.currentTimeMillis()+".txt");
        f.createNewFile();
    }

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

3 Comments

you've modified the question, it's another question
this was not nice, mr @Student
No, i sorry, but i try description my question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.