dcsimg
java language
4 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   suraj_chug
Posted On:   Friday, May 18, 2001 11:03 PM

1.Can i write and compile class definition at run time in java ?



2. Is conditional compliation is possibale in java?

Re: java language

Posted By:   John_Mitchell  
Posted On:   Friday, June 22, 2001 06:01 PM

RTF: Both of those questions are already answered in the jGuru JavaLanguage FAQ. Please remember to actually Read The FAQs (and search them :-) before posting questions.

Re: java language

Posted By:   Luigi_Viggiano  
Posted On:   Sunday, June 17, 2001 07:02 AM

To disable methods compilation I use some comments like this:


//*
public static void main(String[] args) {
// this method is active
}
//*/


to disable it just remove a slash in the first line as following, and will not be compiled.


/*
public static void main(String[] args) {
// this method is inactive
}
//*/

You can use to disable compilation of debugging code.

Re: java language

Posted By:   Finlay_McWalter  
Posted On:   Sunday, June 17, 2001 12:59 AM

To compile code, you have two options:

  1. you can Runtime.exec to javac, which runs the regular java compiler in another process.
  2. as Chris said, you can run the compiler classes yourself, direct from tools.jar


Here is a tiny example of a program that takes the latter approach:



public class caller {
public static final void main(String [] args){
String [] caller_args = new String [1];
caller_args[0] = "test.java";
sun.tools.javac.Main.main(caller_args);
}
}


To compile the above program, you need tools.jar in javac's classpath:

c:jdk1.4injavac -classpath c:jdk1.4lib ools.jar caller.java


and to run the program, you also need tools.jar in the classpath:

c:jdk1.4injava -cp c:jdk1.4lib ools.jar;. caller

Re: java language

Posted By:   Christopher_Schultz  
Posted On:   Monday, May 21, 2001 12:21 PM


  1. I've never done it, but I'd imagine that you can compile Java source at runtime if you have the tools.jar in your classpath. The only problem is that Sun doesn't let you redistribute that JAR file with your application, so you have to get your clients to download the whole JDK.

  2. Conditional compilation (in the C sense) is not possible in Java. However, if you have code which looks like this:





    public class myClass
    {
    private static final boolean DEBUG = false;

    public void myMethod()
    {
    // Do some stuff

    if(DEBUG)
    {
    // Do some more stuff
    }
    }
    }



    then the code labeled "do some more stuff" won't be compiled since the compiler knows that DEBUG will always be false.




-chris
About | Sitemap | Contact