Results 1 to 19 of 19
  1. #1
    diolu is offline Member
    Join Date
    Jan 2017
    Posts
    26
    Rep Power
    0

    Default Malking a library optional in Java

    I have written a java software that need to open images (a colorblind simulator). The standard javafx class to open images (Image) only supports a limited amount of formats (jpg, png and gif). Open sources external libraries exist on the web to open other image formats (like: https://bitbucket.org/luciad/webp-imageio). What I would like is to be able to test at run time if the additional library is installed, if yes use it to decode the image. But if not, use the standard java Class to at least open the supported formats. The reason is simple. The user could download my jar file and run it with the standard java installed and nothing else. If (s)he wants to open additional formats, (s)he can install additional libraries.

    How can we achieve this in Java? Somehow I would like to have the analogous of the function dlopen in C.
    Last edited by diolu; 03-27-2017 at 10:59 AM.

  2. #2
    gozzy is offline Member
    Join Date
    Jan 2017
    Posts
    46
    Rep Power
    0

    Default Re: Malking a library optional in Java

    If you don't want to make an installer to install > 1 jar then you can use a 'fat jar' technique that will enable any extra libraries to be placed in the same jar

  3. #3
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,983
    Rep Power
    8

    Default Re: Malking a library optional in Java

    First of all the library should be in your application's classpath. Tell the user to drop the library of their choice in the lib folder of your application. Or add it in the batchfile that you use it to start the application.

    Then in the application itself, you can use Class.forName("com.pacakge.library.ClassName") to detect if the class is actually there. So if Class.forName() returns a Class object, then it's there, otherwise it will throw a ClassNotFoundException which you have to handle.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  4. #4
    gozzy is offline Member
    Join Date
    Jan 2017
    Posts
    46
    Rep Power
    0

    Default Re: Malking a library optional in Java

    You can also distribute via Java Web Start, which will handle any number of jars, though that will give you code-signing/security issues

  5. #5
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,983
    Rep Power
    8

    Default Re: Malking a library optional in Java

    Quote Originally Posted by gozzy View Post
    You can also distribute via Java Web Start, which will handle any number of jars, though that will give you code-signing/security issues
    What you seem to keep forgetting is that OP has stated that the USER supplies the library, not OP, so fat jars and Java WebStart are not options.

    Java Webstart does not have code-signing/security issues. You only have issues when you have no money (or not willing) to spend on a Code Signing Certificate. Now you can't sign your code and Java Webstart won't let you run it for damn good reasons too. This is not Java Webstart's fault, it's yours.

    A quick DuckduckGo search reveals that Code Signing Certificates will sell from /$ 150-300 per year. If you spend that, you have no issues. If you don't spend it, Java WebStart is not your option, but don't blame that on JWS.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  6. #6
    gozzy is offline Member
    Join Date
    Jan 2017
    Posts
    46
    Rep Power
    0

    Default Re: Malking a library optional in Java

    Why would it be the user's responsibility to have some obscure image library? The likelihood of its already being installed i would guess as being vanishingly small

  7. #7
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,983
    Rep Power
    8

    Default Re: Malking a library optional in Java

    Quote Originally Posted by gozzy View Post
    Why would it be the user's responsibility to have some obscure image library? The likelihood of its already being installed i would guess as being vanishingly small
    I think that's exactly the reason why the user is responsible for adding the library: the image library is obscure. So even though OP supports the image type, there is no need to include the library for all his other 99% of the customers.

    This is what OP says:
    If (s)he wants to open additional formats, (s)he can install additional libraries.
    Say OP supports 150 image formats. JPG, GIF, PNG support is built in, but he codes additional support for the other 147. I understand why he doesn't ship all 147 libraries, because it increases his application JAR/download/ from 20KB to 40MB. If the user wants support for say Windows ICO format, he needs to add the library that allows him to do that. The application wil detect the presence of the ICO-file support class and allows him to open it. Everybody happy.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  8. #8
    gozzy is offline Member
    Join Date
    Jan 2017
    Posts
    46
    Rep Power
    0

    Default Re: Malking a library optional in Java

    There's something to be said in principle for only getting additional libraries as needed, but it's not going to be made easily practicable. For instance the webp-imageio mentioned requires native libraries. They would need an installer, if only a home-brewed one. Of course Java Web Start can handle that painlessly too

  9. #9
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,983
    Rep Power
    8

    Default Re: Malking a library optional in Java

    There is a way through the middle: OP can host a website for his product and offer the libraries over HTTPS when the user choses an obscure format. The client will download and copy the jar into its own lib-folder and tell the user to restart the application to activate it.

    I don't think Webstart is able to handle lazy loading of jar files like that. I believe it downloads all the jars you specify right at the start.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Malking a library optional in Java

    Seems to me that using reflection and a customized class loader would negate the need to restart the app. I know it's possible with classes. Not certain why you couldn't do it with libraries.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  11. #11
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,983
    Rep Power
    8

    Default Re: Malking a library optional in Java

    I'm just assuming a restart is needed. If you add all the libraries in a lib directory to the classpath when you start the application, it won't be on the classpath until the next restart. If you could circumvent that with a custom classloader, it sounds like you're in business. Would be interesting to see the code for that. I never had the need for coding my own classloaders. So, @OP, get coding! ;)
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,964
    Rep Power
    31

    Default Re: Malking a library optional in Java

    interesting to see the code for that. I never had the need for coding my own classloaders.
    I wrote one long ago for some reason. One use I made of it was in a program named: ExecJava that had a table of java programs and the files and args they needed to for executing. It loaded and executed programs I selected in the one JVM it was executing in. My justification for the project was that the JVM took soo long to load on my PC that I hoped to save that load time by reusing the one JVM for multiple programs. I still use it from my browser via the browser command:
    javascript:document.location = 'ExecJava:"D:/JavaDevelopment/runtime ExecuteJava.jar"'
    that is set on my bookmarks toolbar.
    If you're interested, I'll upload the code to some site.

    I just found the jar file I had uploaded years ago:
    https://sourceforge.net/directory/os...?q=executejava
    Last edited by Norm; 03-27-2017 at 05:08 PM.
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    gozzy is offline Member
    Join Date
    Jan 2017
    Posts
    46
    Rep Power
    0

    Default Re: Malking a library optional in Java

    You could address this problem with modules, but you might have to wait until Java 9 ;)
    https://jcp.org/en/jsr/detail?id=376
    It's conceivable it might be done now using OSGI, but i'm afraid i know nothing about it really
    Last edited by gozzy; 03-27-2017 at 05:17 PM.

  14. #14
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,983
    Rep Power
    8

    Default Re: Malking a library optional in Java

    Quote Originally Posted by Norm View Post
    If you're interested, I'll upload the code to some site.
    I would like to see that please. If you don't want to disclose the source in public, send me a PM so I can reply with my emailaddress.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,964
    Rep Power
    31

    Default Re: Malking a library optional in Java

    I downloaded the jar file at the link to sourceforge and took a look inside it to see when I'd written it. There was code from 2001. The main code was from 2008.
    I'll put the source on a site and post the URL.

    http://normsstuff.x10host.com/ExecuteJava.zip

    Some really old code I wrote before Swing provided the classes for the functionality I wanted.
    Last edited by Norm; 03-27-2017 at 10:52 PM.
    If you don't understand my response, don't ignore it, ask a question.

  16. #16
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,983
    Rep Power
    8

    Default Re: Malking a library optional in Java

    Quote Originally Posted by Norm View Post
    I downloaded the jar file at the link to sourceforge and took a look inside it to see when I'd written it. There was code from 2001. The main code was from 2008.
    I'll put the source on a site and post the URL.

    http://normsstuff.x10host.com/ExecuteJava.zip

    Some really old code I wrote before Swing provided the classes for the functionality I wanted.
    Thanks Norm, I appreciate it. Even though this might be outdated by now, I'll try and learn something from it. Cheers!
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  17. #17
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,983
    Rep Power
    8

    Default Re: Malking a library optional in Java

    On a second note: it's very quiet from OP's side. Hey diolu, you have got a nice discussion going. Care to join us?
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  18. #18
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Malking a library optional in Java

    D0n't know whether this one of mine would be useful here: https://tips4java.wordpress.com/2008...-class-loader/

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  19. #19
    gozzy is offline Member
    Join Date
    Jan 2017
    Posts
    46
    Rep Power
    0

    Default Re: Malking a library optional in Java

    FileClassLoader extends java.net.URLClassLoader to provide useful functionality for loading a class from its compiled class file.
    Can't you just use a url with the file protocol? ( file:///..........)

Similar Threads

  1. Replies: 1
    Last Post: 03-02-2012, 05:40 PM
  2. I want to put the native library in library path in mac os x
    By Hussain Ali in forum Advanced Java
    Replies: 2
    Last Post: 02-24-2010, 07:55 AM
  3. Replies: 3
    Last Post: 09-07-2009, 01:28 PM
  4. regex problem - allowing optional space
    By Norm in forum Advanced Java
    Replies: 26
    Last Post: 10-06-2008, 04:38 PM
  5. Tell me jar file for library library org.bouncycastle.cms
    By 82rathi.angara in forum New To Java
    Replies: 10
    Last Post: 09-09-2008, 05:11 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •