Invoke jar main method in java code

hi
i have a jar that has a usage like this
java -jar helper.jar arg1 arg2 etc...i want to invoke this helper main method in my java code.i've tried using reflection but it didn't quite work...
String[] args = new String[]{"arg1","arg2"};
Class c = Class.forName("com.helper.MainClass");
Method mainMethod = c.getMethod("main", args.getClass());
int mods = mainMethod.getModifiers();
if(mainMethod.getReturnType() != void.class || !Modifier.isStatic(mods) || !Modifier.isPublic(mods)) {
     throw new NoSuchMethodException("main");
mainMethod.invoke(null, args);
Exception in thread "main" java.lang.IllegalArgumentException: argument type mismatch
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at Test.callJarMain(Test.java:50)
at Test2.main(Test.java:34)thoughts???

phychem wrote:
i want to use this jar in a non-static way.
public void useJar(String args[]){
//somehow invoke jar method here...
Sorry, I still don't get it. I don't know what you mean by a non-static way of using jars. The main method for launching an application is static so if you call that method, you will be using a static method. I don't know what you mean by a "jar method" either. Methods are inside classes. So far, all I can guess is you want to invoke a method in a class that happens to be inside a jar. Since people do this all the time without reflection, I don't see the problem.

Similar Messages

  • Can we call DLL method in jave code

    hi,
    can we call DLL file method in java Code or program.

    See
    http://forum.java.sun.com/thread.jsp?forum=52&thread=179765

  • Invoking the main() method of a class within another

    Greetings -
    Can someone tell me what happens when you invoke the main() method of a class from within another class?
    I have a third-party class, designed to be invoked from a command line, that I want to invoke from within a separate class. What appears to be happening is that following the successful execution of the third-party class, the invoking class is terminated as well. That's not the intended behavior I had envisioned. ^_^
    Is there a better way than invoking the main() method? I still need the invoking class to continue executing.
    Thanks!

    Navigate yourself around pitfalls related to the Runtime.exec() method

  • How can i rename a jar file using only java code

    i have tried everything i can think of to accomplish this but nothing works.

    ghostbust555 wrote:
    In case you geniuses haven't realized I said I tried everything I can think of not that I tried everything. So help or shut up I realize that I didn't try everything but if you can't figure it out either DO NOT POST.
    And the question is how can i rename a jar file using java code? As it says in the title. Read.I would tell you to use the File.renameTo method, but surely that would have been obvious, and you would have tried it already? But maybe you didn't. You were kind of lacking in details in what you tried.
    And yes, I am a genius. Just don't confuse "genius" with "mind-reader".

  • Class Not found Exception for invoking BPEL process through the Java code

    Hi.
    The JDeveloper IDE raise the Exception From the invoking the BPEL process through the java code .Class Not Found Exception (Locator,ID.......).What is process of importing these classes from API.

    In your code (.bpel file) import the library using the bpelx:exec tag. For example the adding the following entry in your .bpel file imports the com.oracle.bpel.client.util library.
    <bpelx:exec import="com.oracle.bpel.client.util.*"/>

  • Invoking Acrobat PDF compare from Java code

    Need to invoke the Adobe Acrobat XI's PDF compare feature using Java code.
    Description:
    ========
    We are exploring the possibilities of passing 2 PDF's from Java to Adobe Acrobat XI SDK to get the compared results in new PDF.
    If anyone has explored with this feature... please share your thoughts.

    You would need to write a custom plugin to Acrobat that calls the compare APIs (as they are only accessible from C/C++).  Your plugin can then expose whatever from of IAC/IPC that it wishes to communicate with your Java code.
    Of course, this is all DESKTOP code since Acrobat can't be used on a server.

  • How do I invoke another s/w from java code(urgent)

    Hi all,
    My code looks like this :
    Runtime rt =Runtime.getRuntime();
    String command = "~/tts/bin/mbrola" +" ~/tts/mbrola/us2 " + " /home/dir1/dir2/final/temp.pho "+
    " - | ~/tts/bin/mbrplay -";
    rt.exec(command );
    Whenever I ru n this code thru java , I get an error that this command was not found but am able to run it in my unix terminal. Would anyone have any clue as to why this is happening.
    Thanks
    Sohini

    Hi all,
    My code looks like this :
    Runtime rt =Runtime.getRuntime();
    String command = "~/tts/bin/mbrola" +"
    la" +" ~/tts/mbrola/us2 " + "
    /home/dir1/dir2/final/temp.pho "+
    " - | ~/tts/bin/mbrplay -";
    rt.exec(command );
    Whenever I ru n this code thru java , I get an error
    that this command was not found but am able to run it
    in my unix terminal. Would anyone have any clue as to
    why this is happening.
    Thanks
    SohiniHi,
    I guess that you're trying to run command WITH arguments, aren't you?
    So, you have to do it this way(just verify arguments):
        Runtime rt =Runtime.getRuntime();
        String command[] = {"~/tts/bin/mbrola", "la", "~/tts/mbrola/us2",
                                              "/home/dir1/dir2/final/temp.pho",  "-", " | ~/tts/bin/mbrplay", "-"}
        rt.exec(command);(making PROGRAM and ARGUMENTS an array - EACH argument SEPARATELY(I don't know their exact number in your script ))
    Hope to help,
    SHW

  • How to Invoke a main class in a jar file from my project

    Hi
    I am a new user of the Sun Java Studio enterprise. I would like to know how i can invoke a main method in a jar file associated to the project.
    regards
    Nisanth

    Could you please clarify: do you want to run your project or a project from an independent jar?
    In the first case just select Run Project from the project context menu or select a class with main method and click Run File in the class context menu.
    Regarding the second case:
    - I don't think there is such a feature in the IDE (running third party jars is not an IDE function). Could you explain why you need this?

  • Invoking main method from cmd line (deployed as web app)

    I have a application deployed as a web application (war file).
    I want to create a regular java class with main method so that main can invoked
    on a command line.
    The main method wishes to share resources (DB connection pool etc) with the rest
    of the web application.
    Is it possible ? and how to invoke the main method inside the war(not jar) ?

    Hi,
    if you deployed this to OC4J, open EM
    http://server:port/em
    and get to the Web service. You can execute it from here to see if generally this is working, or if you have a proxy problem only
    Frank

  • OpenScript/How to add .jar file to Java Code in relative path

    Hi all,
    I want to add a .jar file which can be executed separately (like "java -jar A.jar") to my recorded Java Code.I've read this wiki http://everest2.us.oracle.com/wiki/Generic_JAR_Project about how to add a .jar file to "Assets", however, I cannot figure out how to use the .jarr file in Java code,I mean , how to get this .jar file like the method the databank added in "Assets"?
    Things I did are as followed:
    1.Execute my .jar file in OpenScript Java code with absolute path like this:
    String cmd = "C:\Users\A.jar";
    Runtime.getRuntime().exec(cmd);
    This does work, but must set a absolute path in Java code like "C:\User\A.jar" ,which is not the workaround I want (I need my scripts can be run on other machines).
    2.Try to get its current path with following codes:
    File directory = new File(".");
    String currentPath=directory.getCanonicalPath();
    However,though this can get its absolute path (which is the the project path) in Eclipse like "C:\Users\Workspace\testProject", this only gets "C:\OracleATS\openScript" in OpenScript.
    I thought to copy my .jar file to the project path , got its current path in java code first,then can know the path of .jar file, but this workaround failed because of the above reason.
    I notice that in the "Assets" there are "Databanks","Object Libraries","JAR Files","Scripts". Since the databanks and scripts that added to "Databanks" and "Scripts" can be got or run in Java Code like:
    *getDatabank("DatabankName").getNextDatabankRecord(); String data = eval("{{db.DatabankName.data}}");*
    *or getScript("ScriptName").run();*
    *Is there a method to get and run the jar file added to "Assets\JAR Files" like the above?*
    Thank you very much!
    Regards,
    Angyoung

    Hi DM,
    Thanks for your reply!
    I've found a workaround,which is calling OpenScript's APIs ,such as this.getScriptPackage().getRepository() and this.getScriptPackage().getWorkspace(),etc to locate the .jar file.
    And this workaround can still work even though the script is run on other machine.
    Sorry to reply you so late!
    Regards,
    Angyoung

  • Running .jar files from Java code

    Hi.
    I am designing a sort of platform to run some games which are saved in .jar files. Basically this platform is a menu from which the user can select the game which he/she wants to run. It also has the feature of adding new games.
    Is is possible to run the .jar file from within java code?
    Thanks for any help!
    Krt_malta

    Just list the jar in the classpath list and call the class in the jar.

  • Invoking beautiful widgets from Java Code

    Hi experts,
    I have the code given below which I obtained from www.gigya.com website. If I add the code given below in an html, I can view the widget inside the html as shown in the attached image in [http://docs.google.com/Doc?id=dfzrknk_31gxvz9nd8|http://docs.google.com/Doc?id=dfzrknk_31gxvz9nd8]
    I would like to know, how I can invoke this widget from a Java code.
    Please help.
    Any help in this regard will be well appreciated with points.
    //Copy the code below to the head section of your page:
    <script src="http://cdn.gigya.com/wildfire/js/wfapiv2.js"></script>
    //Copy the code below and place next to content item:
    <div id="divWildfirePost"></div>
    <script>
    var pconf={
      defaultContent: 'TEXTAREA_ID',
      UIConfig: '<config><display showDesktop="false" showEmail="true" useTransitions="true" showBookmark="true" codeBoxHeight="auto"></display><body><background frame-color="#BFBFBF" background-color="#FFFFFF" gradient-color-begin="#ffffff" gradient-color-end="#F4F4F4" corner-roundness="4;4;4;4"></background><controls color="#202020" corner-roundness="4;4;4;4" gradient-color-begin="#EAEAEA" gradient-color-end="#F4F4F4" bold="false"><snbuttons type="textUnder" frame-color="#D5D5D5" background-color="#fafafa" over-frame-color="#60BFFF" over-background-color="#ebebeb" color="#808080" gradient-color-begin="#FFFFFF" gradient-color-end="d4d6d7" size="10" bold="false" down-frame-color="#60BFFF" down-gradient-color-begin="#6DDADA" over-gradient-color-end="#6DDADA" down-gradient-color-end="#F4F4F4" over-color="#52A4DA" down-color="#52A4DA" over-bold="false"><more frame-color="#A4DBFF" over-frame-color="#A4DBFF" gradient-color-begin="#F4F4F4" gradient-color-end="#BBE4FF" over-gradient-color-begin="#A4DBFF" over-gradient-color-end="#F4F4F4"></more><previous frame-color="#BBE4FF" over-frame-color="#A4DBFF" gradient-color-begin="#FFFFFF" gradient-color-end="#A4DBFF" over-gradient-color-begin="#A4DBFF" over-gradient-color-end="#F4F4F4"></previous></snbuttons><textboxes frame-color="#CACACA" color="#757575" gradient-color-begin="#ffffff" bold="false"><codeboxes color="#757575" frame-color="#DFDFDF" background-color="#FFFFFF" gradient-color-begin="#ffffff" gradient-color-end="#FFFFFF" size="10"></codeboxes><inputs frame-color="#CACACA" color="#757575" gradient-color-begin="#F4F4F4" gradient-color-end="#ffffff"></inputs><dropdowns list-item-over-color="#52A4DA" frame-color="#CACACA"></dropdowns></textboxes><buttons frame-color="#8DD1FF" gradient-color-end="#BBE4FF" color="#202020" bold="false" over-gradient-color-begin="#BBE4FF" down-gradient-color-begin="#BBE4FF" over-gradient-color-end="#FFFFFF" down-gradient-color-end="#ffffff"><post-buttons frame-color="#8DD1FF" gradient-color-end="#BBE4FF"></post-buttons></buttons><listboxes frame-color="#CACACA" corner-roundness="4;4;4;4" gradient-color-begin="#F4F4F4" gradient-color-end="#FFFFFF"></listboxes><checkboxes checkmark-color="#00B600" frame-color="#D5D5D5" corner-roundness="3;3;3;3" gradient-color-begin="#F4F4F4" gradient-color-end="#FFFFFF"></checkboxes><servicemarker gradient-color-begin="#ffffff" gradient-color-end="#D5D5D5"></servicemarker><tooltips color="#6D5128" gradient-color-begin="#FFFFFF" gradient-color-end="#FFE4BB" size="10" frame-color="#FFDBA4"></tooltips></controls><texts color="#202020"><headers color="#202020"></headers><messages color="#202020"></messages><links color="#52A4DA" underline="false" over-color="#353535" down-color="#353535" down-bold="false"></links></texts></body></config>'
    Wildfire.initPost('457492', 'divWildfirePost', 400, 300, pconf);
    </script>Warm Regards,
    Anees

    AneesAhamed wrote:
    Not even in a frame? java.awt.Frame or HTML frame? The former: no. The latter: probably, but it's not a Java question.

  • Args within main() method

    This might be a complex question. I hope that someone can give me a simple answer. What I want to know is what this code means?
    public static void main(String[] args){
         if(args > 2.length){
         // ect...I understand that the main() method creates a String array.
    How does the array get filled?
    What does it get filled with?
    Main is the starting point for the program and so where does this String array come from?
    How can an array be created before the program even starts?

    SquareBox wrote:
    This might be a complex question. I hope that someone can give me a simple answer. What I want to know is what this code means?
    public static void main(String[] args){
    if(args > 2.length){
    // ect...
    Next time, copy/paste, rather than re-typing. That won't even compile. Presumably you meant
    if (args.length > 2)
    I understand that the main() method creates a String array. No.
    It receives a reference to a String array as a parameter. Just like any other method receives its parameters. The array is created before the method is called. There's nothing special about main() as far as the Java language is concerned. It's only the JVM that attaches special meaning to it.
    How does the array get filled? The JVM fills it from the args it receives from the OS/shell on the command line before invoking your main() method.
    When you execute java ABC 123 MyClass the java executable is invoked, and the OS passes "ABC", "123", and "MyClass" to it from the shell. (Actually, I think it may also pass "java", but, whatever). The JVM then loads MyClass, creates a String array containing "ABC" and "123", and call's MyClass's main() method, passing a reference to that String[].
    What does it get filled with? See above.
    Main is the starting point for the program and so where does this String array come from?See above.
    How can an array be created before the program even starts?main() is the starting point for your application which is run inside the JVM. It's not the starting point for the JVM, the shell, or the OS, all of which work together to create that array and deliver a reference to it to your main() method.

  • Can "main" method return values

    Hi,
    I have a requirement of returning values to the
    command prompt from the main method. Is this possible
    to do in the first place. Is it valid.
    i.e. Can you do the following in Java,
    =======================================
    import java.io.*;
    import java.lang.Object;
    import java.util.*;
    public class P
         public static String main(String args[])
         String bye = "BYE";
         return(bye);
    =========================================
    Can "main" method in Java return values at command
    prompt.
    Can you have something like,
    public static String main(String args[])
    instead of
    public static void main(String args[])
    Help urgently needed as I need to pipe data from a
    Java program to a C++ program. One method is to
    writing to a flat file but this would cause overheads
    reading and writing to a file, hence this way.
    Any help would be greatly appreciated.
    Thanks in advance.
    Thank,s

    Hi jchak,
    Yes I am trying to pass a Java String to a C++
    application. Any insights, any help.
    OK the whole thing goes as follows,
    =========================================
    Theres a C++ application which first starts up.
    This app is then supposed to invoke a Java class file
    which does some processing. Finally the value of one
    variable in Java (actually a huge String) is to be made
    available back to C++. This could be done thru flat file
    wherein the Java outputs the string to the flat file
    and on return the C++ program can be made to read the
    same flat file to retrieve that string but this would
    surely cause some overheads (time factor) reading and
    writing files. So I wish to devise some way by means
    of which I can return the string value back to the C++
    program. Piping of data from one app to another is what I
    want to do.
    Is there some way by means of which Java can return
    back or communicate or pipe back the string back to
    C++. How can it be done without using external flat files
    or databases.
    Thansk,
    JAtin

  • Public main method in a non-public class

    Hi,
    It is said that main method in a java class must be public so that it can be invoked by JVM. This works even if the class itself has default modifier.
    But while programming, any method (be it public) of a class with default access modifier from one package can't be accessed by classes from other packages. For example,
    1. Consider class A in package a.
    package a;
    class A {
    public void methA() {}
    2. Now consider class B in pakage b.
    package b;
    class B {
    void methB() {
    // Here it can't access class A and its method methA()
    So how does JVM invoke the main method of a class with default access modifier. Assume that the class is in some package a.b.c
    Regards,
    Amit

    Hi,
    Maybe this can also be beneficial for you.
    Standalone applications don't necessarily have init and start methods. They must have a public static void main routine to start things off. The main routine typically creates some object of the master class. It can't use any non-static variables or methods in the class with the implied this, since there is no this object. It typically quickly returns. Then the event loop processing starts. Even the static main method must live inside some public class, or at least in the top class, the first class and the class for whom the source file is named. There are no such thing as standalone static methods in Java. The signature of the main class must look exactly like this. Cut and paste it to be sure:

Maybe you are looking for