benfry / processing4

Processing 4.x releases for Java 17
https://processing.org
Other
1.34k stars 239 forks source link

sketchPath is different on OSX then Windows #808

Open clankill3r opened 10 months ago

clankill3r commented 10 months ago

Processing 4.3 (and prior) Windows 10

When working outside the IDE it's common to put the libraries in a lib folder like: (even when not using processing this is common)

sketchPath

The above works fine on OSX, but on windows it does not work. The reason it breaks on windows: https://github.com/benfry/processing4/blob/main/core/src/processing/core/PApplet.java#L7186

        if (jarPath.contains("/lib/")) {
          // Windows or Linux, back up a directory to get the executable
          folder = new File(jarPath, "../..").getCanonicalPath();
        }

It is assumed that core.jar is directly in the lib folder, in the above case sketchPath becomes rootOfProject/lib and dataPath becomes rootOfProject/lib/data.

For now I use this in my sketch:

    public void fixSketchPath() {
        try {
            Field sketchPathField = PApplet.class.getDeclaredField("sketchPath");
            sketchPathField.setAccessible(true);

            String sketchPath = (String) sketchPathField.get(this);

            if (sketchPath.contains("\\lib")) {
                int libIndex = sketchPath.indexOf("\\lib");
                sketchPath = sketchPath.substring(0, libIndex);
            }

            sketchPathField.set(this, sketchPath);

        } catch (NoSuchFieldException | IllegalAccessException e) {
            e.printStackTrace();
        }
    }

But this is of course more a work around then a fix.

aayushk9 commented 10 months ago

@clankill3r I would like to work on this. May I?

clankill3r commented 10 months ago

I won't stop you :)