Java very unusual classpath problems

前端 未结 1 727
时光说笑
时光说笑 2020-12-21 16:13

I\'m trying to run an application that has native libraries and stuff using the following code:

ProcessBuilder pb = new ProcessBuilder(\"javaw\",
    \"-clas         


        
相关标签:
1条回答
  • 2020-12-21 16:33

    Since Java 6, "As a special convenience, a class path element containing a basename of * is considered equivalent to specifying a list of all the files in the directory with the extension .jar or .JAR". See the java command-line options for details.

    Addendum: This example starts JFreeChart using the wildcard feature mentioned.

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    
    /** @see https://stackoverflow.com/a/15121864/230513 */
    public class PBTest {
    
        private static final String baseDir = "/opt/jfreechart/";
        public static void main(String[] args) {
            ProcessBuilder pb = new ProcessBuilder("java", "-cp",
                baseDir + "lib/*:" + baseDir + "jfreechart-1.0.14-demo.jar",
                "demo.SuperDemo");
            pb.redirectErrorStream(true);
            try {
                Process p = pb.start();
                String s;
                // read from the process's combined stdout & stderr
                BufferedReader stdout = new BufferedReader(
                    new InputStreamReader(p.getInputStream()));
                while ((s = stdout.readLine()) != null) {
                    System.out.println(s);
                }
                System.out.println("Exit value: " + p.waitFor());
                p.getInputStream().close();
                p.getOutputStream().close();
                p.getErrorStream().close();
            } catch (Exception ex) {
                ex.printStackTrace(System.err);
            }
        }
    }
    

    Addendum: Here's the changes for Windows, which requires ; as a path separator.

    private static final String baseDir = "C:/Users/Public/JFreeChart/";
    public static void main(String[] args) {
        ProcessBuilder pb = new ProcessBuilder("java", "-cp",
            baseDir + "lib/*;" + baseDir + "jfreechart-1.0.14-demo.jar",
            "demo.SuperDemo");
    
    0 讨论(0)
提交回复
热议问题