• Post Reply Bookmark Topic Watch Topic
  • New Topic

get allclasses from java file  RSS feed

 
iyappan sankaran
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how to get all classes in the java file

public class Sample
{
}
class a
{
}
class b
{
}

when i run:javac Sample i should get Sample,a,b;how???
 
Sandeep Jindal
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi iyappan,

javac tool just compiles the java file that you pass as parameter. Along with that i complies all the related java files that i needs.


So if u are writing:
java Sample.java

you will get A.class and B.class only if they are getting used by sample.

Hope this clears.

Regards
Sandeep Jindal
 
iyappan sankaran
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi sandeep,
i want to get all the classes referenced in the Public class through code
ie using reflection is it possible
 
iyappan sankaran
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for e.g)
public class Sample
{
public static void main(String args[])
{
Exam a=new Exam();
System.out.println(a.add());

}
}
class Exam
{
public int add()
{
return 10;
}
}

this is the program i never going to compile.


import java.lang.reflect.*;
public class ReflectionTest
{
public static void main(String args[])
{
try{
Class aclass=Class.forName("Sample");
Class classes[]=aclass.getDeclaredClasses();
System.out.println(classes.length);
for(int i=0;i<classes.length;i++)
System.out.println(classes[i].getName());
}catch(ClassNotFoundException e){System.out.println("class");}
}
}

if i run i could get all the classes that are refernced in Sample should
be displayed.but this doesn't work throws exception.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When posting code, please be sure to surround the code with the [code] and [/code] UBB Tags. This will help to preserve the formatting of the code, thus making it easier to read and understand.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!