Hitachi Vantara Pentaho Community Forums
Results 1 to 28 of 28

Thread: Invoking Java program using Javascript

  1. #1
    Join Date
    Jan 2008
    Posts
    13

    Default Invoking Java program using Javascript

    Hi all,
    I want to call a standalone Java program through Javascript using a Job in Kettle.

    am calling using the below statement in Javascript,
    Packages.FeedsEdit;

    my java class file is present in the folder,
    D:\Workspace\FileHandling\com\aol\ch

    and below is my classpath setting in Spoon.bat
    set CLASSPATH=%CLASSPATH%;D:\Workspace\FileHandling\com\aol\ch

    the Java program contains a main() method. and the program execution needs to start with the main() method.
    I tried setting the classpath in command and it was working correctly. Everything looks fine but still the Java program is not getting invoked. Can someone please help me out?

    Thanks,
    Jayanth.

  2. #2
    Join Date
    Nov 1999
    Posts
    9,729

    Default

    • Put the class file in a java archive, called a jar file
    Code:
    javac FeedsEdit.java
    jar -cf feedsedit.jar FeedsEdit.class
    • Put the jar file in the classpath of Kettle (libext/)
    Code:
    cp feedsedit.jar /foo/bar/kettle/libext/
    • Execute the class' main method in a JavaScript step or job entry:
    Code:
    Packages.Feed.main(null);
    
    true;
    HTH,

    Matt

  3. #3
    Join Date
    Feb 2008
    Posts
    13

    Default

    Hi!! could you repeat please what is the right code I should include in a Javascript step to execute a jar file from a job?

    Thanks!!
    Last edited by lgtleon; 08-21-2008 at 11:24 AM.

  4. #4
    DEinspanjer Guest

    Default

    He said it right there!
    Packages.Feed.main(null);
    true;

  5. #5
    Join Date
    Sep 2008
    Posts
    4

    Default RE: invoking java program using javascript

    I follow the above directions in MattCasters' post and still get the following error in Kettle when trying to run the job:

    Couldn't compile javascript: org.mozilla.javascript.EcmaError: TypeError: main is not a function, it is org.mozilla.javascript.NativeJavaPackage. (<cmd>#3)

    this is using a very basic Java program:

    import java.io.*;
    public class FeedsEdit {
    public static void main(String args[]) {
    System.out.println("in main");
    }
    }

    (except instead of
    Packages.Feed.main(null);
    I use Packages.FeedsEdit.main(null); because I'm assuming there's a typo in that post - please correct me if I'm wrong)

    What might be the problem?

    Thanks.
    -m

  6. #6
    Join Date
    Sep 2008
    Posts
    1

    Default

    Might be that you need to specify a package in your java class

  7. #7
    Join Date
    Nov 1999
    Posts
    9,729

    Default

    It's recommended, not required to use packages in Java code.

  8. #8

    Default

    Hi markw,
    did you write that:
    Quote Originally Posted by markw View Post
    Code:
    import java.io.*;
    public class FeedsEdit {
        public static void main(String args[]) {
     System.out.println("in main");
        }
    }
    into the Javascript step or in a file with name FeedsEdit.java (and compiled and jar'ed it)?

    Did you try a "java;" as first statement in Javascript step?

    Christoph

  9. #9
    Join Date
    Sep 2008
    Posts
    4

    Default

    I've tried both specifying a package and not, both of which result in the same error message.

    The java code that I posted was in a separate java file called FeedsEdit.java that I compiled and jar'ed... and I did put "java;" at the beginning of my javascript step.

    Here is what I have in the Javascript window:

    java;
    Packages.FeedsEdit.main(null);
    true;

    Any other thoughts?

    Thanks!
    -m

  10. #10
    Join Date
    Nov 1999
    Posts
    9,729

    Default

    I don't think you can simply execute a main() method, try using a regular public static final method.

  11. #11
    Join Date
    Sep 2008
    Posts
    4

    Default

    Quote Originally Posted by MattCasters View Post
    I don't think you can simply execute a main() method, try using a regular public static final method.
    That seems to work - I wrote another static method in that same class (a non-main method) and the job ran without an error.
    Thanks everyone.
    -m

  12. #12

    Default invoke java code from script

    • Put the jar file in the classpath of Kettle (libext/)
    Code:
    cp feedsedit.jar /foo/bar/kettle/libext/
    Hi,

    Can you specify the full class path of kettle.
    Im new to kettle..And when i tried puting the jar file in the classpath , i got the following error:

    "cp" is not recognised as an internal or external command,operable program or batch file.

    Why is it not working?


    Thanks,
    Vidhya

  13. #13
    Join Date
    Nov 1999
    Posts
    9,729

    Default

    You need a real operating system like Linux, Solaris, OSX, etc.
    The poor folks that are forced to use Microsoft software can try the "copy" command and learn the art of using backslashes.

  14. #14
    Join Date
    May 2008
    Posts
    232

    Default

    Hi...

    By default, the .jar file is getting created in the same directory of as that of .class file.. What is the need of keeping it again in the same directory?


    Thanks,
    Meena

  15. #15

    Unhappy Problem persists......!

    Hi,
    Even i tried creating a non main method and tried calling that method.but it dint work for me....I got the following error:

    Couldn't execute this script! Error on position (3:0) :
    Java class "FeedsEdit" has no public instance field or method named "display". (script#3)


    The following is my java code:
    public
    class FeedsEdit

    {

    publicstaticvoid main(String args[]) {

    FeedsEdit obj=
    new FeedsEdit();

    System.
    out.println(display());
    }


    staticpublicint display()
    {
    int a=33;
    return a;

    }

    }

    Can you tell me why is it not working...

    Thanks
    vidhya




  16. #16
    Join Date
    Nov 1999
    Posts
    9,729

    Default

    Here is the example:

    FeedsEdit.zip

    Compile FeedsEdit.java :

    Code:
    javac FeedsEdit.java
    Create the jar file:

    Code:
    jar cf FeedsEdit.jar FeedsEdit.class
    Put the file FeedsEdit.jar somewhere in the libext/ folder of your Kettle distribution.
    Then start (or restart) Spoon.

    The JavaScript entry has a single line:

    Code:
    var d = Packages.FeedsEdit.display();
    Finally, I think that this thread has gone on long enough, it turned into a Java beginners course, so let's end it right here and now.

    Matt
    Last edited by MattCasters; 09-11-2008 at 04:26 AM.

  17. #17

    Default Java

    I'm intersting to this post!

    I follow all steps but Spoon not know my java class...what's happened?!


    Ciao,
    Roberto

  18. #18
    Join Date
    Sep 2008
    Posts
    28

    Default Error on running the sample FeedsEdit.ktr

    Hi

    I understand the java,javascript, steps related to compiling and creating jar steps in the feedsedit example. I am very new to Kettle (infact the entire pentaho suit). I am not sure how can I view the output.

    I used the same FeedsEdit.java file given in the zip folder, compiled n jar-ed it put it in the libext folder.
    Started Spoon.bat, in the IDE choose the 'Modifying Java Script Value step', wrote the script as mentioned in this forum - var d = Packages.FeedsEdit.display();

    If i just run this step, it runs with no errors, but I am not sure, how can I view the output? I tried using a text file output as the next step and connected the two steps with a hop. But the ouput isn't displayed in the text file.

    Also then I tried opening the FeedsEdit.ktr (from the zip file) in the IDE and when I ran it, it got the following error:

    org.pentaho.di.core.exception.KettleException:
    Transformation was unable to open [FeedsEdit]
    An error occured reading a transformation from the repository
    This file doesn't exist : FeedsEdit

    at org.pentaho.di.trans.Trans.<init>(Trans.java:205)
    at org.pentaho.di.ui.spoon.trans.TransLog.start(TransLog.java:540)
    at org.pentaho.di.ui.spoon.delegates.SpoonTransformationDelegate.executeTran

    ...

    I keep getting this error with any sample transformations I try in the sample folder as well.

    Also, I do not understand what exactly does the Generate Rows step do? What is it used for? In order to run my transformation do I need to give Generate Rows as an input step and what should I do to view the output.

    These questions may be really trivial, but a small explanation or guidance would really help me.

    Thanks.
    AK

  19. #19
    DEinspanjer Guest

    Default

    At the end of your JS step, var d contains whatever text the java program outputted. You need to go into the Fields list at the bottom of the JS step dialog and put d into the fields. Then you'll be able to preview or write the data out to a file, etc.

    I'm sorry, I can't help you with the question about opening the ktr XML files. Hopefully someone else can look at that.

    The Generate Rows step does what it says. It creates rows of data based on the static values you configure in it. It is frequently used in testing and development when you don't have real data handy to work with.
    In this case, it is being used to "drive" the JS step. The JS step expects rows of input to come into it that it will operate on. In this case though, there is no normal input but rather you are executing the Java application which is creating the data. So the Generate Rows step is used to create one row of data to cause the JS step to begin doing its work.

    hth,
    Daniel

  20. #20
    Join Date
    Sep 2008
    Posts
    28

    Default

    Ok cool. That worked!!

    Thanks, your explanation was of great help.

  21. #21
    Join Date
    Sep 2008
    Posts
    28

    Default

    I tried my previous example with a public static final method with void arguments. Now I want to try passing some arguments to it, that I obtain from my previous step in the transformation (table input). I have extracted the value to a local variable in the javascript. This same java script invokes the java api.

    How do I pass this variable as an argument to the method in my java api.

    I got an error saying:
    Java constructor for "AgeCalculator" with arguments "org.pentaho.di.compatibility.Value,org.pentaho.di.compatibility.Value" not found. (script#3)

    I tried passing it to a constructor as well as a regular public static final method.

    Please suggest.
    Thanks
    AK

  22. #22
    DEinspanjer Guest

    Default

    That sounds like you have the compatibility layer enabled. You could try turning that off, or you could use the .getXXX() methods on the object for the field to get the native object.

  23. #23
    Join Date
    Sep 2008
    Posts
    28

    Default

    Hey

    Wow - Unchecking the compatibility mode worked!! Could you please explain what is the compatibility mode check box used for? I am not sure I know anything about it.

    Thanks,
    AK

  24. #24
    DEinspanjer Guest

    Default

    Foremost, it provides compatibility to allow scripts developed on the versions of Kettle prior to 3.0 to continue to work seamlessly.

    Version 3.0 integrated the previously separate "Mod JS Values" step as the only JS step. While they both use the same JS engine (Mozilla's Rhino), the new version works with native Java objects (String, and primitives) instead of using the Kettle "Value" interface. It makes the JS simpler and in many cases quicker, but the big penalty is that you can't update values using the setValue() method.

    So, if you are writing a new script and you don't need to update an existing field, but rather create new fields, you don't need or want the compatibility layer enabled.

  25. #25
    Join Date
    Sep 2008
    Posts
    28

    Default

    Thank you so much for your prompt and detailed response. I am learning quite some things here
    -AK

  26. #26
    Join Date
    Nov 1999
    Posts
    9,729

    Default

    but the big penalty is that you can't update values using the setValue() method.
    That method was flawed anyway since it allowed for mixing of data types. Many people had problems with that in the 2.x days.

  27. #27
    Join Date
    Sep 2007
    Posts
    10

    Default Still not working !

    I dloaded your FeedEdit.zip, placed the jar file in MacOS's Applications/Kettle.app/Contents/Resources/Java/libext. Does not work and issues the ff. error:

    2008/12/02 18:39:39 - Modified Java Script Value.0 - No starting Script found!
    2008/12/02 18:39:39 - Modified Java Script Value.0 - ERROR (version 3.1.0, build 826 from 2008/09/30 05:29:00) : Unexpected error :
    2008/12/02 18:39:39 - Modified Java Script Value.0 - ERROR (version 3.1.0, build 826 from 2008/09/30 05:29:00) : org.pentaho.di.core.exception.KettleValueException:
    2008/12/02 18:39:39 - Modified Java Script Value.0 - ERROR (version 3.1.0, build 826 from 2008/09/30 05:29:00) : Javascript error:
    2008/12/02 18:39:39 - Modified Java Script Value.0 - ERROR (version 3.1.0, build 826 from 2008/09/30 05:29:00) : TypeError: Cannot call property display in object [JavaPackage FeedsEdit]. It is not a function, it is "object". (script#5)
    2008/12/02 18:39:39 - Modified Java Script Value.0 - ERROR (version 3.1.0, build 826 from 2008/09/30 05:29:00) :
    2008/12/02 18:39:39 - Modified Java Script Value.0 - ERROR (version 3.1.0, build 826 from 2008/09/30 05:29:00) : at org.pentaho.di.trans.steps.scriptvalues_mod.ScriptValuesMod.addValues(ScriptValuesMod.java:409)
    2008/12/02 18:39:39 - Modified Java Script Value.0 - ERROR (version 3.1.0, build 826 from 2008/09/30 05:29:00) : at org.pentaho.di.trans.steps.scriptvalues_mod.ScriptValuesMod.processRow(ScriptValuesMod.java:640)
    2008/12/02 18:39:39 - Modified Java Script Value.0 - ERROR (version 3.1.0, build 826 from 2008/09/30 05:29:00) : at org.pentaho.di.trans.step.BaseStep.runStepThread(BaseStep.java:2664)
    2008/12/02 18:39:39 - Modified Java Script Value.0 - ERROR (version 3.1.0, build 826 from 2008/09/30 05:29:00) : at org.pentaho.di.trans.steps.scriptvalues_mod.ScriptValuesMod.run(ScriptValuesMod.java:703)
    2008/12/02 18:39:39 - Modified Java Script Value.0 - ERROR (version 3.1.0, build 826 from 2008/09/30 05:29:00) : Caused by: org.mozilla.javascript.EcmaError: TypeError: Cannot call property display in object [JavaPackage FeedsEdit]. It is not a function, it is "object". (script#5)
    2008/12/02 18:39:39 - Modified Java Script Value.0 - ERROR (version 3.1.0, build 826 from 2008/09/30 05:29:00) : at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3557)
    2008/12/02 18:39:39 - Modified Java Script Value.0 - ERROR (version 3.1.0, build 826 from 2008/09/30 05:29:00) : at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3535)
    2008/12/02 18:39:39 - Modified Java Script Value.0 - ERROR (version 3.1.0, build 826 from 2008/09/30 05:29:00) : at org.mozilla.javascript.ScriptRuntime.typeError(ScriptRuntime.java:3563)
    2008/12/02 18:39:39 - Modified Java Script Value.0 - ERROR (version 3.1.0, build 826 from 2008/09/30 05:29:00) : at org.mozilla.javascript.ScriptRuntime.typeError3(ScriptRuntime.java:3589)
    2008/12/02 18:39:39 - Modified Java Script Value.0 - ERROR (version 3.1.0, build 826 from 2008/09/30 05:29:00) : at org.mozilla.javascript.ScriptRuntime.notFunctionError(ScriptRuntime.java:3649)
    2008/12/02 18:39:39 - Modified Java Script Value.0 - ERROR (version 3.1.0, build 826 from 2008/09/30 05:29:00) : at org.mozilla.javascript.ScriptRuntime.getPropFunctionAndThis(ScriptRuntime.java:2186)
    2008/12/02 18:39:39 - Modified Java Script Value.0 - ERROR (version 3.1.0, build 826 from 2008/09/30 05:29:00) : at org.mozilla.javascript.optimizer.OptRuntime.callProp0(OptRuntime.java:117)
    2008/12/02 18:39:39 - Modified Java Script Value.0 - ERROR (version 3.1.0, build 826 from 2008/09/30 05:29:00) : at org.mozilla.javascript.gen.c7._c0(script:5)
    2008/12/02 18:39:39 - Modified Java Script Value.0 - ERROR (version 3.1.0, build 826 from 2008/09/30 05:29:00) : at org.mozilla.javascript.gen.c7.call(script)
    2008/12/02 18:39:39 - Modified Java Script Value.0 - ERROR (version 3.1.0, build 826 from 2008/09/30 05:29:00) : at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:401)
    2008/12/02 18:39:39 - Modified Java Script Value.0 - ERROR (version 3.1.0, build 826 from 2008/09/30 05:29:00) : at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3003)
    2008/12/02 18:39:39 - Modified Java Script Value.0 - ERROR (version 3.1.0, build 826 from 2008/09/30 05:29:00) : at org.mozilla.javascript.gen.c7.call(script)
    2008/12/02 18:39:39 - Modified Java Script Value.0 - ERROR (version 3.1.0, build 826 from 2008/09/30 05:29:00) : at org.mozilla.javascript.gen.c7.exec(script)
    2008/12/02 18:39:39 - Modified Java Script Value.0 - ERROR (version 3.1.0, build 826 from 2008/09/30 05:29:00) : at org.pentaho.di.trans.steps.scriptvalues_mod.ScriptValuesMod.addValues(ScriptValuesMod.java:332)
    2008/12/02 18:39:39 - Modified Java Script Value.0 - ERROR (version 3.1.0, build 826 from 2008/09/30 05:29:00) : ... 3 more
    2008/12/02 18:39:39 - Modified Java Script Value.0 - Finished processing (I=0, O=0, R=1, W=0, U=0, E=1)
    2008/12/02 18:39:39 - Spoon - The transformation has finished!!

  28. #28
    Join Date
    Nov 1999
    Posts
    9,729

    Default

    Of-course, it being Apple doing something different you need to re-package the application for that to work.
    Either that or simply use the zip file and use spoon.sh like everyone else :-)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Privacy Policy | Legal Notices | Safe Harbor Privacy Policy

Copyright © 2005 - 2019 Hitachi Vantara Corporation. All Rights Reserved.