How to compile and run a .java file from another java program

hello,
can any one tell me how to compile and run a *.java* file from another java program which is not in same directory?

Well a smarter way of implementing this is by using a solution provided by Java Itself.
If you are using J2SE 6.0+ there is an in built solution provided along with JDK itself and inorder to go ahead with solution the below are set of API which you;d be using it for compiling Java Programs (Files)
http://java.sun.com/javase/6/docs/api/javax/tools/package-summary.html
How do i do that ??
Check out the below articles which would help you of how to do that
http://www.ibm.com/developerworks/java/library/j-jcomp/index.html
http://www.javabeat.net/javabeat/java6/articles/java_6_0_compiler_api_1.php
http://books.google.com/books?id=WVbpv8SQpkEC&pg=PA155&lpg=PA155&dq=%22javax+tools%22+compiling+java+file&source=web&ots=XOt0siYe-f&sig=HH27ovuwvJgklIf8omTykUmy-eM
Now once we are done with compilation.In order to run a Specific class all you ought to do is create an object and its specific methods of a specified class included in the CLASSPATH which you can manage it easily by usage little bit reflections.
Hope that might help :)
REGARDS,
RaHuL

Similar Messages

  • Trying to compile a .java file from another .java file

    Hello,
    I'm trying to compile a .java file from another .java file using Runtime.exec...
    String c[]=new String[3];
    c[0]="cmd.exe"; c[1]="/c" ; c[2]="javac Hello.java";
    Process p=Runtime.exec(c);
    If anyone can help me in atleast getting the command prompt when Runtime.exec("cmd.exe") is executed...that would be great...I tried out notepad.exe, calc, explorer instead of cmd.exe...all the commands display their respective windows..except cmd.exe...the command prompt doesnt appear...
    Please help me ASAP....
    Thanks for your help in advance...
    Regards.
    AKhila.

    try this. ur code will be compliled and will get .class file. but console won't appear. is it a must for u?
    public class Exec{
         public static void main(String a[]) throws Exception{
              String c[]=new String[3];
              c[0]="cmd.exe"; c[1]="/c" ; c[2]="javac Hello.java";
              Process p=Runtime.getRuntime().exec(c);
              // or Runtime.getRuntime().exec("javac Hello.java");

  • How to call inner class method in one java file from another java file?

    hello guyz, i m tryin to access an inner class method defined in one class from another class... i m posting the code too wit error. plz help me out.
    // test1.java
    public class test1
         public test1()
              test t = new test();
         public class test
              test()
              public int geti()
                   int i=10;
                   return i;
    // test2.java
    class test2
         public static void main(String[] args)
              test1 t1 = new test1();
              System.out.println(t1.t.i);
    i m getting error as
    test2.java:7: cannot resolve symbol
    symbol : variable t
    location: class test1
              System.out.println(t1.t.geti());
    ^

    There are various ways to define and use nested classes. Here is a common pattern. The inner class is private but implements an interface visible to the client. The enclosing class provides a factory method to create instances of the inner class.
    interface I {
        void method();
    class Outer {
        private String name;
        public Outer(String name) {
            this.name = name;
        public I createInner() {
            return new Inner();
        private class Inner implements I {
            public void method() {
                System.out.format("Enclosing object's name is %s%n", name);
    public class Demo {
        public static void main(String[] args) {
            Outer outer = new Outer("Otto");
            I junior = outer.createInner();
            junior.method();
    }

  • How to compile and run java files on a mac using command line?

    can someone tell me or link me to some article on how to compile and run java files from command line on a mac? I have mac OS X leopard

    What do you mean by "where to put them" ? What do you want to put anywhere ?
    Have you read Peter's comment in brackets ? Perhaps you have a classpath problem ?
    Edited by: Michael_Knight on Aug 31, 2008 4:23 AM

  • How to compile and run package programs in Java

    Hi,
    I want to know how to compile and run the package programs in Java using -d. instead of creating the package folder manually.
    eg:
    package Test;
    class test1
    public void disp()
    //Any code;
    I want to compile this without creating the folder ' Test ' manually. that is if we use -d with javac the Test folder will be created automatically. I need the format of -d
    could anyone please help me.
    Thanks in Advance ,
    Ambika

    My program Test.java in F:\Tomcat5\webapps\Ambika\WEB-INF\Classes. I compiled in the format below. I got like this. What should I do for this? But yesterday I compiled like this only, It compiled and the folder com\cert\Test.class is created. Today again I compiled the pgm after deleting the already created folder 'com\cert', I got the error like this.
    I've given my pgm and the thing I've got when I compiled it.
    Test.java
    package com.cert;
    public class Test
         public void display()
              System.out.println("Hai");
    F:\Tomcat5\webapps\Ambika\WEB-INF\Classes>javac -d F:\Tomcat5\webapps\Ambika\WEB-INF\Classes\Test.java
    javac: no source files
    Usage: javac <options> <source files>
    where possible options include:
    -g Generate all debugging info
    -g:none Generate no debugging info
    -g:{lines,vars,source} Generate only some debugging info
    -nowarn Generate no warnings
    -verbose Output messages about what the compiler is doing
    -deprecation Output source locations where deprecated APIs are u
    sed
    -classpath <path> Specify where to find user class files
    -cp <path> Specify where to find user class files
    -sourcepath <path> Specify where to find input source files
    -bootclasspath <path> Override location of bootstrap class files
    -extdirs <dirs> Override location of installed extensions
    -endorseddirs <dirs> Override location of endorsed standards path
    -d <directory> Specify where to place generated class files
    -encoding <encoding> Specify character encoding used by source files
    -source <release> Provide source compatibility with specified release
    -target <release> Generate class files for specific VM version
    -version Version information
    -help Print a synopsis of standard options
    -X Print a synopsis of nonstandard options
    -J<flag> Pass <flag> directly to the runtime system
    F:\Tomcat5\webapps\Ambika\WEB-INF\Classes>
    Plz help me.
    thanks in advance
    Ambika

  • How to compile and run java program at command console

    hi there
    can anyone tell me how to compile and run a java program at command console? I have installed JRE 1.3.1, and also have installed JBuilder 5 if it helps.

    try this
    System.out.println("Enter your Name : ");
    BufferedReader console = new BufferedReader( new InputStreamReader( System.in ) );
    String s = console.readLine();
    System.out.println("Hello : "+ s+" !" );

  • How to  compile and run javacard program

    how to compile and run javacard program "HelloWorld"

    1. Where is save Servlet program?
    2. How to set classpath?
    3. how compile and run?
    http://www.coreservlets.com

  • How to call a Java class from another java class ??

    Hi ..... can somebody plz tell me
    How to call a Java Class from another Java Class assuming both in the same Package??
    I want to call the entire Java Class  (not any specific method only........I want all the functionalities of that class)
    Please provide me some slotuions!!
    Waiting for some fast replies!!
    Regards
    Smita Mohanty

    Hi Smita,
    you just need to create an object of that class,thats it. Then you will be able to execute each and every method.
    e.g.
    you have developed A.java and B.java, both are in same package.
    in implementaion of B.java
    class B
                A obj = new A();
                 //to access A's methods
                 A.method();
                // to access A's variable
                //either
               A.variable= value.
               //or
               A.setvariable() or A.getvariable()

  • How to compile and run a java program?

    I am getting this error message, what to do?
    Exception in thread "main" java.lang.NoClassDefFoundError:
    Thanks for you help.
    Ajay

    Hello,
    I believe that this error is caused because when you
    compile and run a java program, you have to use the
    same name that is next to public class. For example,
    the program name is next to the word public class.
    For example, if a program began like this:
    public class Concat
    then the name of the program in this case is Concat
    and when you compile it you type: javac Concat.java
    when you run it type: java Concat
    depending on the name of your program.and depending on whether the class is in a package. And depending on what directory you are currently in.
    Also depending on if you set your class path you might
    have to type the above like this: jdk1.2.1\bin\javac
    Concat.java to compile, and to run you would have to
    type jdk1.2.1\bin\java Concat where you would replace
    Concat with the name of your program and replace
    jdk1.2.1 with the name of your version of java, that
    is again if you do not have your classpath set. Hope
    this helps.Your examples have nothing to do with whether the CLASSPATH is set--only with whether the PATH is set. And, it may not be jdkXXX\bin. The path to the JDK (in this case, to javac and java executables) could be anything. Classpath should be set on the command line. The OP probably did NOT set a classpath on the command line (preferred), or in the environment variables.
    Also, make sure you did not forget to put:
    public static void main(String[ ] args)
    on the line underneath the line where it says "public
    class Concat" where Concat is the name of your
    program. Again, I hope this helps.Irrelevant (with the info we have from the OP so far). The error says that the JVM can't even find the class--not that the class doesn't have a main. The "main" referred to in the error message is within the JVM itself.
    OP: Did you fix your problem? If not, what directory are your files in? Are they in a package? What command are you typing to get that error? That is, please tell us your entire command line.

  • How to compile and run windows built .fmb file in linux

    Hi,
    I have developer suite 10g installed in windows OS.
    i built a .fmb forms binary file in windows. now i want to compile and deploy this .fmb file into .fmx file in linux and run it.
    I have Oracle Application Server Enterprise version along with Forms/Reports services installed on my linux box.
    Can someone guide how to use my AS Form services in linux to complie and run my .fmb file.
    Thanks in advance,
    Philip.

    is there a easier way or is this the only way to
    compile a .fmb file in a linux machine.Yes, there also a way interactive(not in batch), but you have to need use X-session (GUI) variables DISPLAY
    You can compile module after module if you change $i with you form(FMB) userid
    frmcmp_batch.sh userid=scott/tiger@bs817 batch=no module=$i module_type=form
    compile_all=yes window_state=minimize
    Remember you must set FORMS PATH and ORACLE_HOME/bin where frmcmp_batch is
    Then, run file where you insert frmcmp_batch.sh
    How do i invoke forms builder, forms compiler in a
    linux machine where i have installed Application
    Server Enterprise edition?You don't invoke FORM BUILDER
    (assumption is that the "development" machine is a MS Windows platform and the "deployment" machine is a Unix platform)
    FMX, MMX, and PLX files are NOT portable between platforms. Source code (FMB, MMB, PLL) must be recompiled when moving from one platform to another. This includes moving from one Unix platform to another (i.e. Sun Solaris to Linux) or one Windows platform version to another.
    frmcmp_batch.sh is script that run executable (compiler) for Linux OS
    In Forms 10.1.2.0.2 and newer, the executable and script names are. frmcmp , frmcmp_batch..
    Once all of the executables have been generated the path to these files will need to be included in the Forms deployment environment. In most cases this will be the FORMS90_PATH value in the Forms env file, "default.env"
    I hope to have been clear
    Regards

  • How to Compile and run Smart Card Application

    hi
    any one can help me reagrding Javacard API. i download the JavaCard Development Kit from sun and install on my machine but i didn't get all java files and class files of JavaCard API. where i obtain those files so my application get compile and run.
    pls help me i m new in this technology.
    Thanks in Advance.

    My program Test.java in F:\Tomcat5\webapps\Ambika\WEB-INF\Classes. I compiled in the format below. I got like this. What should I do for this? But yesterday I compiled like this only, It compiled and the folder com\cert\Test.class is created. Today again I compiled the pgm after deleting the already created folder 'com\cert', I got the error like this.
    I've given my pgm and the thing I've got when I compiled it.
    Test.java
    package com.cert;
    public class Test
         public void display()
              System.out.println("Hai");
    F:\Tomcat5\webapps\Ambika\WEB-INF\Classes>javac -d F:\Tomcat5\webapps\Ambika\WEB-INF\Classes\Test.java
    javac: no source files
    Usage: javac <options> <source files>
    where possible options include:
    -g Generate all debugging info
    -g:none Generate no debugging info
    -g:{lines,vars,source} Generate only some debugging info
    -nowarn Generate no warnings
    -verbose Output messages about what the compiler is doing
    -deprecation Output source locations where deprecated APIs are u
    sed
    -classpath <path> Specify where to find user class files
    -cp <path> Specify where to find user class files
    -sourcepath <path> Specify where to find input source files
    -bootclasspath <path> Override location of bootstrap class files
    -extdirs <dirs> Override location of installed extensions
    -endorseddirs <dirs> Override location of endorsed standards path
    -d <directory> Specify where to place generated class files
    -encoding <encoding> Specify character encoding used by source files
    -source <release> Provide source compatibility with specified release
    -target <release> Generate class files for specific VM version
    -version Version information
    -help Print a synopsis of standard options
    -X Print a synopsis of nonstandard options
    -J<flag> Pass <flag> directly to the runtime system
    F:\Tomcat5\webapps\Ambika\WEB-INF\Classes>
    Plz help me.
    thanks in advance
    Ambika

  • How to compile and run servlets in Eclipse?

    Does somebody known how can I compile and run servlets in Eclipse IDE? I've added and configured Tomcat's plugin. I've created a project with my example servlets source, and I don't know what farther. How do I have compile it without main method? I must add web.xml file from Tomcat's directory into projects in Eclipse and modify it?
    THX Chudzik

    Try googling for "eclipse servlet", surely someone has written instructions.

  • How to Compile and run a GUI project made in Netbeans from shell

    hi all .....
    I develpoed a java project using Netbeans .The project has GUI also (JFrame and various layout ) . The project is building and running fine through netbeans .But when I tried to compile the source files (*.java files) from shell , I failed .
    The project has two files MainFrame.java (JFrame file and this file has the public static void main method ) and helloworld.java (simple java class file). But the "src" folder created by netbeans (at project location) contains one more file named "MainFrame.form" . Moreover netbeans was using two jar files (AbsoluteLayout.jar and Swing-layout-1.0.jar .
    I tried to compile the two .java files . helloworld.java compiled successfully but the MainFrame.java didnt. I used the command :
    linux:~/netbeans projects/Network tool/src/mytool # javac -classpath ".;AbsoluteLayout.jar;Swing-layout-1.0.jar;" MainFrame.java
    The 14 errors were encountered. MOst of them were like :
    MainFrame.java:138: package org.jdesktop.layout does not exist
    org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
    ^
    Help, suggestion or guidance of any type is welcomed .(Keep in mind that project is running fine when done using netbeansIDE).
    waiting for reply....

    Hello,
    1. The .form file was used by Netbean's GUI builder to create the .java and is not needed for compilation.
    2..../src # javac -classpath "..." mytool/*.java
    I guess the package mytool was not coped with.
    Alternatively it is worth to use the ant build tool also used by Netbeans.
    Then you can make a jar from the class files, indicate in the manifest.mf file:
    1. the main class
    2. the library jars

  • How to compile and run servlets

    Can anybody tell me what is the exact path to put the servlet class file in
    tomcat 4

    hai..
    when u installed
    tomcatx/webapps/root/WEB-INF
    only can c
    no classes folder .
    so u have to created in small case
    classes dir under root/WEB-INF
    now u create ur servlet java file and compile and copy that class file into classes dir..
    then u have to go for servlet mapping in web.xml
    then u run
    i hope it will help u

  • How to compile and run a matlab program?

    Hi,
    I have a college matlab code that i want to compile and run and take an output of. Can anyone guide me with this in xcode?
    Neerav

    Hi,
    I already googled it and went through that website before posting here and as you can see the software is paid. And thats exactly why I asked in my original post as to whether there is a way to do it with xcode.
    And the idea that xcode might be of any help too came after some experimentation. I double clicked the .m file and it opened in xcode. So I guess there must be a way to compile and run it too.
    Thanks for you recommendation anyway.
    Neerav Kothari

Maybe you are looking for

  • Is my airport broken? My wireless has stopped all of a sudden! No solution works!

    hi, I need help please. After working fine for years my wireless connection on my desktop which is on snow leoperd 10.6.8 has stopped connecting all of a sudden. It works fine on my macbook pro and my iphone. When I turn it on I get none of your pref

  • GX70-3BE - How to output 4k resolution

    Hello there, Just got the GX70 3BE notebook with the intention of running 4k resolution on a 4k capable monitor. When I plug in the HDMI into a 4k capable hdmi port, the highest resolution available is still only 1080p. How do I actually output 4k wi

  • Process external functions within xsl maps

    Hi, i am working with xslt maps, and i have now the problem to generate the current date/time. I found some postings with the solution to implement some new java classes. Does somebody know, how to use the registerd java libs within xslt maps ? - pub

  • Exit from sqlplus / plsql script

    Hello, I'm executing the following script (named test_script.sql) SET serveroutput ON DEFINE temp=&1 DECLARE BEGIN dbms_output.put_line('Value: &temp'); END; using the command line sqlplus <username>/<password> @/tmp/test_script.sql "Hello" I want th

  • Need Attribute Text instead of public name in Siebel

    Hi Experts, Is it possible in OPA to send across attribute text instead of public name / attributeName in the "SaveSession" inbound interface to Siebel application? Thanks, Vinay