Exporting an application that's dependant on external resources?

edited October 2013 in How To...

Hi I built a program that loads images from a specific directory, and my question is how do I make it independent of a fixed directory on my machine. For example if I want to export this program and run it on a different machine? I looked into getting the current directory using the "File" class but it looks like that only gives you the directory of the JVM.

To be clear this is in regards to exporting a stand-alone application. Or is my best option to just install processing on the other machine and run it that way?

Answers

  • edited October 2013

    Any resources should accompany a program's folder!
    In Processing, it's advisable to use dataFolder() dataPath("") to get /data folder path.

  • Except that that method doesn't seem to exist anywhere! Could you point me to it in the Javadoc? I don't think I'm looking in the right place.

    So if I export an app, I should go in and create a "data" folder, and then copy all of my resources there is what you're saying?

  • I have been struggling with the same issue. I tried using a program such as Jar2Exe to provide a wrapper to combine the .pde, images, music, etc into one executable file but I have been unsuccessful in doing so. Using the File, Export Application; plus copying the data files will operate on another machine.

  • Yea! Placing resources in /data folder would make your program portable! B-)

  • edited October 2013

    Ok, well this brings me back to my original question I asked a few weeks ago- how do I load all the images from my data folder? I am using this code from Amnon right now to load an absolute path-

    import java.io.*;
    
    String[] filenames;
    PImage[] images;
    String fullPath = "/Users/MyName/Documents/Projects/Processing/MyProject/images";
    filenames = loadFilenames(fullPath);
    images = new PImage[filenames.length];
      for(int j = 0; j < filenames.length; j++){
        String file = fullPath + "/" + filenames[j];
        images[j] = loadImage(file);
      }
    
    String[] loadFilenames(String path) {
      File folder = new File(path);
      FilenameFilter filenameFilter = new FilenameFilter() {
        public boolean accept(File dir, String name) {
          return name.toLowerCase().endsWith(".png"); 
        }
      };
      return folder.list(filenameFilter);
    }
    

    This won't work if I pass "/data" because the File object needs an absolute path. Right? So how would I do this? That code is correct in my program btw I just didnt bother adding setup() here

  • edited October 2013

    Haven't I tipped about dataFolder() dataPath("") function above at my 1st reply? :-t
    Don't forget that Processing is only aware of its folders (and canvas resolution) once setup() is reached!

  • edited October 2013

    I know but like I said, every time I run dataFolder() it tells me that the function doesn't exist! I'm assuming it is part of the PApplet class right? I also can't find any documentation of it. That is a good emoticon lol

  • Answer ✓

    I agree with GoToLoop, the data folder is the best way to attach resources to your exported application. When they are in there, you can load them in your app as relative to your export.

    For example, if you have an 'images' folder inside of data, you should be able to do:

    String relativePath = "images/image.png";
    

    Another useful method might be the System.getProperty("user.home") which will add the path on the hard drive to your home folder, independent of the os. So for your example above, you could use:

    String homeFolder = System.getProperty("user.home");
    String fullPath = homeFolder + "/Documents/Projects/Processing/MyProject/images";
    

    Hope this helps! ak

  • This library may have some helpful tools for dealing with files: http://www.sojamo.de/libraries/drop/

  • edited October 2013 Answer ✓

    Oops! It's dataPath(""), not dataFolder("")!!! :@)
    It's undocumented. That's why we can't find it at Reference section! b-(

    void setup() {
      println(dataPath(""));
      println(System.getProperty("user.home"));
      exit();
    }
    

    Another snippet to better know about Java's own environment: o->

    println( javaVersionName );
    println( System.getProperty("java.home") + "\n");
    
    println( System.getProperty("os.arch") );
    println( System.getProperty("os.name") );
    println( System.getProperty("os.version") + "\n");
    
    println( System.getProperty("user.home") );
    println( System.getProperty("user.dir") );
    println( dataPath("") );
    
    exit();
    
  • edited October 2013

    Ok cool, that works perfectly. Thanks to both of you for helping me out!

    So Processing has undocumented functions too?! How is anyone supposed to know about them then??

  • Ah jeez, I looked at the java doc but I must have missed it. I'm pretty new to Java though so I am still getting used to reading those. Thanks for making me check again haha, every little bit helps

Sign In or Register to comment.