Loading...


bookmark - Running .java Or .class Files From Runtime

Running .java Or .class Files From Runtime

 
 Discussion by kvarnerexpress with 1 Replies.
 Last Update: January 29, 2006, 6:19 pm
 
bookmark - Running .java Or .class Files From Runtime  
Quickly Post to Running .java Or .class Files From Runtime  w/o signup Share Info about Running .java Or .class Files From Runtime  using Facebook, Twitter etc. email your friend about Running .java Or .class Files From Runtime Print
Reply / Comment New Discussion / Topic Share / Bookmark E-Mail a Friend Print

from an API and outputs the result to the API (like someAPI.submitResult(myResult), which checks the results against the sample values. The idea is that the student is able to click a browse button and locate their .java or .class file, which the GUI then launches.

So my question is if I can launch this class as part of the GUI process so that the API actually is actually part of the GUI process. It's a bit backwards really - like launching the API first and then adding in the component that uses the API during runtime. But there is an advantage of doing it this way instead of having the GUI get the question and then the students class being launched on its own with an API separate from the GUI process. If they were two separate processes I'd have to make the student identify what question is being answered, their student ID and password, etc with inputs to the API in their actual code, then they would have to send the submit function call at the end. And the API process would have to include the checking functions and input the data to the database, then the user would have to manually ask GUI to retrieve the results. If I can make it all part of one process the data sharing makes things much more straight forward for the student.

Thanks for your help.

   Thu Jan 12, 2006    Reply         

If i understood correctly, you want to launch a UI (swing probably), allow user to select a class, then call an api of the selected class.

It is possible. I am assuming the class selected is in classpath.

You need to use java reflection.

Here is an example I copied from sun reference docs

CODE


import java.lang.reflect.*;

class SampleInvoke {

  public static void main(String[] args) {
     String firstWord = "Hello ";
     String secondWord = "everybody.";
     String bothWords = append(firstWord, secondWord);
     System.out.println(bothWords);
  }

  public static String append(String firstWord, String secondWord) {
     String result = null;
     Class c = String.class;
     Class[] parameterTypes = new Class[] {String.class};
     Method concatMethod;
     Object[] arguments = new Object[] {secondWord};
     try {
       concatMethod = c.getMethod("concat", parameterTypes);
       result = (String) concatMethod.invoke(firstWord, arguments);
     } catch (NoSuchMethodException e) {
         System.out.println(e);
     } catch (IllegalAccessException e) {
         System.out.println(e);
     } catch (InvocationTargetException e) {
         System.out.println(e);
     }
     return result;
  }
}


Refer http://java.sun.com/docs/books/tutorial/reflect/
for detailed docs.

   Sun Jan 29, 2006    Reply         

Quickly Post to Running .java Or .class Files From Runtime  w/o signup Share Info about Running .java Or .class Files From Runtime  using Facebook, Twitter etc. email your friend about Running .java Or .class Files From Runtime Print
Reply / Comment New Discussion / Topic Share / Bookmark E-Mail a Friend Print

Similar Topics:

Which Is Good Java Or Dot Net

Hi, I want to know which is going to be a success in future. I've asked this question to many but all are giving my vague answers. I'm asking this question because i'm going to join a company where i can choose either dot net or java. Which one should i choose. Wax ...more

   08-Sep-2008    Reply         

Java Run Without Command Line Or Jd...

So I have learned a few basic concepts mostly dealing with algorithms and primatives, although we have now covered some useful things like polymorphism and inheritance...anyways, so I can do a lot with the code, and I was wanting to make a simple game and put it on another machine, but I have always ...more

   04-Dec-2008    Reply         

Comparing The Java And .net 39 fl...

Hi! I'm not sure if I should be posting this in the .NET forum or the Java forum, but since I was inspired to write this post after having used the Java flavor of Hibernate, which I used after trying out the .NET port of Hibernate (called NHibernate), I'll make this post her ...more

   07-Sep-2010    Reply         

Connecting Jsp To Mysql    Connecting Jsp To Mysql (6) (8) One Click Copy And Paste To Clipboard in simple Javascript  One Click Copy And Paste To Clipboard in simple Javascript