-2

I have an assignment: Write a client program Permutation.java that takes an integer k as a command-line argument; reads in a sequence of strings from standard input using StdIn.readString()...

Here is my code:

public class Permutation {
    public static void main(String[] args) {
         int k = Integer.parseInt(args[0]);
         String[] inputStrings = StdIn.readStrings();
    }
}

How to compile it in Windows command line?

I tried

javac Permutation.java

But got an error

error: cannot find symbol
    String[] inputStrings = StdIn.readStrings();`

I use IntelliJ IDEA, external library StdIn is added to the project.

Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
dr Anton
  • 125
  • 1
  • 7
  • 1
    Do you have `import` statements in your code? – PM 77-1 Aug 15 '18 at 13:30
  • I don't have an import statement for StdIn class, IntelliJ IDEA doesn't highlight anything that something is import needed. – dr Anton Aug 15 '18 at 13:36
  • Once the jar is added to the classpath, IntelliJ will offer to import the class. – killjoy Aug 15 '18 at 13:39
  • https://stackoverflow.com/questions/1051640/correct-way-to-add-external-jars-lib-jar-to-an-intellij-idea-project – killjoy Aug 15 '18 at 13:40
  • 1
    Maybe the StdIn class is in the default package – michaeak Aug 15 '18 at 13:42
  • killjoy i said i added jar file of the external library to the project. michaeak is right, StdIn class is in the default package and it's not necessary to import. Thanks for minus in reputation – dr Anton Aug 15 '18 at 13:47
  • I actually didn't downvote you. Thanks for wondering. But yes, the answer below will work. – killjoy Aug 15 '18 at 13:58

1 Answers1

3

To compile it with the library you have to add it to the classpath. This is accomplished with the -cp argument followed by the library which contains your referenced class.

javac -cp StdIn.jar Permutation.java

To run it with command line, similarly use

java -cp StdIn.jar Permutation

See also https://docs.oracle.com/javase/8/docs/technotes/tools/windows/javac.html

michaeak
  • 1,548
  • 1
  • 12
  • 23
  • I compiled, but after running the second command, an error occurs "Error: Could not find or load main class Permutation Caused by: java.lang.ClassNotFoundException: Permutation – dr Anton Aug 15 '18 at 14:16
  • Can you try `java -cp StdIn.jar;Permutation.class Permutation`? – michaeak Aug 15 '18 at 14:28