2

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

ProcessBuilder pb = new ProcessBuilder("javaw",
    "-classpath", 
    binDir + "polyrd.jar;" + binDir + "lwjgl.jar;" + binDir + "lwjgl_util.jar;",
    "-Djava.library.path=" + nativesDir,
    "monster860.polyrd.Polyrd");

I tried doing the equivalent in the command line, changing it to -cp, just using bindir instead of binDir + "polyrd.jar;" + binDir + "lwjgl.jar;" + binDir + "lwjgl_util.jar;", and switching between java and javaw, but no matter what I did it gave me:

java.lang.NoClassDefFoundError: monster860/polyrd/Polyrd
Caused by: java.lang.ClassNotFoundException: monster860.polyrd.Polyrd
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source) 

Can anyone help?

My operating system is Windows Vista. Yes, those files actually exist.

Here's how I got binDir and nativesDir:

public ProcessRunnable(File nativesDir, File binDir) {
        try {
            this.nativesDir = nativesDir.getCanonicalPath() + File.separator;
            this.binDir = binDir.getCanonicalPath() + File.separator;
        } catch (IOException e) {
        // TODO Auto-generated catch block
            e.printStackTrace();
        }
}

EDIT: Well, of course, it's has absolutely nothing to do with this, but the downloader downloading only the first 2 KB of the file.

monster860
  • 124
  • 9

1 Answers1

1

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");
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • I had a hard time understanding that, but I tried `bindir + "*"` and it doesn't work. – monster860 Feb 27 '13 at 20:41
  • 1
    That's what it is, I used `File.getCanonicalPath()` and appended "/" to it, and assigned that as "binDir". – monster860 Feb 27 '13 at 20:50
  • Are you concatenating `getCanonicalPath()`, `/` and `*`? May you could update your question to show you current approach. – trashgod Feb 27 '13 at 20:55
  • Well, what I did is set `binDir` as `File.getCanonicalPath()`, and when trying your approach, I added `*` in the ProcessBuilder constructor. – monster860 Feb 27 '13 at 20:58
  • I've added a working example above; note my OS uses `:` to separate paths, yours uses `;`. – trashgod Feb 27 '13 at 21:06
  • Funny thing is: I tried to extract one of the classes with 7zip and it said unsupported compression method. – monster860 Feb 27 '13 at 21:09