Results 1 to 7 of 7
  1. #1
    wiz0r is offline Member
    Join Date
    Apr 2009
    Posts
    5
    Rep Power
    0

    Default [SOLVED] Running java programs from the command prompt

    Hello:

    I need to run one of my projects from the command prompt. Asides from running it normally like after compiling it like:

    Java package.MainClass


    I need to run it like:

    Java package.MainClass fileName.txt

    What does that means? I mean, what can I do with that .txt? I'm supposed to use that text to recover data, but I just don't understand how can I do that.

    The last one is:

    Java package.MainClass < fileName.txt

    That one is easy, the only thing it does is read the command from that fileName instead of the keyboard. Still I don't understand what to do with the second one...

    I need help, please. Link to where I can read about it would be very helpful, or any examples, whatever... any help is appreciated.

    Edwin

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

    Default

    That's all about command line arguments. If you want to give arguments to your application through the command line, the command should be like follows.

    java main_class argument_list

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

    Default

    Here is a simple example. Look at the following code.

    Java Code:
    public class ParameterTest {
    	public static void main(String[] args) {
    		if(args.length == 1) {
    			System.out.println(args[0]);
    		}
    		else {
    			System.out.println("Arguments not found");
    		}
    	}
    }
    First of all you have to compile and then run. If you run like this,

    java ParameterTest
    you didn't provide any arguments. So the length of args String array is zero. And the output is,

    Arguments not found
    If you run the class as follows,

    java ParameterTest parameter_one
    you have one argument here. And the result is,

    parameter_one

  4. #4
    Mr.Beans's Avatar
    Mr.Beans is offline Senior Member
    Join Date
    Apr 2009
    Location
    Virginia Beach, Virginia
    Posts
    149
    Rep Power
    0

    Default

    Are you familiar with what the static void main methods argument?
    Java Code:
    public static void main(String[] args) {
            for(int i = 0; i < args.length; i++) 
                    System.out.println(args[i]);
    }
    If you ran that from the terminal via
    Java Code:
    java ArgumentPrinter filename.txt Hello World!
    It would print
    Java Code:
    filename.txt
    Hello
    World!
    EDIT: Sorry didn't see eranga's post. Someone always beats me to it

  5. #5
    wiz0r is offline Member
    Join Date
    Apr 2009
    Posts
    5
    Rep Power
    0

    Default

    Yes, that's what I was looking for. Thanks a lot guys! I'll try to finish my project now. I'll come back later if I still got some doubts. :]

    Edwin

  6. #6
    wiz0r is offline Member
    Join Date
    Apr 2009
    Posts
    5
    Rep Power
    0

    Default

    Thank you guys, project completed. :)

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

Similar Threads

  1. Running applet from command prompt
    By niteshwar.bhardwaj in forum Java 2D
    Replies: 1
    Last Post: 03-12-2009, 09:10 AM
  2. Running 3 Java programs in sequence.
    By loktamu in forum New To Java
    Replies: 2
    Last Post: 11-23-2008, 07:50 PM
  3. problems in running d command prompt commands
    By postaholic066 in forum Advanced Java
    Replies: 1
    Last Post: 09-10-2008, 04:47 PM
  4. Problem in running java programs
    By aravind in forum New To Java
    Replies: 0
    Last Post: 07-14-2008, 12:19 PM
  5. Running eclipse java project on command prompt
    By hnmapara in forum New To Java
    Replies: 1
    Last Post: 07-09-2008, 09:29 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
  •