-5

I have a situation where I need to compile classes dynamically.

Following is the structure in which my java classes have been organized.

// no package 
public class A{
}

package test ;

public class B{
      A obj;
     //other java code
}

where A is a class with default package, and B is a class with package name test. B has A as a attribute.

Files are saved in their respective folders( as per package )

I fail to compile this with JAVA Compiler API.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Pavan
  • 9
  • 3
  • 2
    It is almost impossible to help you unless you post your code. – Hunter McMillen May 30 '12 at 15:47
  • *"Any help !!"* (Do you have) Any question? BTW - please leave out noise like sigs. – Andrew Thompson May 30 '12 at 15:49
  • This might help (and the articles it links to): http://stackoverflow.com/questions/2193226/how-to-import-a-class-from-default-package – Martin Wilson May 30 '12 at 15:50
  • 1
    Also, figure out how to use the code formatting, and check your post in the preview before hitting send. That is twice now I've had to edit that for readability. ..and you just broke it again, earning a -1 for the irritation value. – Andrew Thompson May 30 '12 at 15:50
  • 1
    What is `bla bla` supposed to represent? – Hunter McMillen May 30 '12 at 15:50
  • bla bla bal is where i have other variables and java code – Pavan May 30 '12 at 15:56
  • Martin, I am trying to compile using the Java Compiler API – Pavan May 30 '12 at 16:03
  • I was about to edit your question (its is poor as is), but realised there is no point as you certainly need more information. What exactly have you tried? Can you please tell us where these classes are referring to their locations by the path and then indicate the exact `javac` command you are trying? – thejartender Jun 05 '12 at 10:31

3 Answers3

2

You cannot refer to classes in the default package from classes in other packages. This has been the case since 2001. See the Release Notes for Java 1.4.

user207421
  • 305,947
  • 44
  • 307
  • 483
0

You should be able to set the classpath variable as an option.

List<String> optionList = new ArrayList<String>();
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
String path =....
optionList.addAll(Arrays.asList("-classpath",path ));
JavaCompiler.CompilationTask task = compiler.getTask(null, null, null,optionList,null,fileObjects);
Satheesh Cheveri
  • 3,621
  • 3
  • 27
  • 46
  • The problem with this code is, if you have dependency as in, class B uses A as an attribute in it, then i fail to compile it. If i would put the class A in a package ( not necessary as of B) i am able to compile it properly. i am stuck on with the situvation where A is in a default package and B is in a defined package. – Pavan May 30 '12 at 16:08
-1

You can find some opened bugs in oracle site related to this scenario

http://bugs.sun.com/bugdatabase/view_bug.do;jsessionid=9f121e41b450bfffffffffcc19db46db9bbc2?bug_id=4361575

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4114502

Satheesh Cheveri
  • 3,621
  • 3
  • 27
  • 46
  • The first one does not relate to this situation: it relates to a situation where all classes are in the default package and one has an inner class. The second one relates to `rmic`, not `javac`, and it is marked as 'closed: will not fix'. Not an answer. – user207421 May 31 '12 at 05:55