APS Excellence in Physics Education Award
November 2019

Education Prize Logo
Science SPORE Prize
November 2011

NSF Logo
The Open Source Physics Project is supported by NSF DUE-0442581.

Extracting Resources (PDF and Sound) from jar files

Return to the EJS Modeling forum
Login to post to this thread

The idea and the code
Juan Maria Fernandez
11 Posts

In my simulators I am triying to include a sound file-a beep for Detectors-and a PDF file.
Getting them in the jar file is easy in EJS, you just use the Run Options, and add the User Files and the Files required ( in case of html). I was unable to find how to extract files from the jar in the javadocs of EJS. I think the information is there, but I was plainly unable to understand it.

So I got it my way: I want to extract, in runtime of the simulator, the sound and PDF files. They are beep.wav and Readme.pdf, both located in ./_data, with ./ the directory were the jar file is.

The code is



*************
public void setFile(InputStream io, String fileName)
throws IOException {
  FileOutputStream fos = new FileOutputStream(fileName);
  byte[] buf = new byte[256];
  int read = 0;
  while ((read = io.read(buf)) > 0) {
    fos.write(buf, 0, read);
  }
  fos.close();
} // end setFile

public void ExtractPDFandSound() {
  try {
    new File ("_data").mkdir();
    InputStream i =getClass().getResourceAsStream("/Juansource/Wheeler/_data/Readme.pdf");
    setFile(i,"./_data/Readme.pdf");
    i.close();
    i =getClass().getResourceAsStream("/Juansource/Wheeler/_data/beep.wav");
    setFile(i,"./_data/beep.wav");
    i.close();
    extractedResources=true;
  }
  catch (IOException ex) {
    System.out.println("Error Unpacking jar");
    extractedResources=false;
  }
} // end extract PDFandSound


public void ReadPDF() {
  if (Desktop.isDesktopSupported()) {
    try {
      File myFile = new File("./_data/Readme.pdf");
      Desktop.getDesktop().open(myFile);
    } catch (IOException ex) {
      // no application registered for PDFs
      System.out.println("Error reading PDF");
    }
  } //endif
} // end ReadPDF


public void sound () {
  if (allowSound) {
    try {
      java.applet.AudioClip clip =java.applet.Applet.newAudioClip(new java.net.URL("file:"+"./_data/beep.wav"));
      clip.play();
    }
    catch (java.net.MalformedURLException murle) {
      System.out.println(murle);
    }
  }
} // end sound



public void ResourcesdoExists() {
  File WaveFile = new File ("./_data/beep.wav");

  File PDFFile = new File ("./_data/Readme.pdf");

  if (WaveFile.exists() &&(!WaveFile.isDirectory())  && PDFFile.exists() &&(!PDFFile.isDirectory())) {
    extractedResources=true;
  }
} // end ResourcesdoExists




*************

The solution can be refined, but it works for me.

/Juansource/Wheeler/_data is the internal structure of directories in the jar file. To adapt the code, you have to change that to your personal structure.

extractedResources is a boolean variable. If true the resources already are in the disk and you don't need extract them anymore.


I am almost sure that a better way, only with relative paths, is possible, sorry for showing you the quick and dirty version, today I have no time for niceties!

Hope this will be useful.

I am using this in a new simulator about  the Wheeler's Delayed Choice Experiment in a Quantum Mechanics Mach-Zhender interferometer.

Post edited December 23, 2014 at 1:39 PM EST.


OSP Projects:
Open Source Physics - EJS Modeling
Tracker
Physlet Physics
Physlet Quantum Physics
STP Book