Assuming you have a package structure in your application, for example

The following code should work
import java.io.InputStream;
import javafx.scene.text.Font;
public class AccessTest {
public static void main(String[] args) throws URISyntaxException, MalformedURLException {
InputStream is = AccessTest.class.getResourceAsStream("OpenSans-Regular.ttf");
Font font = Font.loadFont(is, 12.0);
System.out.println("Font: " +font);
File f = new File(AccessTest.class.getResource("OpenSans-Regular.ttf").toURI());
Font font1 = Font.loadFont(f.toURL().toExternalForm(), 12.0);
System.out.println("Font 1: " + font1);
Font font2 = Font.loadFont(f.toURI().toURL().toExternalForm(), 12.0);
System.out.println("Font 2: " + font2);
}
}
This gives me the following result:
Font: Font[name=Open Sans, family=Open Sans, style=Regular, size=12.0]
Font 1: Font[name=Open Sans, family=Open Sans, style=Regular, size=12.0]
Font 2: Font[name=Open Sans, family=Open Sans, style=Regular, size=12.0]
If you place a font file outside of your package, there may be an access violation. In your case, the Font.load () method simply splits the appropriate input stream into the construct. It would be a little difficult to get a suitable URL or URI that uses the load method.
Nwdev source
share