ℹ️ Skipped - page is already crawled
| Filter | Status | Condition | Details |
|---|---|---|---|
| HTTP status | PASS | download_http_code = 200 | HTTP 200 |
| Age cutoff | PASS | download_stamp > now() - 6 MONTH | 1.6 months ago |
| History drop | PASS | isNull(history_drop_reason) | No drop reason |
| Spam/ban | PASS | fh_dont_index != 1 AND ml_spam_score = 0 | ml_spam_score=0 |
| Canonical | PASS | meta_canonical IS NULL OR = '' OR = src_unparsed | Not set |
| Property | Value |
|---|---|
| URL | http://web.mit.edu/6.031/www/sp19/projects/crossword/commandline.html |
| Last Crawled | 2026-02-27 15:52:29 (1 month ago) |
| First Indexed | not set |
| HTTP Status Code | 200 |
| Meta Title | Running Java Programs with Command-Line Arguments |
| Meta Description | null |
| Meta Canonical | null |
| Boilerpipe Text | From command prompt
Open a command prompt and
cd
into your project folder.
Then run the
java
command with these arguments:
java -cp bin:lib/parserlib.jar some.package.Main
argument1 argument2 …
-cp
specifies the
classpath
where Java should search for compiled code.
bin
is the folder where Eclipse compiles your code, and
lib/parserlib.jar
is the library jar file your code depends on.
some.package.Main
is the class containing your
main()
method, and
some.package
is the package that contains it.
argument1 argument2 …
are the command-line arguments that you are passing to your program. For example,
for the crossword server, you have only one argument, which is the location of the folder containing your puzzles.
For the crossword client, you also have only one argument, which is the address of the crossword server.
Windows note.
On Windows, you will have to put quotes around the classpath and use a semicolon instead
of a colon to separate multiple folders, so you would type
java -cp "bin;lib/parserlib.jar" some.package.Main
argument1 argument2 …
From Eclipse
You can also specify command-line arguments in Eclipse using the menu command Run → Run Configurations, choosing the class containing your
main()
method in the dialog box, then selecting the Arguments tab.
Enter your arguments in the Program Arguments box, not the VM Arguments box.
Using a JAR
Eclipse can also export your program as a “runnable JAR file” that contains all the code in a single file.
In Eclipse, select File → Export, then Java → Runnable JAR File.
Launch Configuration
identifies which program should be exported.
Find the one that runs the class containing your
main()
method.
Export Destination
is the name and location of the JAR file you want to generate.
Put it somewhere you can find it easily to run it, like your desktop or home folder.
Don’t
put it in your git repo, because it isn’t source code.
For
Library Handling
, keep the default, “Extract required libraries into generated JAR.”
This will ensure that your generated JAR file includes the code from the other jar files you depend on, like
parserlib.jar
in this project.
After you finish the dialog, open your command prompt and
cd
to the folder containing the jar file you just generated.
Assuming its name is
myproject.jar
, you should now be able to start your program using:
java -jar myproject.jar
You may find that your program can no longer find its ParserLib grammar file, because it is trying to read it as a file relative to the project folder, and you are no longer running the program from your project folder.
A better way would read the grammar as a
classpath resource
, which allows the code to be packed up in a jar, along with the grammar file, and still be able to find the grammar.
Here’s an example from the inclass code for the Parsers class.
First, put the grammar file
IntegerExpression.g
in the same folder as the Java class
IntegerExpressionParser.java
that needs to read it.
Then use this code in
IntegerExpressionParser.java
:
InputStream grammarStream = IntegerExpression.class.openResourceAsStream("IntegerExpression.g");
return Parser.compile(grammarStream, IntegerGrammar.ROOT);
A
JavaWorld article has a discussion of classpath resources
. |
| Markdown | [6\.031](http://web.mit.edu/6.031/www/sp19/)
[6\.031 — Software Construction](http://web.mit.edu/6.031/www/sp19/)
Spring 2019
- [Running Java Programs with Command-Line Arguments](http://web.mit.edu/6.031/www/sp19/projects/crossword/commandline.html#running_java_programs_with_command-line_arguments)
- [From command prompt](http://web.mit.edu/6.031/www/sp19/projects/crossword/commandline.html#from_command_prompt)
- [From Eclipse](http://web.mit.edu/6.031/www/sp19/projects/crossword/commandline.html#from_eclipse)
- [Using a JAR](http://web.mit.edu/6.031/www/sp19/projects/crossword/commandline.html#using_a_jar)
# Running Java Programs with Command-Line Arguments
## From command prompt
Open a command prompt and `cd` into your project folder. Then run the `java` command with these arguments:
`java -cp bin:lib/parserlib.jar some.package.Main` *argument1 argument2 …*
`-cp` specifies the *classpath* where Java should search for compiled code. `bin` is the folder where Eclipse compiles your code, and `lib/parserlib.jar` is the library jar file your code depends on.
`some.package.Main` is the class containing your `main()` method, and `some.package` is the package that contains it.
*argument1 argument2 …* are the command-line arguments that you are passing to your program. For example, for the crossword server, you have only one argument, which is the location of the folder containing your puzzles. For the crossword client, you also have only one argument, which is the address of the crossword server.
**Windows note.** On Windows, you will have to put quotes around the classpath and use a semicolon instead of a colon to separate multiple folders, so you would type
`java -cp "bin;lib/parserlib.jar" some.package.Main` *argument1 argument2 …*
## From Eclipse
You can also specify command-line arguments in Eclipse using the menu command Run → Run Configurations, choosing the class containing your `main()` method in the dialog box, then selecting the Arguments tab. Enter your arguments in the Program Arguments box, not the VM Arguments box.
## Using a JAR
Eclipse can also export your program as a “runnable JAR file” that contains all the code in a single file.
In Eclipse, select File → Export, then Java → Runnable JAR File.
- *Launch Configuration* identifies which program should be exported. Find the one that runs the class containing your `main()` method.
- *Export Destination* is the name and location of the JAR file you want to generate. Put it somewhere you can find it easily to run it, like your desktop or home folder. *Don’t* put it in your git repo, because it isn’t source code.
- For *Library Handling*, keep the default, “Extract required libraries into generated JAR.” This will ensure that your generated JAR file includes the code from the other jar files you depend on, like `parserlib.jar` in this project.
After you finish the dialog, open your command prompt and `cd` to the folder containing the jar file you just generated. Assuming its name is `myproject.jar`, you should now be able to start your program using:
`java -jar myproject.jar`
You may find that your program can no longer find its ParserLib grammar file, because it is trying to read it as a file relative to the project folder, and you are no longer running the program from your project folder. A better way would read the grammar as a *classpath resource*, which allows the code to be packed up in a jar, along with the grammar file, and still be able to find the grammar. Here’s an example from the inclass code for the Parsers class. First, put the grammar file `IntegerExpression.g` in the same folder as the Java class `IntegerExpressionParser.java` that needs to read it. Then use this code in `IntegerExpressionParser.java`:
```
```
A [JavaWorld article has a discussion of classpath resources](http://www.javaworld.com/article/2077352/java-se/smartly-load-your-properties.html).
MIT EECS
spring 2019 course site archive \| latest site at [mit.edu/6.031](http://web.mit.edu/6.031/) \| [accessibility](http://accessibility.mit.edu/) |
| Readable Markdown | ## From command prompt
Open a command prompt and `cd` into your project folder. Then run the `java` command with these arguments:
`java -cp bin:lib/parserlib.jar some.package.Main` *argument1 argument2 …*
`-cp` specifies the *classpath* where Java should search for compiled code. `bin` is the folder where Eclipse compiles your code, and `lib/parserlib.jar` is the library jar file your code depends on.
`some.package.Main` is the class containing your `main()` method, and `some.package` is the package that contains it.
*argument1 argument2 …* are the command-line arguments that you are passing to your program. For example, for the crossword server, you have only one argument, which is the location of the folder containing your puzzles. For the crossword client, you also have only one argument, which is the address of the crossword server.
**Windows note.** On Windows, you will have to put quotes around the classpath and use a semicolon instead of a colon to separate multiple folders, so you would type
`java -cp "bin;lib/parserlib.jar" some.package.Main` *argument1 argument2 …*
## From Eclipse
You can also specify command-line arguments in Eclipse using the menu command Run → Run Configurations, choosing the class containing your `main()` method in the dialog box, then selecting the Arguments tab. Enter your arguments in the Program Arguments box, not the VM Arguments box.
## Using a JAR
Eclipse can also export your program as a “runnable JAR file” that contains all the code in a single file.
In Eclipse, select File → Export, then Java → Runnable JAR File.
- *Launch Configuration* identifies which program should be exported. Find the one that runs the class containing your `main()` method.
- *Export Destination* is the name and location of the JAR file you want to generate. Put it somewhere you can find it easily to run it, like your desktop or home folder. *Don’t* put it in your git repo, because it isn’t source code.
- For *Library Handling*, keep the default, “Extract required libraries into generated JAR.” This will ensure that your generated JAR file includes the code from the other jar files you depend on, like `parserlib.jar` in this project.
After you finish the dialog, open your command prompt and `cd` to the folder containing the jar file you just generated. Assuming its name is `myproject.jar`, you should now be able to start your program using:
`java -jar myproject.jar`
You may find that your program can no longer find its ParserLib grammar file, because it is trying to read it as a file relative to the project folder, and you are no longer running the program from your project folder. A better way would read the grammar as a *classpath resource*, which allows the code to be packed up in a jar, along with the grammar file, and still be able to find the grammar. Here’s an example from the inclass code for the Parsers class. First, put the grammar file `IntegerExpression.g` in the same folder as the Java class `IntegerExpressionParser.java` that needs to read it. Then use this code in `IntegerExpressionParser.java`:
```
```
A [JavaWorld article has a discussion of classpath resources](http://www.javaworld.com/article/2077352/java-se/smartly-load-your-properties.html). |
| Shard | 180 (laksa) |
| Root Hash | 10722954425220430980 |
| Unparsed URL | edu,mit!web,/6.031/www/sp19/projects/crossword/commandline.html h80 |