Johanna
Johanna

Reputation: 27628

command_line argument

I can not get the difference between these sentences! would you please write some snippet code for these sentences?thanks

Upvotes: 0

Views: 234

Answers (3)

Andreas Dolk
Andreas Dolk

Reputation: 114757

Imagine a command line copy program that you use like that:

copy <destination-dir> <source-file>

A simple implementation in Java would be (provided as a fragment):

package com.example;
import java.io.File;
public class Copy {

  public static void main(String[] args) {

    if (args.length != 2) {
      exitWithErrorCode(); // to be implemented
    }

    File destinationDir = new File(args[0]);
    File sourceFile = new File(args[1]);

    copyFileToDir(sourceFile, destinationDir); 
  }

  private static void copyFileToDir(File sourceFile, File destDir) {
    // to be implemented
  }
}

and you would call it like

java com.example.Copy /tmp /home/me/example.txt

Upvotes: 1

Pointy
Pointy

Reputation: 413682

It means that the program will be run like this:

java some.package.YourProgram /some/directory /some/file/name

Upvotes: 0

AndiDog
AndiDog

Reputation: 70108

It's as simple as that:

public static void main(String[] args)
{
    // args[0] is the directory path
    // args[1] is the file path
}

So what don't you understand?

Upvotes: 1

Related Questions