1

I am trying to run the following method in Loader.java from the command line:

public static void method(String path, String word)

As seen above, I must pass in the variables path and word, and I want the command line to display the System.out.println()'s in the method.

What command can I run to do this?

Note: when I run the following commands,

javac *.java
jar -cvf Loader.jar Loader.class
java -cp ./Loader.jar Loader

I get the following error:

Caused by: java.lang.NoClassDefFoundError: path/to/Loader (wrong name: Loader)

What must I do to successfully run method from the command line?

Here is minimum reproducible version of Loader.java:

public class Loader {
     public static void main(String[] args) {
          method("my/path", "my_word");
     }
     public static void method(String path, String word) {
          System.out.println("Output after doing something");
     }
}
5
  • Post the complete code of Loader.java. Commented Jun 15, 2020 at 21:01
  • @user yes as seen above Commented Jun 15, 2020 at 21:04
  • @ArvindKumarAvinash I have edited the question to include that. Commented Jun 15, 2020 at 21:05
  • Satya Vejus - Any update? Commented Jun 15, 2020 at 21:29
  • As I mentioned in the question, when I run the code you are suggesting, I get Caused by: java.lang.NoClassDefFoundError: path/to/Loader (wrong name: Loader). Also I still don't know how to pass parameters from command line. Commented Jun 15, 2020 at 21:45

1 Answer 1

1

Just do the following:

javac Loader.java
java Loader

In fact, if you are you Java-11 or above, you don't even need to use the first command i.e. you can directly use the following command:

java Loader.java

However, if you want to create a jar file and execute the class from it, execute the steps given below:

mkdir demo
cd demo

Now create/place Loader.java in this folder. Then,

javac *.java
jar -cvf loader.jar .
java -cp loader.jar Loader

Note that I've used a new directory, demo to make it clear but it is not necessary. Another thing you should notice is the . at the end of jar command which specifies the current directory.

How to process command-line arguments?

String[] args parameter in main stores all the parameters from the command-line e.g. if you run the following program as java Loader my/path my_word from the command-line,

public class Loader {
    public static void main(String[] args) {
        if (args.length >= 2) {
            method(args[0], args[1]);
        } else {
            System.out.println("Command line parameters are missing");
        }
    }

    public static void method(String path, String word) {
        System.out.println("Path: " + path);
        System.out.println("Word: " + word);
    }
}

the output will be

Path: my/path
Word: my_word
Sign up to request clarification or add additional context in comments.

9 Comments

As I mentioned in the question, when I run the code you are suggesting, I get Caused by: java.lang.NoClassDefFoundError: path/to/Loader (wrong name: Loader). Also I still don't know how to pass parameters from command line.
@SatyaVejus - Did you try to create/place Loader.java in a separate folder as I've suggested? I've tested everything I've posted and I don't find any problem.
I am not sure I can place Loader in a separate folder because it requires other classes that I have made. It also processes a different file dict.txt in method. Also in your recent updated answer, what is the command line prompt for passing in String[] args?
The command is java Loader my/path my_word where my/path is the first parameter and will be stored into args[0] and my_word is the second parameter which will be stored into args[1].
What happens when you run jar -cvf loader.jar . after running javac *.java? Do you see loader.jar file after that? If yes, java -cp loader.jar Loader will run successfully. If you want to pass parameters, you can run java -cp loader.jar Loader my/path my_word where my/path and my_word are parameters.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.