# Solved: JAVA: How to open CMD via a JAVA program?



## sepala (May 20, 2010)

Hi,

I am trying to compile a java file by another java file. In this case I tried to open the cmd by java codes, but it didn't work. I know this is possible through the processbuilder class, but I am very much new to this class. The following is an example of what I tried to do.


```
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.io.*;

public class CompileCheck extends JFrame implements ActionListener
{
    JButton but;
    
    
    public CompileCheck()
    {
        but = new JButton("Create");
        but.addActionListener(this);
        
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new FlowLayout());
        buttonPanel.add(but);
        
        getContentPane().add(buttonPanel);
        
                
    }
    
    public void actionPerformed(ActionEvent ae)
    {
        try
        {
       ProcessBuilder p = new ProcessBuilder("cmd.exe");
       p.start();
        }
        catch(Exception e)
        {
            JOptionPane.showMessageDialog(null,e);
        }
    }
    
    
    
     
    
    public static void main(String[]args)
    {
        CompileCheck c = new CompileCheck();
        c.setVisible(true);
        c.setSize(200,200);
        c.validate();
    }
}
```
Pleas help me..


----------



## sepala (May 20, 2010)

Any support here??


----------



## sepala (May 20, 2010)

Found the way,

Process p = Runtime.getRuntime().exec("CMD.exe /c start");


----------



## sepala (May 20, 2010)

OK, now another thing, how o execute the following commands after opening the CMD?

1. cd\ 
2. cd java\appletTest 
3. javac appletTest.java


----------



## andythepandy (Jul 2, 2006)

You don't need to execute cmd or cd because you can execute javac from the process like this:

```
Process p = Runtime.getRuntime().exec("javac \"C:/java/appletTest/appletTest.java\"");
```


----------



## sepala (May 20, 2010)

Thanks for the reply andy. Anyway, it didn't create any class file.. Why is that? Apart from that, what I have submitted here is a test code, following is the REAL method in my application


```
public void createClassFile()
    {
        File location = new File("");
        String locationString = location.getAbsolutePath();
        String code = "javac "+locationString+"\\appletTest.java\"";
        try {
            Process p = Runtime.getRuntime().exec(code);
        } 
        catch (IOException ex)
        {
            JOptionPane.showMessageDialog(null,ex);
        }
    }
```
Please help....


----------



## andythepandy (Jul 2, 2006)

Are you sure you have the correct path, and capitalisation? (is it .java, or .Java)


----------



## sepala (May 20, 2010)

yep, correct path, that's why I put getAbsolutePath() to make it sure. The capitalizinf is also OK.


----------



## andythepandy (Jul 2, 2006)

Make sure that the correct command is being generated by outputing it before you call javac:


```
File location = new File("");
        String locationString = location.getAbsolutePath();
        String code = "javac "+locationString+"\\appletTest.java\"";
        JOptionPane.showMessageDialog(null, code);
        try {
            Process p = Runtime.getRuntime().exec(code);
        } 
        catch (IOException ex)
        {
            JOptionPane.showMessageDialog(null,ex);
        }
```


----------



## sepala (May 20, 2010)

OOOPSSS!! My mistake andy... The path is the issue. In my real application, it creates a sub folder inside the parent folder, but in the given method I am not accessing the sub folder but the parent folder.. I corrected it, and everything is fine now.. Thanks a lot for the help any.. I really appreciate it..Thanks again..


----------

