- 1 1. හැඳින්වීම
- 2 2. Command‑Line Arguments කියන්නේ කුමක්ද?
- 3 3. මූලික භාවිතය සහ කේත උදාහරණ
- 4 4. සාමාන්ය වැරදි සහ සැලකිලිමත් කිරීම්
- 5 5. ප්රායෝගික භාවිතය: Type Conversion, Option Arguments, සහ Default Values
- 6 6. Applied Examples: Common Real-World Patterns
- 7 7. Testing and Debugging Tips / Configuring Arguments in IDEs
1. හැඳින්වීම
මෙම අධ್ಯಾಯයේ අරමුණ
Java command‑line arguments යනු වැඩසටහන් රන්ටයිම්දී බාහිර අගයන් ලබාගෙන ඒවා අනුව හැසිරීම වෙනස් කිරීමට ඉඩ දෙන මූලික විශේෂාංගයකි. මෙම ලිපිය String[] args යන අර්ථය සිට සැබෑ ලෝක යෙදුම්වල භාවිතා වන ප්රායෝගික සැලසුම් රටාවන් දක්වා සියල්ල පැහැදිලි කරයි. මෙම අධ್ಯಾಯයේ අපි පළමුව command‑line arguments මඟින් කුමක් කළ හැකිද, ඒවා කුමන අවස්ථාවල ප්රයෝජනවත්ද යන්න පැහැදිලි කරමු.
Command‑Line Arguments කියන්නේ කුමක්ද?
Java යෙදුමක් සාමාන්යයෙන් පහත සත්යය (signature) සමඟ main ක්රමය තුළ ආරම්භ වේ:
public class App {
public static void main(String[] args) {
// args is an array of strings passed at runtime
}
}
args යනු string අරේ එකක් වන අතර, වැඩසටහන ආරම්භ කරන විට ලබා දෙන අගයන් ගබඩා කරයි. උදාහරණයක් ලෙස:
javac App.java
java App Tokyo 2025 debug
මෙහි args හි අන්තර්ගතය ["Tokyo", "2025", "debug"] වේ.
ආර්ගුමෙන්ට් කිසිවක් ලබා නොදෙන්නේ නම්, args.length යනු 0 වේ.
සාමාන්ය භාවිත අවස්ථා
- පරිසර හෝ ඉලක්ක මාරු කිරීම : නිෂ්පාදන vs පරීක්ෂණ මාදිලිය, කලාප කේත, භාෂා, ලොග් මට්ටම්.
- බාහිරවින් සැකසීමේ ඉලක්ක නියම කිරීම : ඇතුළත් ගොනු නාම, නාමාවලිය, URL, ID ලැයිස්තු.
- කණ්ඩායම් සැකසීම සහ ස්වයංක්රීය කිරීම : කාලසටහන් කළ රැකියා සඳහා දිනයන් හෝ පරාසයන් ලබා දීම, CI/CD පයිප්ලයින් වලින් පරාමිතීන් ඇතුළත් කිරීම.
සියලු අවස්ථාම, සංස්කරණය නොකළ තත්වයෙන් හැසිරීම වෙනස් කළ හැකි බැවින්, command‑line arguments ශෙල් ස්ක්රිප්ට් සහ cron වැනි රැකියා සැලසුම්කරු සමඟ භාවිතා කිරීමට ඉතා සුදුසු වේ.
ප්රධාන සැලසුම් සැලකිලි
- අවශ්ය සහ විකල්ප ආර්ගුමෙන්ට් වෙන් කිරීම : අවශ්ය ආර්ගුමෙන්ට් අස්ථානගත වූ විට, උදව් පණිවුඩයක් හෝ පිටවීමේ කේතයක් සමඟ පැහැදිලිව අසාර්ථක වන්න.
- ඉක්මනින් වලංගු කිරීම : සංඛ්යාත්මක හෝ දිනයේ අගයන් හැකි ඉක්මනින් පරිවර්තනය කර, අසත්ය ආදානය පිළිබඳ පැහැදිලි මාර්ගෝපදේශ ලබා දෙන්න.
- පෙරනිමි අගයන් සැලසුම් කිරීම : විකල්ප ආර්ගුමෙන්ට් සඳහා ආරක්ෂිත පෙරනිමි අගයන් තිබිය යුතු, එවිට වැඩසටහන තවමත් ක්රියාත්මක විය හැක.
- කියවීමට සහ නඩත්තු කිරීමට පහසුකම : සෘජු අරේ ප්රවේශය පතුරුවා නොදැමීම; ආර්ගුමෙන්ට් වස්තු (DTOs හෝ configuration classes) ලෙස විග්රහ කිරීම.
ආර්ගුමෙන්ට්, පරිසර විචල්ය, සහ සැකසුම් ගොනු අතර තේරීම
- Command‑line arguments : තාවකාලික අධික්රමණ හෝ රැකියා‑විශේෂ සැකසුම් සඳහා හොඳ (ඉහළ ප්රමුඛතාව, ස්ථානික සැකසුම).
- පරිසර විචල්ය : රහස් හෝ පරිසර‑විශේෂ අවසන්‑බින්දු සඳහා සුදුසු.
- සැකසුම් ගොනු (properties/JSON/YAML) : බහු‑සැකසුම් පද්ධති පද්ධතිකරණය, ප්රතිභාවිතය, සහ සංස්කරණ පාලනය සඳහා ඉතා සුදුසු.
ප්රායෝගිකව, සැකසුම් ගොනු + පරිසර විචල්ය + command‑line arguments යන ත්රි‑ස්තර සැලසුම වැඩි ප්රමුඛතාවය ලබා දේ, එහි command‑line arguments ඉහළම ප්රමුඛතාවය ගනී.
අවම උදාහරණය: සියලු ආර්ගුමෙන්ට් ලැයිස්තුගත කිරීම
public class ArgsEcho {
public static void main(String[] args) {
if (args.length == 0) {
System.out.println("No arguments were provided.");
System.out.println("Example: java ArgsEcho input.txt debug");
return;
}
System.out.println("Received arguments:");
for (int i = 0; i < args.length; i++) {
System.out.printf("args[%d] = %s%n", i, args[i]);
}
}
}
මෙම ලිපියේ ඊළඟ අංශ (මාර්ග සිතියම)
String[] argsමත මූලික මෙහෙයුම් (දිග පරීක්ෂා, අංග ප්රවේශය)- වර්ග පරිවර්තනය (int/double/boolean සහ εξαίρεση handling)
- විකල්ප ආකෘති
-v,--help,--mode=prodවැනි - IDE සැකසුම් සහ පරීක්ෂණයේ ආර්ගුමෙන්ට් ලබාදීම
- දෝෂ සැකසීම සහ ආරක්ෂක සැලකිලි (අසත්ය ආදානය, εξαίρεση කළමනාකරණය)
- ප්රායෝගික උදාහරණ (ගොනු සැකසීම, මාදිලිය මාරු කිරීම, ලොග් මට්ටම් පාලනය)
පළමුව, මෙම මූලික සංකල්පය මතක තබා ගන්න: සියලු ආර්ගුමෙන්ට් string ලෙස ලබා දෙන අතර, ඒවා භාවිතා කිරීමට පෙර ආරක්ෂිතව පරිවර්තනය කර සහ වලංගු කිරීම අවශ්ය වේ. ඊළඟ අධ್ಯಾಯයේ, මූලික වාක්ය රචනා සහ පොදු රටාවන් concrete code උදාහරණ සමඟ විස්තර කරමු.
2. Command‑Line Arguments කියන්නේ කුමක්ද?
main ක්රමය සහ String[] args අතර සම්බන්ධතාවය
Java යෙදුමේ ඇතුළත් වීමේ ස්ථානය main ක්රමය වන අතර, එය පහත පරිදි නිර්වචනය කර ඇත:
public static void main(String[] args)
මෙහි, args යනු “තර්ක” (arguments) සඳහා වන නාමය이며, පෙරළිය ආරම්භ වන විට පිටතින් ලබා දෙන අගයන් ගබඩා කරන 문자열 අරේ.
java ClassName පසු සඳහන් කරන, ඉඩක් (space) වලින් වෙන් කර ඇති අගයන්, ඔබ වැඩසටහනක් ධාවනය කරන විට, args තුළ අනුක්රමයෙන් ගබඩා වේ.
උදාහරණය: තර්ක සමඟ ධාවනය කිරීම
javac Sample.java
java Sample apple orange banana
මෙම අවස්ථාවේ, args හි අන්තර්ගතය පහත පරිදි වේ:
| Index | Value |
|---|---|
| args[0] | “apple” |
| args[1] | “orange” |
| args[2] | “banana” |
අනෙක් වචනයෙන් කියනවා නම්, args විචල්ය-දිගැති 문자열 ලැයිස්තුවක් වැනි ක්රියාවලියක් පවතින අතර, ඔබට ඕනෑම ගණනක් අගයන් පවරා ගැනීමට ඉඩ සලසයි.
තර්ක කිසිවක් නියම කර නොමැති නම්, args.length 0 වේ (එය කිසිවිටෙකත් null නොවේ).
ක්රියාත්මක කිරීමේ උදාහරණය සහ ප්රතිඵලය
public class Sample {
public static void main(String[] args) {
System.out.println("Number of arguments: " + args.length);
for (int i = 0; i < args.length; i++) {
System.out.println("args[" + i + "] = " + args[i]);
}
}
}
java Sample dog cat
ප්රතිඵලය:
Number of arguments: 2
args[0] = dog
args[1] = cat
මෙහි වැදගත් කරුණ වන්නේ සියලු තර්ක 문자열 ලෙස ලබා ගන්නා බවයි.
ඔබට ඒවා සංඛ්යා හෝ බූලියන් (boolean) අගයන් ලෙස භාවිතා කිරීමට අවශ්ය නම්, පසුව ඔබ විසින් පැහැදිලිව පරිවර්තනය කළ යුතුය.
ඔබට කවදා කමාන්ඩ්-ලයින් තර්ක (Command-Line Arguments) භාවිතා කළ යුතුද?
කමාන්ඩ්-ලයින් තර්ක සාමාන්යයෙන් පහත සඳහන් අවස්ථා වල භාවිතා වේ:
- කාර්ය මෝඩ් (operation modes) මාරු කිරීම උදාහරණය:
java Server startසහjava Server stopවෙනස් ක්රියාකාරකම් සිදු කිරීම සඳහා. - ගොනු මාර්ග හෝ වින්යාස අගයන් නියම කිරීම උදාහරණය:
java ReportGenerator /data/input.csv - ස්ක්රිප්ට් ධාවනයේදී තාවකාලික පරාමිතීන් පවරා ගැනීම උදාහරණය: දිනයන්, පරිශීලක නාම, හෝ පරිසර විචල්ය සඳහා ලාංඡන-බර (lightweight) ප්රතිස්ථාපක ලෙස.
මෙම භාවිතා අවස්ථා සෘජුතාවය වැඩි කරයි සහ වැඩසටහන් සංයුජිත නැවත සංග්රහ නොකළ තත්ත්වයේ පිටත ආදානයෙන් පාලනය කළ හැකි බවට පත් කරයි.
තර්ක කිසිවක් ලබා නොදෙන විට හැසිරීම
Java හි main ක්රමය සෑම විටම String[] args අඩංගු කරයි, තර්ක කිසිවක් ලබා නොදෙන විටත්.
වැඩසටහන තර්ක නොමැතිව ක්රියාත්මක කරන්නේ නම්, args.length == 0 වේ, එබැවින් පහත පරිදි කොන්දේසි පරීක්ෂාවක් (conditional check) එකතු කිරීම සුදුසු වේ:
if (args.length == 0) {
System.out.println("No arguments were specified. Exiting.");
return;
}
මෙය කමාන්ඩ්-ලයින් තර්කයේ ප්රධාන ලක්ෂණය පෙන්වයි: ආරම්භයේදී වැඩසටහන්ට සෘජු, පිටත ආදානයක් ලබා ගැනීමට ඉඩ සලසයි.
සාරාංශය
String[] argsවැඩසටහන ක්රියාත්මක වන විට ලබා දෙන තර්ක ලැයිස්තුව ගබඩා කරයි.- සියලු තර්ක strings ලෙස සැකසෙයි.
- තර්ක කිසිවක් ලබා නොදෙන විටත්,
argsnullනොව, හිස් අරේ (empty array) වේ. - තර්ක භාවිතා කිරීම වැඩසටහන් සෘජු, නැවත භාවිතා කළ හැකි, සහ ස්වයංක්රීය කිරීම පහසු කරයි.
ඊළඟ පරිච්ඡේදයේ, අපි මෙම පදනම මත ඉදිරියට ගොස්, args වෙතින් අගයන් ලබා ගැනීම සහ භාවිතා කිරීම සඳහා මූලික භාවිතා රටා සහ නිරූපිත කේත උදාහරණ පෙන්වන්නෙමු.
3. මූලික භාවිතය සහ කේත උදාහරණ
කමාන්ඩ්-ලයින් තර්ක වල මූලික වාක්ය රීතිය
කමාන්ඩ්-ලයින් තර්ක භාවිතා කිරීමට, ඔබ ප්රථමයෙන් main ක්රමයට ලබා දෙන String[] args වෙතින් අගයන් ලබා ගත යුතුය.
args අරේ (array) වන බැවින්, ඔබට එහි එක් එක් අංගය එහි අංකය (index) අනුව ප්රවේශ විය හැක.
public class CommandExample {
public static void main(String[] args) {
System.out.println("Number of arguments: " + args.length);
if (args.length > 0) {
System.out.println("First argument: " + args[0]);
} else {
System.out.println("No arguments were specified.");
}
}
}
වැඩසටහන පහත පරිදි ධාවනය කරන්න:
javac CommandExample.java
java CommandExample hello
ප්රතිඵලය:
Number of arguments: 1
First argument: hello
ඉහත පෙන්වා ඇති පරිදි, args[0] පළමු ලබා දෙන අගය ගබඩා කරයි.
තර්ක බහුලව ලබා දුන් විට, ඒවා args[1], args[2] ආදී ලෙස ප්රවේශ විය හැක.
එක්වරම සියලු පරාමිතීන් සැකසීම
When the number of arguments is variable, it is common to process them using a loop.
පරාමිතීන් ගණන වෙනස් විය හැකි විට, ඒවා ලූප් එකක් භාවිතා කර සැකසීම සාමාන්යය වේ.
The following example prints all received arguments sequentially.
පහත උදාහරණය ලැබුණු සියලු පරාමිතීන් අනුක්රමයෙන් මුද්රණය කරයි.
public class PrintArgs {
public static void main(String[] args) {
if (args.length == 0) {
System.out.println("No arguments were provided.");
System.out.println("Example: java PrintArgs apple orange banana");
return;
}
System.out.println("Argument list:");
for (int i = 0; i < args.length; i++) {
System.out.printf("args[%d] = %s%n", i, args[i]);
}
}
}
Execution:
ක්රියාත්මක කිරීම:
java PrintArgs apple orange banana
Output:
ප්රතිඵලය:
Argument list:
args[0] = apple
args[1] = orange
args[2] = banana
Using a loop allows your program to handle any number of arguments.
ලූප් එකක් භාවිතා කිරීම ඔබේ වැඩසටහනට ඕනෑම ගණනක පරාමිතීන් හසුරවීමට ඉඩ සලසයි.
As a best practice, always check the array length before accessing elements.
හොඳ පුරුදු ලෙස, අංග වලට ප්රවේශ වීමට පෙර සදා පරිමාණයේ දිග පරීක්ෂා කරන්න.
පරාමිතීන් අනුපිළිවෙල අනුව අර්ථය නියම කිරීම
You can assign specific meanings to arguments based on their order.
ඔබට පරාමිතීන්ගේ අනුපිළිවෙල අනුව විශේෂ අර්ථයන් නියම කළ හැක.
For example, consider a program that accepts a file name, a mode, and an overwrite flag.
උදාහරණයක් ලෙස, ගොනු නාමයක්, ක්රමයක්, සහ අතිලේඛන ධජයක් (overwrite flag) පිළිගන්නා වැඩසටහනක් ගැන සිතන්න.
public class FileProcessor {
public static void main(String[] args) {
if (args.length < 3) {
System.out.println("Usage: java FileProcessor <file> <mode> <overwrite>");
System.out.println("Example: java FileProcessor data.txt verbose true");
return;
}
String fileName = args[0];
String mode = args[1];
boolean overwrite = Boolean.parseBoolean(args[2]);
System.out.println("File name: " + fileName);
System.out.println("Mode: " + mode);
System.out.println("Overwrite enabled: " + overwrite);
}
}
Execution example:
ක්රියාත්මක කිරීමේ උදාහරණය:
java FileProcessor data.txt simple false
Output:
ප්රතිඵලය:
File name: data.txt
Mode: simple
Overwrite enabled: false
By assigning roles to argument positions, you can achieve flexible program control.
පරාමිතීන්ගේ ස්ථානවලට භූමිකා නියම කිරීමෙන්, ඔබට ලවච්චි වැඩසටහන් පාලනයක් ලබා ගත හැක.
උදාහරණය: පරාමිතීන් සංඛ්යා ලෙස සැලැස්වීම
Since all arguments are passed as strings, type conversion is required when treating them as numbers.
සියලු පරාමිතීන් 문자열 (string) ලෙස ලබා දෙන බැවින්, ඒවා සංඛ්යා ලෙස සැලැස්වීමට වර්ග පරිවර්තනය අවශ්ය වේ.
The following program receives two integers and prints their sum.
පහත වැඩසටහන පූර්ණ සංඛ්යා දෙකක් ලබාගෙන ඒවාගේ එකතුව මුද්රණය කරයි.
public class SumArgs {
public static void main(String[] args) {
if (args.length < 2) {
System.out.println("Please specify two integers.");
return;
}
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
int sum = a + b;
System.out.println("Sum: " + sum);
}
}
Execution:
ක්රියාත්මක කිරීම:
java SumArgs 7 13
Output:
ප්රතිඵලය:
Sum: 20
If a non-numeric value such as "abc" is passed, a NumberFormatException will occur.
"abc" වැනි සංඛ්යා නොවන අගයක් ලබා දුන් විට, NumberFormatException එකක් සිදුවේ.
To make your program more robust, you should add exception handling.
ඔබේ වැඩසටහන වඩා ශක්තිමත් කිරීම සඳහා, ඔබට විශේෂිත දෝෂ හසුරවීම (exception handling) එකතු කළ යුතුය.
දෝෂ හසුරවීම සමඟ ආරක්ෂිත ක්රියාත්මක කිරීම
public class SafeSum {
public static void main(String[] args) {
try {
if (args.length < 2) {
throw new IllegalArgumentException("Insufficient arguments. Please specify two integers.");
}
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
System.out.println("Sum: " + (a + b));
} catch (NumberFormatException e) {
System.out.println("One or more arguments cannot be interpreted as numbers.");
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
}
}
With proper exception handling, the program can return clear messages instead of terminating abruptly when invalid arguments are provided.
සුදුසු දෝෂ හසුරවීම සමඟ, වැරදි පරාමිතීන් ලබා දෙන විට වැඩසටහන හදිසිව නවත්වීමේ වෙනුවට පැහැදිලි පණිවුඩ ලබා දිය හැක.
සාරාංශය
- කමාන්ඩ්-ලයින් ලක්ෂණ
argsarray තුළ string වලින් ලබා දෙනු ලැබේ. - elements වලට ප්රවේශ වීමට පෙර සැමවිටම
args.lengthපරීක්ෂා කරන්න. - විචල්ය සංඛ්යාවක ලක්ෂණ කිහිපයක් කළමනාකරණය සඳහා loops භාවිතා කරන්න.
Integer.parseInt()වැනි methods භාවිතා කරමින් සංඛ්යාත්මක වටිනාකම් පරිවර්තනය කරන්න.- පරිශීලක-හිතකාමී හැසිරීම සඳහා exception handling ක්රියාත්මක කරන්න.
ඊළඟ පරිච්ඡේදයේදී, අපි සාමාන්ය වැරදි සහ වැදගත් සැලකිලිමත් කිරීම් සමාලෝචනය කරමු, සහ ආරක්ෂිත සහ නැවත භාවිතා කළ හැකි කේතය ලිවීමට ක්රම පැහැදිලි කරමු.
4. සාමාන්ය වැරදි සහ සැලකිලිමත් කිරීම්
කමාන්ඩ්-ලයින් ලක්ෂණ සරල යාන්ත්රණයක් වුවද, ආරම්භකයින් සාමාන්යයෙන් මුහුණ දෙන ගැටලු කිහිපයක් තිබේ.
මෙම පරිච්ඡේදය සාමාන්ය වැරදි සහ ඒවා වැළැක්වීම සඳහා ප්රායෝගික උපාය මාර්ග හඳුන්වා දෙයි.
Array Index වැරදි (ArrayIndexOutOfBoundsException)
වඩාත් සුලබ වැරදිය වන්නේ පවතිනා නොවන index එකකට ප්රවේශ වීමයි.
args array එකක් නිසා, පරාසයෙන් පිටත index එකක් සඳහන් කිරීමෙන් පහත දැක්වෙන exception එකක් ඇති වේ.
උදාහරණය: වැරදි කේතය
public class ErrorExample {
public static void main(String[] args) {
System.out.println(args[0]); // Error occurs if no arguments are provided
}
}
ක්රියාත්මක කිරීම:
java ErrorExample
අඩක්කු:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
උපාය මාර්ගය
array elements වලට ප්රවේශ වීමට පෙර සැමවිටම args.length පරීක්ෂා කරන්න.
if (args.length == 0) {
System.out.println("No arguments were specified.");
return;
}
System.out.println("First argument: " + args[0]);
කෙලින්ම වැදගත් කරුණ:
args කවදාවත් null නොවේ.
ලක්ෂණ ලබා නොදුන් විට පවා, එය length 0 ඇති array එකක් ලෙස පවතී.
සියලුම ලක්ෂණ Strings වේ
කමාන්ඩ්-ලයින් ලක්ෂණ සැමවිටම strings ලෙස ලබා ගනී.
එබැවින්, සංඛ්යාත්මක ගණනය කිරීම් සඳහා type පරිවර්තනය අවශ්ය වේ.
int num = Integer.parseInt(args[0]);
"abc" වැනි non-numeric string එකක් ලබා දුන්නහොත්, NumberFormatException එකක් ඇති වේ.
උපාය මාර්ගය: Exception Handling එකතු කිරීම
try {
int num = Integer.parseInt(args[0]);
System.out.println("Input value: " + num);
} catch (NumberFormatException e) {
System.out.println("The argument cannot be interpreted as a number. Please provide a valid value.");
}
කෙලින්ම වැදගත් කරුණ:
ලක්ෂණ ඍජුවම සංඛ්යා ලෙස භාවිතා කිරීම අවදානමකි.
පරිශීලක-ලබා දුන් ආදානය නිවැරදි නොවිය හැකි බව සැමවිටම උපකල්පනය කරන්න.
Spaces අඩංගු ලක්ෂණ
කමාන්ඩ් ලයිනයේදී, spaces ලක්ෂණ වෙන්කරු ලෙස සැලකේ.
spaces අඩංගු string එකක් ලබා දීමට, එය double quotation marks වලින් ආවරණය කළ යුතුය.
උදාහරණය:
java Message "Hello World"
ප්රතිඵලය:
args[0] = Hello World
quotation marks නොමැතිව වැඩසටහන ක්රියාත්මක කළහොත්, "Hello" සහ "World" වෙනම ලක්ෂණ ලෙස සැලකේ.
Multibyte අක්ෂර කළමනාකරණය
non-ASCII text වැනි multibyte අක්ෂර ලබා දීමේදී, character encoding ගැටලු ඇති විය හැක.
විශේෂයෙන්ම Windows පද්ධතිවලදී, console encoding බොහෝ විට MS932 (Shift_JIS) වන අතර, එය UTF-8–අඩධාරය Java වැඩසටහන් සමඟ ගැටලුවක් ඇති කළ හැක.
උපාය මාර්ග:
- ක්රියාත්මක කිරීමට පෙර command prompt එක UTF-8 සකසන්න (
chcp 65001). - IDE run configurations (Eclipse / IntelliJ IDEA) වල encoding එක UTF-8 ට ඒකාබද්ධ කරන්න.
- Java startup option
-Dfile.encoding=UTF-8සඳහන් කරන්න.java -Dfile.encoding=UTF-8 Sample Hello
ඕනෑවට වඩා ලක්ෂණ හෝ දිගු ලක්ෂණ
ඕපරේටිං සිස්ටම් අනුව, කමාන්ඩ්-ලයින් ලක්ෂණවල මුළු දිග සීමාවක් තිබේ.
Windows හි සීමාව ආසන්න වශයෙන් 8,000 අක්ෂර වන අතර, Linux හි එය 128 KB පමණ වේ.
විශාල දත්ත ප්රමාණයක් කළමනාකරණය කිරීමට අවශ්ය නම්, කමාන්ඩ්-ලයින් ලක්ෂණ වෙනුවට file input හෝ standard input භාවිතා කරන්න.
විශේෂ අක්ෂර සහ Escaping
shells සහ command prompts වලදී, <, >, සහ & වැනි නිශ්චිත අක්ෂරවල විශේෂ අර්ථයක් තිබේ.
ඒවා literal ලක්ෂණ ලෙස ලබා දීමට, quotation marks වලින් ආවරණය කරන්න හෝ escape කරන්න.
උදාහරණය:
java SymbolTest "<tag>" "&value"
මෙය shell විසින් මෙම අක්ෂර redirection හෝ pipe operators ලෙස වැරදි විග්රහ කිරීම වැළැක්වීමයි。
වැරදි තර්කානුක්රමය
තර්කානුක්රමය වැදගත් වන විට, පරිශීලකයින් අනපේක්ෂිතව වැරදි අනුපිළිවෙලකින් වටිනාකම් දක්වා තිබිය හැකියි.
මෙය විශේෂයෙන්ම input සහ output ගොනු නම් වැනි යුගල සමඟ සුලබයි.
එයට එරෙහිව ගන්නා බලපෑම්:
- නම්කළ විකල්ප භාවිතා කරන්න, උදාහරණයක් ලෙස
--input=input.txt(පසුකාලීන පරිච්ඡේදවල සාකච්ඡා කර ඇත). helpවිකල්පයක් සපයන්න එය භාවිතය පැහැදිලිව පැහැදිලි කරයි.
සාරාංශය
| Error Type | Cause | Countermeasure |
|---|---|---|
| Out-of-range access | Accessing arguments without checking count | Validate with args.length |
| Number conversion error | Direct conversion of invalid strings | Handle with try-catch |
| Incorrect splitting | No quotation marks for spaced values | Use double quotation marks |
| Encoding issues | Character set mismatch | Unify to UTF-8 |
| Special character errors | Shell interpretation | Quote or escape characters |
| Incorrect order | User input mistakes | Named options or help output |
ඊළඟ පරිච්ඡේදයේදී, අපි ප්රායෝගික නිර්මාණ රටා හඳුන්වා දෙමු type conversion, option formats, සහ default values භාවිතයෙන් වඩාත් ශක්තිමත් command-line මෙවලම් ගොඩනැගීම සඳහා.
5. ප්රායෝගික භාවිතය: Type Conversion, Option Arguments, සහ Default Values
මෙම පරිච්ඡේදයේදී, අපි command-line arguments හැසිරවීම සඳහා වඩාත් නම්යශීලී සහ production-ready ආකාරයකින් තාක්ෂණයන් ගවේෂණය කරමු.
type conversion, option-style arguments, සහ default value design යෙදීමෙන්, ඔබට වඩා සුරක්ෂිත සහ පරිශීලකයින්ට භාවිතා කිරීමට පහසු වැඩසටහන් ගොඩනැගිය හැකියි.
Arguments Numeric සහ Boolean Types වලට පරිවර්තනය කිරීම
සියලුම command-line arguments String වටිනාකම් ලෙස ලබා දෙනු ලැබුවත්, arithmetic operations සහ conditional logic සඳහා සුදුසු දත්ත වර්ග අවශ්ය වේ.
Numbers වලට පරිවර්තනය කිරීම
int count = Integer.parseInt(args[0]);
double rate = Double.parseDouble(args[1]);
Boolean වලට පරිවර්තනය කිරීම
boolean debugMode = Boolean.parseBoolean(args[2]);
උදාහරණය: Numeric Calculation Program
public class Multiply {
public static void main(String[] args) {
if (args.length < 2) {
System.out.println("Usage: java Multiply <number1> <number2>");
return;
}
try {
double a = Double.parseDouble(args[0]);
double b = Double.parseDouble(args[1]);
System.out.println("Result: " + (a * b));
} catch (NumberFormatException e) {
System.out.println("One or more arguments are not numeric.");
}
}
}
ධාවනය:
java Multiply 4.5 3
අඩක්ක:
Result: 13.5
ප්රධාන කරුණු:
- අනවසර input හැසිරවීම සඳහා conversions සැමවිටම
try-catchblocks වල ආවරණය කරන්න. Boolean.parseBoolean()වටිනාකම"true"යටතේ පමණක්trueආපසු දෙයි, case නොසලකා.
Named Option Arguments නිර්මාණය කිරීම
Argument order මත රඳා පවතින නිර්මාණ human error වලට ගොදුරු වේ.
--key=value වැනි named options හෝ -v වැනි flags භාවිතයෙන් වැඩසටහන් වඩාත් සංකේතාත්මක වේ.
උදාහරණය: Named Options Parsing
public class OptionParser {
public static void main(String[] args) {
boolean verbose = false;
String mode = "normal";
String file = "default.txt";
for (String arg : args) {
if (arg.equals("-v") || arg.equals("--verbose")) {
verbose = true;
} else if (arg.startsWith("--mode=")) {
mode = arg.substring("--mode=".length());
} else if (arg.startsWith("--file=")) {
file = arg.substring("--file=".length());
}
}
System.out.println("Mode: " + mode);
System.out.println("File: " + file);
System.out.println("Verbose output: " + verbose);
}
}
ධාවනය:
java OptionParser --mode=debug --file=log.txt -v
අඩක්ක:
Mode: debug
File: log.txt
Verbose output: true
ප්රධාන කරුණු:
startsWith()--key=valueformats හඳුනා ගැනීම පහසු කරයි.- Arguments ඕනෑම අනුපිළිවෙලකින් දක්වා තිබිය හැකියි.
- Shell scripts සහ automated jobs සඳහා හොඳින් ගැලපේ.
Default Values සැපයීම
Arguments අනුපාසක කළහොත් නිරාපදා සුරක්ෂිත default values නිර්වචනය කිරීම වැදගත් වේ.
මෙය වැඩසටහන අවම input සමඟ ධාවනය වීමට ඉඩ සලසයි සහ අනපේක්ෂිත අවසන්වීම වැළැක්වේ.
උදාහරණය: Default Values සහිත Program
public class Greeting {
public static void main(String[] args) {
String name = "Guest";
String lang = "ja";
if (args.length > 0) name = args[0];
if (args.length > 1) lang = args[1];
if (lang.equals("en")) {
System.out.println("Hello, " + name + "!");
} else if (lang.equals("fr")) {
System.out.println("Bonjour, " + name + "!");
} else {
System.out.println("Hello (default), " + name + "!");
}
}
}
Execution:
java Greeting Taro en
Output:
Hello, Taro!
Execution without arguments:
java Greeting
Output:
Hello (default), Guest!
Key points:
- Safely handles missing arguments.
- Behavior adapts based on the number of provided arguments.
Generalizing Option Parsing
For more complex tools, creating a dedicated option parser improves maintainability compared to manually processing args.
Example: Simple Option Parser
import java.util.HashMap;
public class SimpleParser {
public static void main(String[] args) {
HashMap<String, String> options = new HashMap<>();
for (String arg : args) {
if (arg.startsWith("--") && arg.contains("=")) {
String[] pair = arg.substring(2).split("=", 2);
options.put(pair[0], pair[1]);
}
}
System.out.println("Parsed options:");
for (String key : options.keySet()) {
System.out.println(key + " = " + options.get(key));
}
}
}
Execution:
java SimpleParser --user=admin --port=8080 --mode=test
Output:
Parsed options:
user = admin
port = 8080
mode = test
Key points:
HashMapallows flexible storage of key-value pairs.- The parser can serve as a reusable foundation for CLI tools.
Practical Design Patterns
- Few arguments : positional arguments are sufficient.
- Many configuration values : use named options (
--key=value). - Many optional values : define default values.
- System integration and scripting : adopt consistent option conventions (e.g.,
--help,--config).
Summary
| Feature | Approach | Benefit |
|---|---|---|
| Numeric/boolean conversion | parseInt, parseDouble, parseBoolean | Enables calculations and conditions |
| Named arguments | --key=value format | Order-independent and flexible |
| Default values | Variable initialization and branching | Safe and user-friendly |
| Generic parser | Store in HashMap | Easy to extend and maintain |
In the next chapter, we introduce real-world usage patterns, including file operations, batch processing, and mode switching commonly used in production environments.
6. Applied Examples: Common Real-World Patterns
This chapter demonstrates how command-line arguments are used in real-world scenarios.
By flexibly specifying arguments, you can build highly reusable tools for file operations, mode switching, and logging control.
Processing Files Specified by Command-Line Arguments
The most common use case is receiving a target file as a command-line argument.
This pattern is especially useful for automating file read/write operations.
Example: Reading and Displaying File Contents
import java.nio.file.*;
import java.io.IOException;
public class FileReaderTool {
public static void main(String[] args) {
if (args.length == 0) {
System.out.println("Usage: java FileReaderTool <filePath>");
return;
}
String filePath = args[0];
try {
String content = Files.readString(Paths.get(filePath));
System.out.println("=== Contents of " + filePath + " ===");
System.out.println(content);
} catch (IOException e) {
System.out.println("Failed to read file: " + e.getMessage());
}
}
}
Execution example:
java FileReaderTool data.txt
Sample output:
=== Contents of data.txt ===
Sample data line 1
Sample data line 2
Key points:
- Always include file existence checks and exception handling.
- Easy to extend for batch processing of multiple files.
Switching Program Behavior by Mode
Using arguments to switch behavior allows a single program to serve multiple roles.
Example: Mode-Based Execution
public class ModeSelector {
public static void main(String[] args) {
if (args.length == 0) {
System.out.println("Usage: java ModeSelector <mode>");
System.out.println("Available modes: test / prod / debug");
return;
}
String mode = args[0].toLowerCase();
switch (mode) {
case "test":
System.out.println("Running in test mode...");
break;
case "prod":
System.out.println("Running in production mode...");
break;
case "debug":
System.out.println("Running in debug mode with detailed logging...");
break;
default:
System.out.println("Unknown mode: " + mode);
}
}
}
ක්රියාත්මක කිරීම:
java ModeSelector debug
ප්රතිඵලය:
Running in debug mode with detailed logging...
ප්රධාන කරුණු:
- සංවර්ධන, පරීක්ෂණ, සහ නිෂ්පාදන පරිසර අතර මාරු කිරීම සඳහා ප්රයෝජනවත්.
- ස්වයංක්රීය ස්ක්රිප්ට් සහ බැච් රැකියා වල පුළුල් ලෙස භාවිතා වේ.
බහු පරාමිතීන් සමඟ ගණනය කිරීම ස්වයංක්රීය කිරීම
බැච් සැකසීම සහ කාලසටහන්ගත ස්ක්රිප්ට් වල, පරාමිතීන් සාමාන්යයෙන් පරාමිතීන් ධාරිතාවයෙන් ලබා දීමට භාවිතා වේ.
පහත උදාහරණය දෙකක් සංඛ්යාත්මක අගයන් මත ක්රියා කරන සරල කැල්කියුලේටරයක් පෙන්වයි.
public class Calculator {
public static void main(String[] args) {
if (args.length < 3) {
System.out.println("Usage: java Calculator <num1> <num2> <op>");
System.out.println("Example: java Calculator 10 5 add");
return;
}
double a = Double.parseDouble(args[0]);
double b = Double.parseDouble(args[1]);
String op = args[2];
switch (op) {
case "add": System.out.println(a + b); break;
case "sub": System.out.println(a - b); break;
case "mul": System.out.println(a * b); break;
case "div":
if (b == 0) {
System.out.println("Division by zero is not allowed.");
} else {
System.out.println(a / b);
}
break;
default:
System.out.println("Unknown operation.");
}
}
}
ක්රියාත්මක කිරීම:
java Calculator 8 2 mul
ප්රතිඵලය:
16.0
මෙම ක්රමය ඔබට කුඩා, ස්ක්රිප්ට්-හිතකාමී මෙවලම් නිර්මාණය කිරීමට ඉඩ සලසයි, ඒවා ස්වයංක්රීය වැඩපිළිවෙළ සමඟ පහසුවෙන් ඒකාබද්ධ වේ.
විකල්ප පරාමිතීන් සමඟ වින්යාසය නිරූපණය කිරීම
වැඩි ලවච්චිත ක්රියාකාරිත්වයක් සඳහා, --key=value වැනි විකල්ප-ශෛලී පරාමිතීන් ඉතා ප්රයෝජනවත් වේ.
public class ConfigLoader {
public static void main(String[] args) {
String config = "default.conf";
boolean verbose = false;
for (String arg : args) {
if (arg.startsWith("--config=")) {
config = arg.substring("--config=".length());
} else if (arg.equals("--verbose")) {
verbose = true;
}
}
System.out.println("Configuration file: " + config);
System.out.println("Verbose logging: " + (verbose ? "ON" : "OFF"));
}
}
ක්රියාත්මක කිරීම:
java ConfigLoader --config=prod.conf --verbose
ප්රතිඵලය:
Configuration file: prod.conf
Verbose logging: ON
ප්රධාන කරුණු:
- නාමික විකල්පයන් මගින් මනුෂ්ය දෝෂය අඩු වේ, මන්ද අනුපිළිවෙල වැදගත් නොවේ.
- වින්යාස මාර්ග සහ ක්රියාත්මක මාදිලි සඳහා සාමාන්යයෙන් භාවිතා වේ.
ප්රායෝගික උදාහරණය: ලොග් කිරීම සමඟ ගොනු සැකසීමේ මෙවලම
import java.nio.file.*;
import java.io.*;
public class FileCopyTool {
public static void main(String[] args) {
if (args.length < 2) {
System.out.println("Usage: java FileCopyTool <input> <output> [--verbose]");
return;
}
String input = args[0];
String output = args[1];
boolean verbose = (args.length > 2 && args[2].equals("--verbose"));
try {
Files.copy(Paths.get(input), Paths.get(output), StandardCopyOption.REPLACE_EXISTING);
if (verbose) {
System.out.println("File copied successfully: " + input + " → " + output);
}
} catch (IOException e) {
System.out.println("Copy failed: " + e.getMessage());
}
}
}
Execution:
java FileCopyTool report.txt backup.txt --verbose
Output:
File copied successfully: report.txt → backup.txt
Key points:
- Logging can be toggled via options for development or production use.
- This structure is reusable as a foundation for real-world scripts.
Summary
| Use Case | Typical Argument Pattern | Scenario |
|---|---|---|
| File specification | <file> | Input/output processing, automated backups |
| Mode switching | <mode> or --mode=debug | Environment-specific execution |
| Configuration selection | --config=xxx.conf | System configuration and runtime parameters |
| Option control | --verbose, --dry-run | Logging and safe test runs |
| Batch processing | <startDate> <endDate> | Scheduled jobs and data aggregation |
In the next chapter, we cover how to pass command-line arguments in IDEs and testing environments, including Eclipse and IntelliJ IDEA, along with debugging tips.
7. Testing and Debugging Tips / Configuring Arguments in IDEs
Programs that use command-line arguments can be executed and tested not only from the terminal, but also within development environments such as Eclipse and IntelliJ IDEA.
This chapter explains how to configure arguments during development and introduces tips for efficient debugging.
Setting Arguments in Eclipse
In Eclipse, you can configure command-line arguments using Run Configurations.
This allows you to run programs with predefined arguments without manually typing commands each time.
Steps:
- From the menu bar, select Run → Run Configurations… .
- From the list on the left, choose the target Java Application.
- Open the Arguments tab.
- Enter arguments in the Program arguments field, separated by spaces. Example:
data.txt debug true - Click Apply , then Run .
The same arguments will be reused the next time you run the program.
You can also save multiple configurations to switch between different test scenarios.
Tips:
- Arguments may be written on separate lines; Eclipse treats them as space-separated values.
- Strings containing spaces must be enclosed in double quotation marks (e.g.,
"Hello World"). - To change character encoding, specify
-Dfile.encoding=UTF-8in the VM arguments field.
Setting Arguments in IntelliJ IDEA
IntelliJ IDEA provides an even more streamlined configuration process.
Steps:
- Open Run → Edit Configurations… .
- Select the configuration for the target class.
- Enter arguments in the Program arguments field. Example:
--mode=debug --file=log.txt --verbose - Click OK or Apply , then run the program.
Tips:
- Run configurations are saved per project.
- The same configuration is used for both Run and Debug executions.
- You can define environment variables alongside arguments to closely simulate production environments.
Understanding Differences from Command-Line Execution
There may be differences between running a program in an IDE and running it directly from the terminal, particularly regarding environment variables and working directories.
| Aspect | IDE Execution | Terminal Execution |
|---|---|---|
| Working directory | Project root (configurable) | Current shell directory |
| Environment variables | Defined per run configuration | Inherited from the shell |
| Encoding | IDE default or configured | Depends on OS and console |
Being aware of these differences helps prevent issues that only occur after deployment.
Debugging Tips for Argument-Based Programs
- Print all received arguments at startup to verify correct input.
- Log parsed values after validation and type conversion.
- Use breakpoints at argument parsing logic to inspect runtime values.
- Test edge cases such as missing arguments, invalid values, and empty strings.
Summary
- Command-line arguments can be tested efficiently within IDEs.
- Eclipse and IntelliJ IDEA provide built-in support for argument configuration.
- Be mindful of differences between IDE and terminal environments.
- Effective debugging starts with validating and logging argument values.
මෙම තාක්ෂණයන් සමඟ, ඔබට විශ්වාසයෙන් Java වැඩසටහන් සංවර්ධනය, පරීක්ෂා කිරීම සහ දෝෂ නිරාකරණය කළ හැක, ඒවා කමාන්ඩ්-ලයින් පරාමිතීන් මත පදනම්ව ඇති අතර, සංවර්ධන සහ නිෂ්පාදන පරිසරයන් තුළ සමාන්ය හැසිරීමක් සහතික කරයි.

