I have some coded that analyzes data, and I've realized that I'd like to have a bit of code that's tailored to the very specific source of the data, rather than generic processing. Any individual source may change with time, and there are already several different sources to consider. To handle this case, I was launching, from Java, complete stand-alone programs, and that's nice, but there are some problems with that and I decided I'd like to use dynamic class loading instead. I've been using Java since v 1.1 and am generally familiar with most Java issues but this specific task has me wondering if I'm using a wrong version of Java or some such because the error I'm getting looks like the method calls I'm trying aren't known to my version. I was thinking maybe I've left out a vital import statement?
I found several good examples to follow, the best of which appears to be this one because it's doing EXACTLY what I'd like to do: http://ift.tt/1chBEfa
The very first example of code there is EXACTLY the kind of thing I want to do, so, I lifted some bits from it - not very different from what I was doing on my own (which I was struggling with), but it wouldn't compile - various "cannot find symbol" errors. As my code is a lot more bulky, I tried theirs all on its own and it won't compile either, same as mine! Here is that code:
public class DynamicLoader
{
public static void main(String[] args) throws Exception
{
Class toRun = Class.forName(args[0]);
Method mainMethod = findMain(toRun);
mainMethod.invoke(null, new Object[] { args });
}
private static Method findMain(Class clazz) throws Exception
{
Method[] methods = clazz.getMethods();
for (int i=0; i<methods.length; i++)
{
if (methods[i].getName().equals("main"))
return methods[i];
}
return null;
}
}
When I try and compile it, I get:
# javac DynamicLoader.java
DynamicLoader.java:11: error: cannot find symbol
private static Method findMain(Class clazz) throws Exception
^
symbol: class Method
location: class DynamicLoader
DynamicLoader.java:8: error: cannot find symbol
Method mainMethod = findMain(toRun);
^
symbol: class Method
location: class DynamicLoader
DynamicLoader.java:13: error: cannot find symbol
Method[] methods = clazz.getMethods();
^
symbol: class Method
location: class DynamicLoader
3 errors
My java version: javac 1.7.0_45
I imagine the problem is very simple / basic, but looking at the documentation yielded no clues!
Aucun commentaire:
Enregistrer un commentaire