• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can some one help me out with this code..

 
Srudeep Kumar Reddy Katamreddy
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
am trying to load .class Files form JAR file... am able to load the files sucessfully. But when i call a fuction in the .class file in the JAR file am getting an error... am not able to resolve it.

import java.io.*;
import java.lang.ClassLoader;
import java.lang.reflect.*;
import java.util.jar.*;

public class TestLoader {

/* provide full path of .jar file as a command line argument */


public static void main( String args[] ) throws Exception
{
String progClass = args[0];
TestLoader ccl = new TestLoader();
String coms ="";
java.util.Vector classes = ccl.load(progClass);
System.out.println("finally ve got classes of size :"+classes.size());
int nulls = 0;
int i = 0;
for(;i<classes.size();i++)
{
Object ob ;
ob = classes.get(i);
if(ob==null) nulls++;
System.out.println(" "+i+" "+ob);
String actName =ob.toString().substring(6);
if (actName.equals("GETenv"))
{
Class abc = GETenv.newInstance();
coms = abc.getenv("COMS");
System.out.println("COMS =..."+coms);
}
}
i = i-nulls;
// coms =actName.getenv("COMS");
// System.out.println("COMS =..."+coms);
System.out.println("no of classes loaded..."+i);
System.out.println("no of classes not loaded..."+nulls);
}
public java.util.Vector load(String jarfilename)
{
try
{
final JarFile ajar = new JarFile(jarfilename);
jarfile = ajar;
java.util.Enumeration enum234 = ajar.entries();
java.util.Vector classes = new java.util.Vector();
for(;enum234.hasMoreElements()
{
JarEntry loadedentry = (JarEntry) enum234.nextElement();
if(isClassFile(loadedentry.getName()))
{
classes.add(loadClass(loadedentry));
}
}
return classes;
}
catch(Exception e)
{
e.printStackTrace();
return null;
}
}
private Class loadClass(final JarEntry jarentry)
{
try
{
Class loadedclass = new ClassLoader()
{
public Class findClass(String name)
{
try
{
InputStream is = jarfile.getInputStream(jarentry);
int available = is.available();
byte data[] = new byte[available];
is.read(data);
name = parseClassName(name);
try
{
return defineClass(name, data, 0, data.length);
}
catch(ClassFormatError err)
{
System.out.println(err+"class not loaded :"+name );
return null;
}
}
catch(IOException ex)
{
return null;
}
}
}.loadClass(jarentry.getName());
return loadedclass;
}
catch(Exception e)
{
e.printStackTrace();
return null;
}
}
private boolean isClassFile(String jarentryname)
{
return jarentryname.endsWith(".class");
}
private String parseClassName(String jarentryname)
{
int index = jarentryname.indexOf("class");
String classname = jarentryname.substring(0,index-1);
return classname;
}
private JarFile jarfile;
}

in this: we have to give the name of the JAR file as command line argument. here GETenv (GETenv.class) is the class name inside the JAR file. and getenv(string) is the function name inside the class GETenv.class.... the error am getting is

TestLoader.java:29: cannot find symbol
symbol : variable GETenv
location: class TestLoader
Class abc = GETenv.newInstance();
^
TestLoader.java:30: cannot find symbol
symbol : method getenv(java.lang.String)
location: class java.lang.Class
coms = abc.getenv("COMS");
^
Please Help me out in resolving this issue
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The class GETenv doesn't seem to be on the classpath during compilation. That's causing the first error.

abc is of type java.lang.Class, which does not have a getenv method. That's causing the second error. If case class GETenv has a getenv method, and you meant to call its method, you need to construct an instance of GETenv first:

(if GETenv has a newInstance method that returns a GETenv object)

Also, in the future UseCodeTags when posting code of any length. It is unnecessarily hard to read it as it is.
[ March 25, 2008: Message edited by: Ulf Dittmer ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic