Jump to content
xisto Community
Sign in to follow this  
kvarnerexpress

Running .java Or .class Files From Runtime

Recommended Posts

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.

Share this post


Link to post
Share on other sites

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

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://docs.oracle.com/javase/tutorial/reflect/
for detailed docs.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.