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.

PDF in EJS simulations post and replies

Return to the PDF in EJS simulations thread
Login to post to this thread

How to see a PDF file from an EJS sim
Juan Maria Fernandez
11 Posts

I need to launch the PDF reader and show a PDF file from an EJS java simulator.

I can do it-I have the code ready and working- but now I want to include the PDF file as a resource in the jar file. It is possible with sound and gif files (e.g, the SeeSaw simulator of Paco).

Is there any way to do it ?


Replies to How to see a PDF file from an EJS sim

Re: How to see a PDF file from an EJS sim -
Francisco Esquembre
228 Posts

Juan,

If I am not wrong, the OS PDF reader requires that the file is actually on your disk. Since the file will be on the JAR file, you need to extract it before trying to read it.

The org.colos.ejs.library.Simulation class has a couple of utilities for this. The first one is this:

/**
Finds the file in the jar file and extracts it to a temporary file.
The file will be deleted on exit
*/
static public File getResourceFile(String _filename);

/**
Extracts the file to a target directory.
The file will not be deleted on exit.
*/
static public File extractToDirectory(String _resourceString, File _targetDirectory, boolean _verbose);

/**
Similar to the previous one, but changes the name to filename.
*/
static public File extractAs(String _resourceString, String filename, boolean _verbose);

I use it for my own internal purposes, but have not tried on PDFs.

If this does not work, I can try to do something new to help.

Paco



Re: Re: How to see a PDF file from an EJS sim -
Juan Maria Fernandez
11 Posts

Thank you Paco.

But I am unable to get

static public File extractToDirectory(String _resourceString, File _targetDirectory, boolean _verbose);


to work. Could you, please, give me an example of how to call extractToDirectory in an actual ejs sim?


in fact, I have no idea about what is the File_targetDirectory parameter: is a string?, how do you write it?


my last erroneous try was

org.colos.ejs.library.Simulation.extractToDirectory("/_data/Readme.pdf", "./_data", false);


***************+

Vamos, que no tengo ni p... idea sobre como escribir el directorio de destino de la resourceString, y creo que ya estoy autolobotomizado de tanto buscarlo por los javadocs...



Got it!! -
Juan Maria Fernandez
11 Posts

Got it!!


I have just now concocted some java code to extract a resource PDF file from the  simulator's jar file, and then read it with the system default PDF reader.

I will put another tread with the complete solution with comments and a better code indentation than the actual.

Post edited December 16, 2014 at 5:26 AM EST.



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