Java APIs to execute external execute file or command

I would like to enquire about the 'program statement' to execute an external program or dos command (e.g 'cls' to clear the screen) in a java source program.
Is there any built-in API s which like 'execute' or something else so as to execute an external command ?

Look at Runtime.exec()
However, 'cls' is not a external command in Windows it is and internal command and cannot be run using Runtime.exec()
If you are expecting to run programs in the same 'command prompt' window, you are going to be disappointed. That window is already running java.exe.

Similar Messages

  • How To Run An External .exe File With Command Line Arguments

    Hiya, could anyone tell me how I can run an external .exe file with command line arguments in Java, and if possible catch any printouts the external .exe file prints to the command line.
    Thanks.

    Using the Runtime.exec() command. And read this:
    http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html

  • Java API for running entire ".sql" files on a remote DB ( mySQL or Oracle)?

    Hi,
    Would anyone happen to know if there's a java API for executing entire ".sql" files (containing several different SQL commands), on a remote database server ?
    It's enough if the API works with MySQL and/or Oracle.
    Just to demonstrate what i'm looking for:
    Suppose you've created sql file "c:/test.sql" with several script lines:
    -- test.sql:
    insert into TABLE1 values(3,3);
    insert into TABLE1 values(5,5);
    create table TABLE2 (name VARCHER) ENGINE innoDB; -- MYSQL specific
    Then the java API should look something like:
    // Dummy java code:
    String driver="com.mysql.jdbc.Driver";
    String url= "jdbc:mysql://localhost:3306/myDb";
    SomeAPI.executeScriptFile( "c:/test.sql", driver, url);
    Thanks.

    No such a API, but it's easy to parse all sqls in a file, then run those command:
    For instance:
    import java.sql.*;
    import java.util.Properties;
    /* A demo show how to load some sql statements. */
    public class testSQL {
    private final static Object[] getSQLStatements(java.util.Vector v) {
    Object[] statements = new Object[v.size()];
    Object temp;
    for (int i = 0; i < v.size(); i++) {
    temp = v.elementAt(i);
    if (temp instanceof java.util.Vector)
    statements[i] = getSQLStatements( (java.util.Vector) temp);
    else
    statements[i] = temp;
    return statements;
    public final static Object[] getSQLStatements(String sqlFile) throws java.
    io.IOException {
    java.util.Vector v = new java.util.Vector(1000);
    try {
    java.io.BufferedReader br = new java.io.BufferedReader(new java.io.
    FileReader(sqlFile));
    java.util.Vector batchs = new java.util.Vector(10);
    String temp;
    while ( (temp = br.readLine()) != null) {
    temp = temp.trim();
    if (temp.length() == 0)
    continue;
    switch (temp.charAt(0)) {
    case '*':
    case '"':
    case '\'':
    // System.out.println(temp);
    break; //Ignore any line which begin with the above character
    case '#': //Used to begin a new sql statement
    if (batchs.size() > 0) {
    v.addElement(getSQLStatements(batchs));
    batchs.removeAllElements();
    break;
    case 'S':
    case 's':
    case '?':
    if (batchs.size() > 0) {
    v.addElement(getSQLStatements(batchs));
    batchs.removeAllElements();
    v.addElement(temp);
    break;
    case '!': //Use it to get a large number of simple update statements
    if (batchs.size() > 0) {
    v.addElement(getSQLStatements(batchs));
    batchs.removeAllElements();
    String part1 = temp.substring(1);
    String part2 = br.readLine();
    for (int i = -2890; i < 1388; i += 39)
    batchs.addElement(part1 + i + part2);
    for (int i = 1890; i < 2388; i += 53) {
    batchs.addElement(part1 + i + part2);
    batchs.addElement(part1 + i + part2);
    for (int i = 4320; i > 4268; i--) {
    batchs.addElement(part1 + i + part2);
    batchs.addElement(part1 + i + part2);
    for (int i = 9389; i > 7388; i -= 83)
    batchs.addElement(part1 + i + part2);
    v.addElement(getSQLStatements(batchs));
    batchs.removeAllElements();
    break;
    default:
    batchs.addElement(temp);
    break;
    if (batchs.size() > 0) {
    v.addElement(getSQLStatements(batchs));
    batchs.removeAllElements();
    br.close();
    br = null;
    catch (java.io.FileNotFoundException fnfe) {
    v.addElement(sqlFile); //sqlFile is a sql command, not a file Name
    Object[] statements = new Object[v.size()];
    for (int i = 0; i < v.size(); i++)
    statements[i] = v.elementAt(i);
    return statements;
    public static void main(String argv[]) {
    try {
    String url;
    Object[] statements;
    switch (argv.length) {
    case 0: //Use it for the simplest test
    case 1:
    url = "jdbc:dbf:/.";
    if (argv.length == 0) {
    statements = new String[1];
    statements[0] = "select * from test";
    else
    statements = argv;
    break;
    case 2:
    url = argv[0];
    statements = getSQLStatements(argv[1]);
    break;
    default:
    throw new Exception(
    "Syntax Error: java testSQL url sqlfile");
    Class.forName("com.hxtt.sql.dbf.DBFDriver").newInstance();
    //Please see Connecting to the Database section of Chapter 2. Installation in Development Document
    Properties properties = new Properties();
    Connection con = DriverManager.getConnection(url, properties);
    Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
    ResultSet.CONCUR_READ_ONLY);
    //Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
    // stmt.setMaxRows(0);
    stmt.setFetchSize(10);
    final boolean serializeFlag = false;//A test switch to serialize/deserialize the resultSet
    ResultSet rs;
    for (int i = 0; i < statements.length; i++) {
    if (statements[i] instanceof java.lang.String) {
    String temp = (java.lang.String) statements;
    switch (temp.charAt(0)) {
    case 'S':
    case 's':
    case '?':
    System.out.println(temp);
    rs = stmt.executeQuery(temp);
    if (serializeFlag) {
    // serialize the resultSet
    try {
    java.io.FileOutputStream fileOutputStream = new
    java.io.FileOutputStream("testrs.tmp");
    java.io.ObjectOutputStream
    objectOutputStream = new java.io.
    ObjectOutputStream(fileOutputStream);
    objectOutputStream.writeObject(rs);
    objectOutputStream.flush();
    objectOutputStream.close();
    fileOutputStream.close();
    catch (Exception e) {
    System.out.println(e);
    e.printStackTrace();
    System.exit(1);
    rs.close(); //Let the CONCUR_UPDATABLE resultSet release its open files at once.
    rs = null;
    // deserialize the resultSet
    try {
    java.io.FileInputStream fileInputStream = new
    java.io.FileInputStream("testrs.tmp");
    java.io.ObjectInputStream objectInputStream = new
    java.io.ObjectInputStream(
    fileInputStream);
    rs = (ResultSet) objectInputStream.
    readObject();
    objectInputStream.close();
    fileInputStream.close();
    catch (Exception e) {
    System.out.println(e);
    e.printStackTrace();
    System.exit(1);
    ResultSetMetaData resultSetMetaData = rs.
    getMetaData();
    int iNumCols = resultSetMetaData.getColumnCount();
    for (int j = 1; j <= iNumCols; j++) {
    // System.out.println(resultSetMetaData.getColumnName(j));
    /* System.out.println(resultSetMetaData.getColumnType(j));
    System.out.println(resultSetMetaData.getColumnDisplaySize(j));
    System.out.println(resultSetMetaData.getPrecision(j));
    System.out.println(resultSetMetaData.getScale(j));
    System.out.println(resultSetMetaData.
    getColumnLabel(j)
    + " " +
    resultSetMetaData.getColumnTypeName(j));
    Object colval;
    rs.beforeFirst();
    long ncount = 0;
    while (rs.next()) {
    // System.out.print(rs.rowDeleted()+" ");
    ncount++;
    for (int j = 1; j <= iNumCols; j++) {
    colval = rs.getObject(j);
    System.out.print(colval + " ");
    System.out.println();
    rs.close(); //Let the resultSet release its open tables at once.
    rs = null;
    System.out.println(
    "The total row number of resultset: " + ncount);
    System.out.println();
    break;
    default:
    int updateCount = stmt.executeUpdate(temp);
    System.out.println(temp + " : " + updateCount);
    System.out.println();
    else if (statements[i] instanceof java.lang.Object[]) {
    int[] updateCounts;
    Object[] temp = (java.lang.Object[]) statements[i];
    try {
    for (int j = 0; j < temp.length; j++){
    System.out.println( temp[j]);
    stmt.addBatch( (java.lang.String) temp[j]);
    updateCounts = stmt.executeBatch();
    for (int j = 0; j < temp.length; j++)
    System.out.println((j+1)+":"+temp[j]);
    for (int j = 0; j < updateCounts.length; j++)
    System.out.println((j+1)+":" +updateCounts[j]);
    catch (java.sql.BatchUpdateException e) {
    updateCounts = e.getUpdateCounts();
    for (int j = 0; j < updateCounts.length; j++)
    System.out.println((j+1)+":"+updateCounts[j]);
    java.sql.SQLException sqle = e;
    do {
    System.out.println(sqle.getMessage());
    System.out.println("Error Code:" +
    sqle.getErrorCode());
    System.out.println("SQL State:" + sqle.getSQLState());
    sqle.printStackTrace();
    while ( (sqle = sqle.getNextException()) != null);
    catch (java.sql.SQLException sqle) {
    do {
    System.out.println(sqle.getMessage());
    System.out.println("Error Code:" +
    sqle.getErrorCode());
    System.out.println("SQL State:" + sqle.getSQLState());
    sqle.printStackTrace();
    while ( (sqle = sqle.getNextException()) != null);
    stmt.clearBatch();
    System.out.println();
    stmt.close();
    con.close();
    catch (SQLException sqle) {
    do {
    System.out.println(sqle.getMessage());
    System.out.println("Error Code:" + sqle.getErrorCode());
    System.out.println("SQL State:" + sqle.getSQLState());
    sqle.printStackTrace();
    while ( (sqle = sqle.getNextException()) != null);
    catch (Exception e) {
    System.out.println(e.getMessage());
    e.printStackTrace();

  • Executing .sql scripts file from command prompt

    I had created the .sql scripts for
    --Create Database
    --Create tables
    --Create Stored procedure
    I am using SQL server 2008.
    Now i want to deploy it into the target server using the command prompt.
    How can I call to execute that script on the target server?

    with few errors as "Incorrect Syntax near "GO"".
    Hello,
    GO is a command, which is only known & interpretted by SSMS + SqlCmd.exe; SQL Server engine / data access components don't know this command, therefore you get an error.
    See
    Query Options Execution (General Page); for SSMS you can change it from GO to any other term.
    Olaf Helper
    [ Blog] [ Xing] [ MVP]

  • Conversion of .csv file format to .xls format using java api

    Please help me with the code and the links to jars and libraries of an open source java api to convert the .csv file into .xls file with all propertirs running.
    Thanks

    umangjain87 wrote:
    Please help me with the code and the links to jars and libraries of an open source java api to convert the .csv file into .xls fileApache POI and jExcelApi are two libraries that can produce xls files.
    [A simple Google search|http://www.lmgtfy.com/?q=Java+Excel+library] could have told you as much.
    with all propertirs running.What does that mean.

  • How and where to install MDM Connector and MDM Java API

    Hi all
    I am installing MDM Server 5.5 and refering to Installation gude
    MDM 5.5 SP06 installation guide (Document Version 1.1 – December 10, 2007).  While installing Development and Portal components , We have to install MDM Connector and MDM JAVA API. I want to know whether the file JAVAAPI<version>.sca file is to extracted on MDM Server or SRM Server ( Using SDM). Please let me know
    Thanks in advance.
    Vitthal prabhu

    Hi Vitthal,
                 We have to install MDM Connector and MDM JAVA API.
    All these sca files that u got along with the business content needs to be deployed onto Web Application Server (recommended WAS 7.0 ).
    U can deploy these files with the help of SDM.
    Hope dis solves ur problem.
    Regards Tejas..............

  • MDM 5.5 Java API Library Reference Guide

    hi friends,
    I need MDM 55 Java API Library Reference Guide  pdf file
    please forward to me
    its urgent

    That is for pdf. I thought once you're signed on here, that should work, too. However, here is the online help (same docs):
    [http://help.sap.com/saphelp_mdm550/helpdata/en/43/D7AED5058201B4E10000000A11466F/frameset.htm]
    In Programming Interfaces on the left you will find the Java API guide.

  • Install MDM JAVA API

    Hi,
    I am using SAP MDM 5.5 SP05. Can someone guide me on how to install MDM Java API.
    I referred to many threads on SDN but couldn't get through any.
    I have already downloaded the java API which has one .sca file and two folders, each has some .jar files and javadoc folder.
    Kindly help on how should i go with it.
    Regards,
    Chintan Sheth

    Hi Chintan,
    You wont install MDM Java API as u install your MDM server or client.
    <b>What the use of the MDM Java API?</b>
    1. Stand-alone Java Applications.
    U can use MDM java API for your stand-alone applications that runs locally on your PC etc... For this you can import the MDM Java jar file into to the library of yourstand alone java application project.
    2. J2EE Application or custom MDM iview in EP or SAP Webdynpro Apllication.
    U can deploy the .sca file in the SAP WAS Java using the SDM client and u can extend the MDM Java API in your J2EE Application or custom MDM iview in EP or SAP Webdynpro Apllication
    get back for help...
    Regards,
    Vijay

  • Generate Java API Doc

    Hi,
    Is there any way to generate Java API Doc from the class files? I downloaded a set of Java class files from a web site, but it does not have any API docs, so I am hoping I can still generate the API docs from them?
    Any ways?
    thanks.

    so if I have no Java source file, there is no way to generate the java API docs?
    As I said, I downloaded a set of java classes, but without java doc inside it, I wanna generate the java doc to use them.
    any one can help?

  • Executing external files within java

    Could someone pls tell me how i can execute external files, i.e. .bat files within java. thanks.

    See java.lang.Runtime and java.lang.Process classes.
    http://java.sun.com/j2se/1.4.1/docs/api/java/lang/Runtime.html
    http://java.sun.com/j2se/1.4.1/docs/api/java/lang/Process.html
    And if you search this site you can probably find a ton of examples.
    Keep in mind that using this can limit portablilty of your Java application.

  • How to execute a .exe file in java(Jsp) without using a process ???

    Hi All ,
    How to execute a .exe file in Jsp without using a process ??? ...
    Is it Possiable ????

    itsdhanasaraa wrote:
    But as this a web application ... By using Runtime i'm getting some probs ..
    Let me guess, you want your web application to run a program on the client and to your surprise that's not working?
    Ain't gonna happen.
    its taking more time to execute .... that's y is there any other option to execute .exe file other than Runtime.getRuntime().exec("filename");Write proper English and you may be taken more seriously.
    1) it's not "taking more time to execute", whatever that's supposed to mean.
    2) there's no other way to execute something. Not that you should every use even that way anyway
    3) whenever you start thinking of executing external programs from Java, start thinking of not using Java in the first place.

  • Running an Executable from a JAVA API in LINUX

    Hi,
    I want to run a C++ executable from my JAVA API in Linux. This is the sample of the code which i
    have used
    String[] arguments = new String[5] ;
    arguments[0] = "/usr/local/code/fun/dcmdump";
    arguments[1] = "+f";
    arguments[2] = "/usr/local/code/fun/240.dcm";
    arguments[3] = "+W";
    arguments[4] = "/usr/local/code/fun";
    Process p = Runtime.getRuntime().exec(arguments);
    On running the test file, the program seems to hang there. dcmdump is the name of my c++ executable and arguments [1] - [4] are its inputs. dcmdump should accepts these inputs and dump the contents of the file 240.dcm into the path mentioned by the last argument.
    But my program jst hangs. Is the the correct way to go about??
    Any suggestions or ideas r welcome!!
    Please let me know
    Thanks
    Dhaval

    Hi
    I did the following
    String[] cmd ={"/bin/sh","-c","/usr/local/code/fun/dcmdump","+f","/usr/local/code/fun/240.dcm","+W","/usr/local/code/fun"};
    Process p = Runtime.getRuntime().exec(cmd);
    When i execute my application, i see the following output
    [root@voltaire fun]# java TestRead2
    $dcmtk: dcmdump v3.5.3 2004-05-27 $
    dcmdump: Dump DICOM file and data set
    usage: dcmdump [options] dcmfile-in...
    parameters:
    dcmfile-in DICOM input filename to be dumped
    general options:
    I try to write the arguments in the command line when i execute but still get the same output.
    I dont think dcmdump is receving any arguements..
    Executing /bin/sh does it mean you have to mention the arguements in the command in the same manner that i have the String[] cmd??
    PLease let me know..
    Also i have put a Buffered reader to capture the output of dcmdump but I dont think i have reached my application has reached thr as yet. The code i have written is
    // put a BufferedReader on the output
    InputStream inputstream = p.getInputStream();
    InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
    BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
    // read the output
    String line;
    while ((line = bufferedreader.readLine()) != null) {
    System.out.println(line);
    Please let me know your suggestions/ideas.
    thanks

  • How to execute a jar file in a java application

    Hi,
    I've already try to execute a jar file by this way :
    Runtime.getRuntime().exec(java -jar myJarFile.jar);
    But the problem is that the display doesn't launch. Normally a "JFrame" display.
    however the corresponding process is launched.
    So i would to know if there is a way to execute a Jar File without using the Runtime class.
    Is there a specific class for the Jar File?
    Thank you.
    Richard

    Create a 'manifest' file pointing to the 'main' class, jar it into the .jar file, place it onto your desktop - or whereever you can click on it, and click on it.

  • Java.io.filepermission error while executing a batch file from java prog

    Hi,
    i want run a java program which executes a batch file, both are in a jar file. while am trying this using webstart it shows error:access denied java.io.filepermission <<ALL FILES>>execute. why this happens how to rectify this.
    By
    Vinod

    Clearly, it would be a security vulnerability to be able to do such a thing from the web w/o user granting trust to the application.
    Java Web Start applications run in the Java SE secure sandbox unless they have been granted all-permissions by the user:
    1.) sign all jar files.
    2.) add <security><all-permissions/></security> to the jnlp file.
    The user would then be prompted to grant trust to the applications.
    /Andy

  • Through Java code I want to execute a exe file which is in aJar file

    I am having some classes and an exe file in a directory. I have made them in to a Jar file. In a class file which is in that jar file i want to execute a Exe file which is also resides in that jar file. Is it possible to exexute that EXE file?
    For Example....
    1. Im having a directory named CLIENT.
    2. In that directory I have 10 clss files and an EXE file.
    3. These class files and EXE files are ziped in to a Jar file.
    4. I have to give the Jar file to my client.
    5. He can put that Jar file where ever he installed my product may be C driver or D drive like that
    Now the problem is...
    I want to execute the Exe File from one of the class where both the exe file and class file resides in the Jar file
    This is my requirment
    Can anyone Help to me to solve this problem?
    Thanks in Advancd
    Ibram Shah.A.M
    ([email protected])

    The answer is to extract the EXE into a temp directory, execute it, and delete it when you're done. For example:
    //This is the path *inside* the JAR file!
    InputStream in = getClass().getResourceAsStream("/resources/myprog.exe");
    OutputStream out = new FileOutputStream("myprog.exe");
    File file = new File("myprog.exe");
    int data;
    while((data = in.read()) >= 0) out.write(data);
    in.close();
    out.close();
    //Execute the EXE here using java.lang.Runtime.exec()
    if(file.exists()) file.delete();
    ...

Maybe you are looking for