Srudeep Kumar Reddy Katamreddy

Greenhorn
+ Follow
since Mar 25, 2008
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Srudeep Kumar Reddy Katamreddy

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
16 years ago
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
16 years ago
Hi all,

This is srudeep. Currently am facing some problems.

1. my project needs a JAR file which contains .class files to be loaded dynamically at runtime.
2. Using the class name which i know, i should search the JAR file loaded for the existance of that class.
3. If that class exists then i should create an instance of that class.
4. After i create instance i have to call the fuction which is already in the class(provided i know the fuction's name earlier).

But as a beginner am not able to do this.... can any one help me in performing the required task???
16 years ago
Hi all,

This is srudeep. Currently am facing a couple of problem.

1. my project needs a JAR file which contains .class files to be loaded dynamically at runtime.
2. Using the class name which i know, i should search the JAR file loaded for the existance of that class.
3. If that class exists then i should create an instance of that class.
4. After i create instance i have to call the fuction which is already in the class(provided i know the fuction's name earlier).

But as a beginner am not able to do this.... can any one help me in performing the required task???
16 years ago