#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2008
    Posts
    43
    Rep Power
    11

    How to run a batch file from a java program


    Hi guys,

    I'm trying to run a batch file from a java program, i have never done this before, i tried writting the code:

    import java.io.*;

    public class bat_test {

    public static void main(String[] args){

    String line=("cmd.exe/c C:\\Batch_Test\\upload_files.bat");
    Process pp=run.exec(line);
    {
    {

    When compiling:
    =================
    bat_test.java:8: cannot find symbol
    symbol : variable run
    location: class bat_test
    Process pp=run.exec(line);
    ^
    1 error

    When running:
    =================
    java.lang.NoClassDefFoundError: batch_test
    Caused by: java.lang.ClassNotFoundException: batch_test
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClassInternal(Unknown Source)
    Could not find the main class: batch_test. Program will exit.
    Exception in thread "main"

    Can somebody help me out please?

    Regards,
    hellboy83
  2. #2
  3. Feelin' Groovy
    Devshed Supreme Being (6500+ posts)

    Join Date
    Aug 2001
    Location
    Chicago, IL
    Posts
    10,131
    Rep Power
    5058
    #1: If your code doesn't compile, there isn't any sense trying to run it.

    #2: You haven't declared a variable named "run" anywhere, so the compiler doesn't know what you're talking about when you write "run.exec(line)". That's what the "cannot find symbol" message means - the compiler tells you which symbol it cannot understand ("run", in this case).

    ~
    Yawmark
    class Sig{public static void main(String...args){\u0066or(int
    \u0020$:"v\"ʲ\"vΤ\"".to\u0043h\u0061rArray()
    )System./*goto/*$/%\u0126//^\u002A\u002Fout.print((char)(($>>
    +(~'"'&'#'))+('<'>>('\\'/'.')/\u002Array.const(~1)\*\u002F)));}}
  4. #3
  5. Feelin' Groovy
    Devshed Supreme Being (6500+ posts)

    Join Date
    Aug 2001
    Location
    Chicago, IL
    Posts
    10,131
    Rep Power
    5058
    Addendum: You may also want to have a look at ProcessBuilder. Example:
    Java Code:
    import java.io.*;
     
    public class Foo {
     
        public static void main(String[] args) throws IOException {
            java.lang.ProcessBuilder pb = new ProcessBuilder("cmd.exe", "/C", "test.bat");
            java.lang.Process p = pb.start();
     
            String line;
            BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
            while ((line = r.readLine()) != null) {
                System.out.println(line);
            }
            r.close();
        }
    }


    BAT Code:
    :: test.bat
    echo "Hello, World!"


    Output:
    Code:
    C:\dev\java\scratch>echo "Hello, World!" 
    "Hello, World!
    ~
    Yawmark
    class Sig{public static void main(String...args){\u0066or(int
    \u0020$:"v\"ʲ\"vΤ\"".to\u0043h\u0061rArray()
    )System./*goto/*$/%\u0126//^\u002A\u002Fout.print((char)(($>>
    +(~'"'&'#'))+('<'>>('\\'/'.')/\u002Array.const(~1)\*\u002F)));}}
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2008
    Posts
    43
    Rep Power
    11
    Yawmark thanks for the huge help, i got it running. Now i can call it a day

    Regards,
    hellboy83

    Comments on this post

    • Yawmark agrees : You're welcome! Glad you got it running!

IMN logo majestic logo threadwatch logo seochat tools logo