2

Can arguments for a class be passed the following way?

java cs123.Learn -mode train -algorithm even_odd -model_file speech.even_odd.model -data speech.train -task classification

Here, cs123 is the package within which the different java files and their compiled versions are located. I have already compiled the .java files using the following command

javac -cp commons-cli-1.2.jar cs123\*.java

To make things clear, the structure of the .java and .jar files are

lib
|--cs362
|    |--all the java files including Learn.java
|--commons-cli-1.2.jar

I am running the command prompt from the lib folder. What worries me is that from java documentation and other sources the format for passing arguments is simply an array of strings and for options it can be seen from java documentation too. Using the above run time java execution, I get the

java.lang.ClassNotFoundException: org.apache.commons.cli.OptionBuilder

but if I execute,

java -cp commons-cli-1.2.jar cs123.Learn -mode train -algorithm even_odd -model_file speech.even_odd.model -data speech.train -task classification

I get the following error

Unrecognized option: -mode
Error: Could not create the Java Virtual Machine
Error: A fatal exception has occured. Program will exit.

I understand that java tries to associate anything with a - associated with it as a predefined option, -mode not being the one it recognizes. but at the same time the .jar file is there to do it's job. For research purposes, the commons-cli-1.2.jar file is associated with several methods, two of them being commons/cli/Option and commons/cli/OptionBuilder.

I am having to do this because the instruction is to run the program using

java cs123.Learn -mode train -algorithm even_odd -model_file speech.even_odd.model -data speech.train -task classification
CS101
  • 444
  • 1
  • 6
  • 21

2 Answers2

2

I have made a test with commons-cli with the -mode in option. It compile and run as expected. So I can make sure with you that you can using that option. enter image description here I am standing in java folder to run the command, my folder structure:

java
|--upwork
|    |--Main.java
|--commos-cli-1.2.jar

For your problem: I see that your command only point classpath to commons-cli lib and not point to the location of your package. Refer to this answer your command should start with: java -cp .;commons-cli-1.2.jar ...

If this does not help, please upload your code to get help or you can create a simple test like mine to check this out.

TuyenNTA
  • 1,194
  • 1
  • 11
  • 19
0

Can arguments for a class be passed the following way?

I don't think so for the good reason that what you're trying to pass with your "-naming" are program arguments. Program arguments are retrieved in your app with the String[] args of your main and in Java you don't have String index in arrays.

If you want to achieve what you want, you could still do it but you would have to implement the logic in your program. Firstly, you would need to get all the args in your program and then implement the logic by building a map for example and process this map. As an example, you could say that if any argument (i.e args[0]) starts by "-" it means that it's a key for your map and that the following argument (i.e. args[1]) is the associated value.

Edit: I haven't used commons-cli and I may be completely wrong but from a quick research online, I think the library is offering you the logic to build commands to execute in command line once your application is running.

Maaaatt
  • 429
  • 4
  • 14
  • The commons-cli-1.2.jar file is being used for that purpose. It has classes called Option and OptionBuilder to be able to parse the command line in the given format. As a matter of fact, passing arguments in this way is also new to me. I have to run the program this way since the main class has already been implemented. I only had to include some bits and pieces of code to implement a machine learning algorithm. I did not write the code to parse the command line. It was already there. – CS101 Jul 19 '17 at 22:40
  • can you tell me if my method for executing the java command is correct or not? I am talking about the java -cp commons-cli-1.2.jar cs123.Learn -mode train -algorithm even_odd -model_file speech.even_odd.model -data speech.train -task classification In that case, there's probably some other dependencies I need to understand. – CS101 Jul 20 '17 at 03:02
  • You called the required library then your class and your arguments, so it seems to be fine. One assumption is that by using the commons-cli, your program arguments are being interpreted to match your use of the lib in your program: I think there is probably something going one with your code more than with your cli command. – Maaaatt Jul 20 '17 at 09:36