|
|
Running .java Or .class Files From Runtime | ||
Discussion by kvarnerexpress with 1 Replies.
Last Update: January 29, 2006, 6:19 pm | |||
![]() |
|
|
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.
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.
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://java.sun.com/docs/books/tutorial/reflect/
for detailed docs.
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.
Similar Topics:
Which Is Good Java Or Dot Net
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
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
Connecting Jsp To Mysql (6)
|
(8) One Click Copy And Paste To Clipboard in simple Javascript
|
Loading...
HOME 





Compile and run java on Mac via Terminal
[Java-Beginner] Episode 1 : Getting Started
How to fix java problems for running private server clients COMMENTARY

