• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

dont know where the problem is... after loading... please help me out....

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 43028
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please do not post the same question to multiple forums: CarefullyChooseOneForum

Let's continue the discussion in this duplicate thread.
 
This guy is skipping without a rope. At least, that's what this tiny ad said:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic