2

I want to know that is it possible to get the classes, functions/methods available in a java package from C#. Suppose I have a java package p which contains SampleClass. I want to inspect this package from C# so that I can get SampleClass. I do not want to use this class in C# in fact I just want to get the name of the available classes and functions.

Hope this is possible!

Thanks

Vinod Maurya
  • 4,167
  • 11
  • 50
  • 81

2 Answers2

4

Assuming that you have a JDK installed, you can use 'jar -tf jarName.jar' to get a list of classes, which you can then parse.

To get more information about a particular class, you can use the command-line 'javap' utility to print out a list of all methods and fields. This is in a fairly easily parsable format. For example:

javap -classpath . com.prosc.io.ZipCreator
Compiled from "ZipCreator.java"
public class com.prosc.io.ZipCreator extends java.lang.Object{
    public com.prosc.io.ZipCreator();
    public java.lang.Integer getLevel();
    public void setLevel(java.lang.Integer);
    public void createZip(java.io.File, java.io.File)       throws java.io.IOException;
    public void createZip(java.io.File, java.util.zip.ZipOutputStream)       throws java.io.IOException;
    public void setMaximumZipSize(long);
    static {};
}
Jesse Barnum
  • 6,507
  • 6
  • 40
  • 69
  • When I use javap, it is giving me an error - Error could not find layoutlib. My jar file is placed in d:\javaproject\ and i am using javap in the same directory like: D:\javaproject>javap layoutlib.jar – Vinod Maurya Jan 28 '11 at 14:47
  • You don't pass the name of a jar file to javap, you pass in the name of a class contained inside the jar. Use 'jar -tf jarName.jar' to get the list of classes. Let's say you see a class inside there called /my/cool/Button.class. Use 'javap -classpath jarName.jar my.cool.Button' to see the list of functions. – Jesse Barnum Jan 28 '11 at 14:55
1

If you are asking about Jar files. It is just a zip file.

You can map folders and files to class names like that: "/java/lang/Object.class" --> "java.lang.Object".

On this post you can read about listing the contents of a .zip folder in c#.

The advantage of this method is that you don't need to worry about JDK installed but if you are planning anything complex e.g. list of methods I would go for solution proposed by @Jesse Barnum.

Community
  • 1
  • 1
bartosz.lipinski
  • 2,627
  • 2
  • 21
  • 34