Results 1 to 6 of 6
  1. #1
    Join Date
    Feb 2009
    Posts
    1
    Rep Power
    0

    Default Executing an external java program

    How to execute an external java program using Runtime class?
    I have used ,
    Process p=Runtime.getRuntime().exec("c:/j2sdk1.4.0/bin/helloworld.java ");
    But it throws a runtime IOException error:2 or error:123.
    Help me with the code. Thanks in advance.

  2. #2
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    A few things.

    First, you want to run the "java" or "javaw" command.

    Second, that command requires an option that defines the classpath, which tells "java" where to find classes.

    Third, "java" works with .class files, not .java. You have to compile the .java files into .class files.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Basically think about the steps you have to follow, working on with the command prompt.

    First of all you have to set the class path. On the command prompt you can do that as follows on the working folder.

    path=C:\Java\jdk1.4\bin\
    Depends on the Java installation path the version, the above command can change.

    Then you have to compile the Java class.

    javac MyClass.java
    Then run the class, that's what Steve clearly explain. You cannot run the *.java file. Only *.class file can run.

  4. #4
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    I hate to nitpick, but...

    path=C:\Java\jdk1.4\bin\

    sets the system path for finding programs, not the classpath. You need to set the "classpath" environment variable. You can also specify that on the "java" command using the "-cp" option.

    One more thing. You should *not* be working in the JDK's "bin" directory, or in any directory of the JDK. Set up a separate directory structure for your stuff.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Steve11235 View Post
    I hate to nitpick, but...

    path=C:\Java\jdk1.4\bin\
    I don't like this way too, because it's temporary. But some cases really useful, specially in industry. Debugging the code and stuff.

  6. #6
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    c:/j2sdk1.4.0/bin/helloworld.java is not a recognized executable by windows. '.java' files are not executable, and windows does not recognize "/", it uses "\".

    If you can't run it by typing it into 'run' from the start menu, then Runtime.exec isn't going to be able to run it either.

    First you need to run javac on your 'helloworld.java'. Assuming you did that in c:\j2sdk1.4.0\bin (which is generally a bad idea, but since you're learning...), then the command you would use in Runtime.exec woudl be something like

    "c:\\j2sdk1.4.0\\bin\\java -cp c:/j2sdk1.4.0/bin helloworld"

    Java uses "/" notation, which is why this seems all mixed up.

Similar Threads

  1. executing external programs
    By Levish2002 in forum AWT / Swing
    Replies: 0
    Last Post: 09-10-2008, 07:04 PM
  2. help-executing the program
    By j2vdk in forum New To Java
    Replies: 6
    Last Post: 08-30-2008, 09:18 PM
  3. Replies: 0
    Last Post: 04-04-2008, 02:40 PM
  4. Replies: 0
    Last Post: 10-04-2007, 09:33 PM
  5. Use a external file in my program
    By romina in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 05:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •