Hi,Someone please shade somelight about this-
how arguments are to be passed from a run.bat file
Thanks
Best Regards
Bethuel

Dani AI

Generated

Following up on and : a run.bat simply forwards whatever you type on the command line to the Java process — Java always receives arguments as strings in public static void main(String[] args). Decide whether you want values set as environment variables in the batch (persisting to that cmd session) or passed as command-line parameters (recommended for simple runs).

A minimal, practical batch pattern that forwards whatever was given on the command line:

@echo off
rem usage: run.bat Name "Worker Type" Salary
setlocal
if "%~1"=="" (
  echo Usage: run.bat Name "Worker Type" Salary
  exit /b 1
)
rem forward all arguments exactly as received
java -cp "%MY_CLASSPATH%" com.example.worker.Main %*
endlocal

Notes for the Java side (parse and validate incoming strings):

public static void main(String[] args) {
  if (args.length < 3) { System.err.println("Usage: Name WorkerType Salary"); return; }
  String name = args[0];
  String workerType = args[1];
  // strip any trailing 'd' (user used "22000d") then parse
  String s = args[2].replaceAll("[dD]$", "");
  double salary = Double.parseDouble(s);
  // ... use values
}

Practical tips: wrap arguments that contain spaces in double quotes when invoking the batch; use %* in the batch to forward all arguments without rebuilding them; use setlocal/endlocal to avoid polluting the environment; and do not include Java literal suffixes like d in numeric argument strings unless you strip them before parsing. For many options, consider key=value pairs or a properties file instead of long positional lists — parsing is more robust and easier to maintain.

Recommended Answers

All 3 Replies

Arguments in batch files are passed on the command-line.
Inside the batch, you use %1, %2, %3, etc. to receive each argument.

SET CLASSPATH_JARS=../../lib/log4j/log4j-1.2.14.jar

SET BUILD_CLASS_DIR_IMPLEMENTATION=BUILD_LOCAL\implementation\build\classes

SET RUN_CLASSPATH=%BUILD_CLASS_DIR_IMPLEMENTATION%;%CLASSPATH_JARS%
SET CLASS_TO_RUN=chepsoft.micro.javadoc.workerapplication.WorkerApplication

SET ARGUMENTS=Worker implementation by WorkerImpl

ECHO CLASSPATH_JARS=%CLASSPATH_JARS%

ECHO BUILD_CLASS_DIR_IMPLEMENTATION=%BUILD_CLASS_DIR_IMPLEMENTATION%

ECHO RUN_CLASSPATH=%RUN_CLASSPATH%

ECHO CLASS_TO_RUN=%CLASS_TO_RUN%

java -classpath %RUN_CLASSPATH% %CLASS_TO_RUN% %ARGUMENTS%
%1String name = "Bethuel", %2String workerType = "Monthly Employee", %3double salary = 22000d,
if i want to pass the above variables from run.bat file instead of main() method where should insert them in my code of bat file.

So, in this example, some values are passed by %1 and %2, etc. and some are SET through environment variables.

If this is called Run.bat, you would (at the command-line) type:

Run Bethuel "Monthly Employee" 22000d

...but inside the batch file, the call the program would be
java -classpath %RUN_CLASSPATH% %CLASS_TO_RUN% %1 %2 %3

The %ARGUMENTS% variable is redundant depending on how you want to use them.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.