Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Executing cmd from Java, Compiling and Executing IN Runtime

  1. #1
    Member Andrew R's Avatar
    Join Date
    Jul 2013
    Location
    San Pedro Sula, Honduras
    Posts
    58
    My Mood
    Inspired
    Thanks
    11
    Thanked 1 Time in 1 Post

    Post Executing cmd from Java, Compiling and Executing IN Runtime

    Hi there,

    I am new to this forum. First post here. I've been working on a code lately that compiles and executes another main class:

    CompilerMaster.java :
    package compilermaster;
     
    import java.io.BufferedReader;
    import java.io.InputStream;
    import java.io.InputStreamReader;
     
    public class CompilerMaster {
     
        public static void printLines(String name, InputStream ins) throws Exception {
            String line = null;
            BufferedReader in = new BufferedReader( new InputStreamReader(ins) );
            while ((line = in.readLine()) != null) { 
                System.out.println(name + " " + line);
            }
        }
     
        private static void runProcess(String command) throws Exception {
        Process pro = Runtime.getRuntime().exec(command); // <------------- this executes cmd lines
        printLines(command + " stdout:", pro.getInputStream());  // <------------- Gets info if it worked
        printLines(command + " stderr:", pro.getErrorStream());  // <------------- Gets info on any errors
        pro.waitFor();
        System.out.println(command + " exitValue() " + pro.exitValue());
        }
     
        public static void main(String[] args) {
     
            try { runProcess("javac Test.java");
                  runProcess("java Test"); 
            }
     
            catch (Exception e) { e.printStackTrace(); }
     
        }
     
    }

    Test.java:
    package compilermaster;
     
    public class Test {
     
        public static void main(String[] args) {
            System.out.println("ok");
        }
    }

    Output:
    run:
    javac Test.java stderr: javac: file not found: Test.java
    javac Test.java stderr: Usage: javac <options> <source files>
    javac Test.java stderr: use -help for a list of possible options
    javac Test.java exitValue() 2
    java Test stderr: Error: Could not find or load main class Test
    java Test exitValue() 1
    BUILD SUCCESSFUL (total time: 1 second)
    In theory, the program should be printing "ok".

    Additional Notes: I am using NetBeans IDE 7.3.1, classes are saved in the same location. If you substitute any of the commands with "dir" it doesn't work. Also tried adjust the cmd location using "cd (file location)" and did not work.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Executing cmd from Java, Compiling and Executing IN Runtime

    Your classes are part of a package. If you aren't familiar with packages, I'd suggest removing the IDE (netbeans) from the equation for the moment and compile/run your class files from the command line (either remove the package statements, or cd into the appropriate directory, addin the appropriate folder paths to your code)

  3. #3
    Member Andrew R's Avatar
    Join Date
    Jul 2013
    Location
    San Pedro Sula, Honduras
    Posts
    58
    My Mood
    Inspired
    Thanks
    11
    Thanked 1 Time in 1 Post

    Default Re: Executing cmd from Java, Compiling and Executing IN Runtime

    hey copeg,

    I used cmd to run the java program, and placed it into the default folder... and its working now!

    Thanks for the advice.

    I haven't tested placing a command to give it a different location, but I am sure it will work.
    So the issue was definitely the package / NetBeans issue. I am used to program in Notepad++ and running directly in the cmd.

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Executing cmd from Java, Compiling and Executing IN Runtime

    Now that you have that part working, get it working with packages too.

  5. #5
    Member Andrew R's Avatar
    Join Date
    Jul 2013
    Location
    San Pedro Sula, Honduras
    Posts
    58
    My Mood
    Inspired
    Thanks
    11
    Thanked 1 Time in 1 Post

    Default Re: Executing cmd from Java, Compiling and Executing IN Runtime

    hey jps,

    I've been trying to use Netbeans with the whole package deal / code... I am still unable to make it work.

    I think I am looking at the correct place at least.

    Managing Source and Class Files (The Java™ Tutorials > Learning the Java Language > Packages)

    I find this Packages & CLASSPATH in Java more useful and easier to understand. I still havent been albe to figure out what to include as command. Although I understand that I need to set a CLASS_PATH or a $BASE_DIR.

  6. #6
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Executing cmd from Java, Compiling and Executing IN Runtime

    A package is the same thing as a folder, just a fancy name
    com.sampledomain.projectrelatedfoldername.otherfol der.morespecfiicfolder.exactfoldername
    This just means that the Java file is located inside a folder named "exactfoldername" which is found in a folder named "morespecificfolder" which is found in "otherfolder" which is found in "projectrelatedfoldername" which is found in "sampledomain" which is found in "com"
    so path to the above file would look more like ".../com/sampledomain/projectrelatedfoldername/otherfolder/morespecificfolder/exactfoldername/javafileitself.xxx"
    You have to modify the path to include the folder hierarchy created within the project
    There can be as many or as few packages as you like to have. Also search for the default package, the package used when no package is specifically listed. Plenty of search results available
    Scribble up a "hello world" program and make it run without the package, and if you can not get it to run using packages post the sample here

  7. #7
    Member Andrew R's Avatar
    Join Date
    Jul 2013
    Location
    San Pedro Sula, Honduras
    Posts
    58
    My Mood
    Inspired
    Thanks
    11
    Thanked 1 Time in 1 Post

    Default Re: Executing cmd from Java, Compiling and Executing IN Runtime

    Well thanks for your help.

Similar Threads

  1. [SOLVED] Null error when executing in cmd prompt
    By Nasty in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 7th, 2013, 11:28 AM
  2. Help: Executing perl script from java
    By jessie in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 27th, 2012, 08:07 AM
  3. Executing query from java program
    By ashu000 in forum JDBC & Databases
    Replies: 1
    Last Post: June 25th, 2011, 12:43 PM
  4. Help Executing C Program From Java! (Ubuntu)
    By penguinGirl in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 18th, 2010, 01:47 PM
  5. Executing Linux Commands with Java GUI
    By linuxrockers in forum AWT / Java Swing
    Replies: 2
    Last Post: February 15th, 2010, 10:57 PM

Tags for this Thread