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");

Similar Messages

  • 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

  • Executing .jar files from another .jar file.

    How would I run one .jar file from another .jar file. and is there anyway to call specific class arguments? Because I have one .jar file that reads a specified file and returns its contents.
    So how would I execute it and specify its arguments and how would I make it return something to the executing jar file?

    Because I have one .jar file that reads
    a specified file and returns its contents. Presumably you have a class that does that, and you have that class stored in a jar. And you want to know how to... um... do something with that class. I say "um..." because normally you don't execute a class, either, you either call its static methods or you create an instance of the class and call its instance methods.
    If you have been writing a whole lot of little classes each of which just has a static main method, then stop doing that. Write real Java classes instead. The tutorial is here:
    http://java.sun.com/docs/books/tutorial/java/index.html

  • 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()

  • Please help with executing a java program from another java program

    Hi, I have tried to execute a very simple java program which is printed out "Hello world" from another java gui program when the user click on the "START" button. However, I don't receive any print out on the terminal when they click it. These two programs are being developed in Unix environment. Here is the code that I am trying to use.
    Runtime r = Runtime.getRuntime();
    r.exec("java HelloWorld");
    p.s. HelloWorld is java class of the HelloWorld.java

    I think you have to luanch your MS Prompt first before executing the command.

  • How to call a function in one .js file from another .js file

    Hello Techies,
    I am trying to call a function in two.js file from one.js file.
    Here is my code
    one.js
    <script>
    document.write("<script type='text/javascript' src='/htmls/js/two.js'> <\/script>");
         function one()
                        var a;
                       two(a);
              }two.js
                  function two(a)
                          alert("two");
                      }But the function two() is not working.
    How can I do this one??
    regards,
    Krish

    I think there is a syntax error in line
    document.write("<script type='text/javascript' src='/htmls/js/two.js'> <\/script>");
    end tag <\/script> is wrong.

  • 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();
    }

  • Setting JTree in one class file from another class file

    Hello,
    I'm new to java. I recently created a project in netbeans and here is one of the java files. I used the IDE to make a split pane, with a tree structure and panel in it.
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    * AmplifierDesignGUI.java
    * Created on Jun 20, 2010, 1:18:52 PM
    package AmplifierDesign;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    * @author Bugz
    public class AmplifierDesignGUI extends javax.swing.JFrame {
    /** Creates new form AmplifierDesignGUI */
    public AmplifierDesignGUI() {
    initComponents();
    /** This method is called from within the constructor to
    * initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is
    * always regenerated by the Form Editor.
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {
    jSplitPane1 = new javax.swing.JSplitPane();
    jScrollPane1 = new javax.swing.JScrollPane();
    jTree1 = new javax.swing.JTree();
    jPanel1 = new javax.swing.JPanel();
    jMenuBar1 = new javax.swing.JMenuBar();
    jMenu1 = new javax.swing.JMenu();
    jMenu2 = new javax.swing.JMenu();
    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    jScrollPane1.setViewportView(jTree1);
    jSplitPane1.setLeftComponent(jScrollPane1);
    org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
    jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
    .add(0, 475, Short.MAX_VALUE)
    jPanel1Layout.setVerticalGroup(
    jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
    .add(0, 274, Short.MAX_VALUE)
    jSplitPane1.setRightComponent(jPanel1);
    jMenu1.setText("File");
    jMenuBar1.add(jMenu1);
    jMenu2.setText("Edit");
    jMenuBar1.add(jMenu2);
    setJMenuBar(jMenuBar1);
    org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
    layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
    .add(layout.createSequentialGroup()
    .add(8, 8, 8)
    .add(jSplitPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 571, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
    .addContainerGap(32, Short.MAX_VALUE))
    layout.setVerticalGroup(
    layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
    .add(layout.createSequentialGroup()
    .add(8, 8, 8)
    .add(jSplitPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 278, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
    .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    pack();
    }// </editor-fold>
    * @param args the command line arguments
    public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    new AmplifierDesignGUI().setVisible(true);
    try {
    new JTreeStructure().setVisible(true);
    } catch (Exception ex) {
    Logger.getLogger(AmplifierDesignGUI.class.getName()).log(Level.SEVERE, null, ex);
    // Variables declaration - do not modify
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JTree jTree1;
    // End of variables declaration
    So once this was done I wanted to link the JTree to a mysql database. So I found a sample .java file on the net:
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    package AmplifierDesign;
    import java.awt.*;
    import java.sql.*;
    import java.util.*;
    import javax.swing.*;
    import javax.swing.tree.*;
    public class JTreeStructure extends JFrame {
    Connection con = null;
    Statement st = null;
    ResultSet rs = null;
    //public static void main(String args[]) throws Exception {
    // new JTreeStructure();
    public JTreeStructure() throws Exception {
    super("Retrieving data from database ");
    String driver = "com.mysql.jdbc.Driver";
    String url = "jdbc:mysql://localhost:8889/";
    String db = "icons";
    ArrayList list = new ArrayList();
    list.add("Laser Objects");
    Class.forName(driver);
    con = DriverManager.getConnection(url + db, "root", "root");
    try {
    String sql = "Select * from fiberComponents";
    st = con.createStatement();
    rs = st.executeQuery(sql);
    while (rs.next()) {
    Object value[] = {"Fiber Components",rs.getString(2) };
    list.add(value);
    } catch (Exception e) {
    System.out.println(e);
    rs.close();
    st.close();
    con.close();
    Object hierarchy[] = list.toArray();
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container content = getContentPane();
    DefaultMutableTreeNode root = processHierarchy(hierarchy);
    JTree tree = new JTree(root);
    content.add(new JScrollPane(tree), BorderLayout.CENTER);
    setSize(275, 300);
    setLocation(300, 100);
    setVisible(true);
    private DefaultMutableTreeNode processHierarchy(Object[] hierarchy) {
    DefaultMutableTreeNode node = new DefaultMutableTreeNode(hierarchy[0]);
    DefaultMutableTreeNode child;
    for (int i = 1; i < hierarchy.length; i++) {
    Object nodeSpecifier = hierarchy;
    if (nodeSpecifier instanceof Object[]) // Ie node with children
    child = processHierarchy((Object[]) nodeSpecifier);
    } else {
    child = new DefaultMutableTreeNode(nodeSpecifier); // Ie Leaf
    node.add(child);
    return (node);
    The problem is when I run my program two windows open up. The original one with JTree1, and the panel and horizontal splitplane and another window with a new tree component that did get its objects from the database. My question is how do I "replace" the JTree1 with the new tree created from the second java file?
    Or additionally, maybe I could set the data for JTree1 from within the second java file?

    zmoddynamics wrote:
    ....Please excuse my post as I am not sure what is meant by code tags?To use code tags, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.
    Another way to do this is to manually place the tags into your code by placing the tag [cod&#101;] above your pasted code and the tag [cod&#101;] below your pasted code like so:
    [cod&#101;]
      // your code goes here
      // notice how the top and bottom tags are different
    [/cod&#101;]Luck.

  • Opening a hyperlinked password protected file from another Adobe File

    I have a user that has a table of contents pdf file. This file links to other files in the same directory. Originally in XP she would click on the link and a password box would open. She would type in the password and was able to read the file. She has now been replaced with a Windows 7 machine and this is not the case. She is able to open the individual files (without actually knowing the contents) in the directory. The password box will pop up when this occurs. When she tries to access the PDF file from the toc.pdf file she cannot.
    Is there some sort of security permission that is not allowing a pdf file to open a hyperlinked password protected PDF file in Windows 7.
    She is and has been using Adobe Reader X on both machines.
    Thank you.

    I think this may have something to do with your dns settings.  From a shell type ipconfig /flushdns and try again.

  • Launching a java application from another java application

    Hi,
    I'm facing an interesting challenge :-)
    Inside a Java application, I want to start another java application.
    Possible solutions in my list :
    1. Using jakarta commons launcher.
    2. Runtime.exec (make the code platform dependent).
    3. Starting the main method of the other app by create a new Thread inside the current app
    4. I know people did this by calling an ant target from the java application but I don't have
    any sample code, I appreciate any sample code for it.
    Thanks,
    Ali Salehi

    That is an iteresting challenge :)Don't be mean; he's polite, done some investigation and taken the trouble to be clear. The question, presumably, is "has anyone got a different solution or a preference as to which of these is the best?"
    In which case, my advice would be to say that invoking main in a separate thread is fundamentally different from Runtime.exec() because the former would run in the same JVM. Is this what you want? If one app has the other on its classpath then do they need to be separate apps? Could they be modules of a single app?

  • Open a PDF file from another PDF file on iPad

    Hi,
    I made a test with 2 PDF files. In the first PDF file I link it to the another PDF file.
    I used Hyperlinks in InDesign but it doesn't work.

    I seriously doubt that that's possible.
    It's difficult enough to accomplish that on a Mac or PC because the file hierarchy must be maintained or the link will be broken. The iOS operating system seems to have only a rudimentary file system.
    I looked through the Help files for the best PDF reader I know, PDF Expert, and saw no reference to supporting that feature.

  • Killing a Java process from another Java process

    Hi
    Is there a possible way of sending a SIGINT, SIGKILL, or any other signal from a Java process running in one JVM, to a java process running in a different JVM on the same machine.
    I've the signal handlers written in my process and they do respond to singals (e.g. on pressing Ctrl-C) on the console, but i want to write a separate program that sends this signal to the first process.
    Any ideas?
    Thanks in advance and regards
    Kashif

    The answer, as always, is that Java can't do operating-system-specific things like that, but you can use JNI to do it. However, if your code created the Java process you want to kill, via Process p = Runtime.getRuntime().exec(...), then you can use p.destroy() to kill it.

  • Help needed to load a flash file from another flash file

    This is a two part question - but I am sure that answering
    one will solve the other.
    I have a menu.swf file, and if I click on the top button it
    should load module1.swf. (Which it does) - however I can see the
    menu.swf file underneath the module1.swf file.
    The second part of the problem is that when I click on the
    top button of menu.swf, I want it to goto a specific location
    within module1.swf ( say the 8th page )
    If I use loadmovienum ("module1.swf",0) it opens a new window
    - instead of replacing menu.swf
    any ideas

    Use loadMovie() or loadClip (with the MovieClipLoader class)
    depending on how much you want to know about/control the load. With
    these you can specify a target movieClip for the load.
    Not sure what you mean by 'location'. If you're talking
    frames, you can use the load's target movieclip as a reference to
    the swf and use gotoAndPlay - but you have to make sure the swf is
    loaded first.

  • Calling Java Application from another

    How can i call a Java Application from another java App.
    eg., If my Java application is called MyApp and i would like call another java application from within it.
    One way could be by using "System". I would like to know if there is any other method and is portable.
    Thanks in advance.

    hi,
    it works and not!
    if you start an other class with a command like this the 2nd prog/class terminates too if you terminate the caller-class!
    dear
    oliver scorp

  • Not able to execute one Java program from another one via. exec()

    Hi,
    I am new to this forum, so I might be asking a very trivial quetion.
    My program Ap1 is :
    class Ap1
    public static void main(String a[]) throws Exception
    Runtime rt=Runtime.getRuntime();
    Process p=null;
    p=rt.exec("javac,Pr.java");
    Runtime.getRuntime().exec("javac,Pr.java");
    When I run the above program, I get below error at the line 7(bold one):
    D:\batchwe>javac Ap1.java
    D:\batchwe>java Ap1
    Exception in thread "main" java.io.IOException: CreateProcess: javac,Pr.java err
    or=2
    at java.lang.ProcessImpl.create(Native Method)
    at java.lang.ProcessImpl.<init>(ProcessImpl.java:81)
    at java.lang.ProcessImpl.start(ProcessImpl.java:30)
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:451)
    at java.lang.Runtime.exec(Runtime.java:591)
    at java.lang.Runtime.exec(Runtime.java:429)
    at java.lang.Runtime.exec(Runtime.java:326)
    at Ap1.main(Ap1.java:7)
    Path is set as below(which seems correct):
    D:\batchwe>path
    PATH=C:\Program Files\Java\jdk1.5.0_05\bin
    I am just wondering how to call one Java program from another one?!
    Please help.
    thanks
    Rashmi

    Hi diptaPB,
    Thanks for your suggestion.
    Here is how I tried it again, but not getting the desired output:
    Class Pr looks like below -->
    class Pr
    public static void main(String a[])
    System.out.println("hi from process");
    When I run 'Pr' from command prompt, i get the desired output:
    D:\batchwe>java Pr
    hi from process
    However, when I call 'Pr' from another program Ap2.java, it seems that it does not call 'Pr':
    class Ap2
    public static void main(String a[]) throws Exception
    Runtime rt=Runtime.getRuntime();
    Process p=null;
    String[] cmd=new String[2];
    cmd[0]="javac";
    cmd[1]="Pr.java";
    p=rt.exec("cmd");
    D:\batchwe>javac Ap2.java
    D:\batchwe>java Ap2
    ************* no output appears here***********************
    Looking forward for your help.
    regards
    Rashmi

Maybe you are looking for

  • Why - Separate Partnerlinks for same child process

    Early in our development, we noticed that we had to have separate partnerlinks for the same service if two (or more) parallel paths were concurrently invoking the service receive. Our guess was the delivery of the child Async send required separate p

  • Gmail app tab lost when I log out of gmail

    I have gmail as an app tab. Since installing ff 7.1 when I log out of gmail the app tab icon is lost and rather than being presented with the logon screen for gmail I'm presented the Gmail approach to email screen which gives me the option to sign in

  • SUM( ) versus Aggregation SUM

    When creating a new logical column for a fact table, I can define it with an expression like SUM (column A - columnB) with aggregation rule None - column A and B being pointed to physical columns. On the other hand I can use an expression (column A -

  • My iPad is suddenly charging very slowly. What would cause this?

    My iPad is suddenly charging very slowly. What would cause this?

  • Accessing children of element created with PopUpManager

    My application has 2 files. First one is main application, second one is custom component (Popup) used to create popup windows. I would like to know how can i access ViewStack element (has id assigned to "vs") in popup from main application. NB! Lot