using premade .java files

edited January 2018 in Library Questions

hi people, i know this question sounds a little weird (maybe too much weird)... i want to make some classes in processing and be able to use them in other processing apps... for example i write class A in processing... then open new processing sketch and use that java file (or maybe jar file) as an external file and make objects of that class... is it possible anyway? i know i can write class A and export it as a separate program and then run it externally from another program. but can i make objects dynamically from another java file, and of course externally? thanks in advance

Answers

  • https://github.com/processing/processing/wiki/Library-Basics

    Probly check the section about Creating jar files for your library, almost at the end of the prev link.

    Kf

  • thanks a lot kf... now i see my problem better.. can i use any library from the program? i mean do the importing process when the program is running... for example, my program read the name of a library from a text file (while running) and then try to load and use that? this is exactly what i'm looking for :-)

  • In Java some approaches to dynamic loading of jars at runtime are difficult by design because it is considered a security issue.

    This discussion of approaches might help:

  • edited January 2018

    wow thanks jeremy, that was a great hint... so how can i use this method in processing? because in every processing sketch, the output is setup-draw program of jar type. how can i use it? or should i make libraries (or even pure java classes) ?

  • Looking back over your question, I think that maybe I misunderstood you -- are you

    1. Trying to have the user of a running sketch open a file dialog while the sketch is running, pick a jar file, and then load classes from the jar that do something? or
    2. Trying to reuse code from compiled jars? The second is extremely easy -- build the jar, then drag it into your sketch to add it to the sketch. Reference its class members as normal with import.
  • edited January 2018

    it's a mixed of these two question, but i want it to be done while the program is running, or when the program is restarted... for example read new libraries' names from a text file, and then load them and use their functions... making the program extensible... so i can add new features to the program without rewriting and recompiling the main code...

  • for example read new libraries' names from a text file, and then load them and use their functions...

    Problem; how do you know what classes and functions are available? The most direct way to do do that is to use Java reflection but that is quite advanced programming. Even this will give you a list of classes and methods that you could use but your main compiled program has to understand what a particular method is supposed to do before executing it.

    so i can add new features to the program without rewriting and recompiling the main code...

    The JVM which executes a Java program only accepts compiled classes so why not just import any jars that you might need before compiling? You don't have to use them!

    making the program extensible...

    Assuming you are creating the jars to be dynamically loaded then create the classes so they can be extended (i.e. use OO inheritance) to modify the class behaviour. Many of the classes in the G4P library allow this and this has been used to provide bug fixes by users between versions.

  • Another way to approach this is to use a language that makes it much easier to do runtime reimport for live coding. I would recommend Python, but I believe that Processing.py is compiled, so I'm not sure whether reload would work.

    I'm also not sure whether such a limitation might also apply to JRubyArt.

  • Another way to approach this is to use a language that makes it much easier to do runtime reimport for live coding.

    Java bundles Nashorn JS: https://Forum.Processing.org/two/discussions/tagged?Tag=#Nashorn

  • Does Nashorn enable live runtime JavaScript importing / re-importing?

  • AFAIK, Nashorn's imports are fully dynamic at runtime. ~O)

    In the link below, class PApplet is imported via Java.type(): \m/
    https://Forum.Processing.org/two/discussion/21709/code-evaluation-during-program-execution#Item_3

    I've also made some useful functions to deal w/ Nashorn more easily: :bz
    https://Forum.Processing.org/two/discussion/15151/how-to-convert-string-to-a-line-of-code#Item_9

  • Problem; how do you know what classes and functions are available?

    i want to make those new classes myself. so i will try to make a standard for writing them, for example all those classes should have same setting up function and same draw function, this way i can call those classes, instantiate objects from them and use those objects in my main program without recompiling...

  • Another way to approach this is to use a language that makes it much easier to do runtime reimport for live coding...

    that would be a solution, but i've written bunch of code in processing in java mode now, and doing that would cost me a lot of time...

  • AFAIK, Nashorn's imports are fully dynamic at runtime....

    can i use this Nashorn in my current program (written in java mode)?

  • can i use this "URLClassLoader" that Jeremy suggested before? that sounds like a good answer to my needs....

  • Can I use this Nashorn in my current program (written in Java Mode)?

    Well, as I've already stated, Nashorn is bundled in Java. So it's ready available. :)>-

    You can dynamically import and use your Java classes from within Nashorn code. ~O)

  • thanks GoToLoop, so this means i can use Nashorn directly from processing IDE?

  • Yea, it's JS language inside Java. Just try out the examples from my 2 forum links. :-bd

  • Thanks again soooooooooooooooo much... i will try this method, hope i get the desired result... i will post a sample code later too. thanks a lot

  • ... all those classes should have same setting up function and same draw function, ...

    If they've got the same 2 methods, you can make an abstract class a as base for your other subclasses. *-:)

Sign In or Register to comment.