0

I'm trying to write a java program which takes in input from the command line but for some reason I keep getting this message;

public class Class {
  public static void main(String[] args) {
    String s = args[0];
  }
}

Literally trying to just run the above program for now, and once I can get the input working I can build upon the program to complete my h/w assignment.

I keep getting this message when trying to compile in terminal:

error: Class names, 'test', are only accepted if annotation processing is explicitly requested

Also, this is what I'm writing in terminal javac Filename.java test

sometimes I also use "" so I write javac Filename.java "test"

However, I get the same error. I've found other questions who get the same error but the answer seems to be to add a .java when compiling - but I'm already doing this?

5
  • Why do you write test after the filename? Commented Feb 8, 2021 at 18:55
  • 1
    read the manual page of your javac Commented Feb 8, 2021 at 18:56
  • 5
    javac Class.java to compile the file, then java Class "test" to run it. Commented Feb 8, 2021 at 18:57
  • 3
    And don’t name your class Class, it’s just a bad idea. Commented Feb 8, 2021 at 19:07
  • (The reason that it is a bad idea is that there is already a class called Class that is imported by default. What you have written should work in this case ... but it is liable to lead to confusion in more complicated code. Besides, calling a class Class is a bit like naming your cat "Cat". ) Commented Feb 8, 2021 at 23:38

1 Answer 1

2

You add the word "test" in the compilation command javac. Try to put javac Filename.java without args and with them when you run your programmjava Filename command.

Sign up to request clarification or add additional context in comments.

5 Comments

i need to use args in the assignment
@computerprogram Yes, thats what this answer is saying: You need to compile your .java file first using javac (java compiler), then you can run it with your arguments with java Filename.class arg1 arg2. You can't compile and run a .java file with a single command.
@thetechnician94 technically from Java 11 you can do something like java Filename.java and it will compile in the background and then run it. Anyway java Filename.class is just plain wrong. It should be java Filename.
@FedericoklezCulloca interesting that you can do that in Java 11. Unfortunately it only works with single file programs, which may not be OP's case, nor do we know what version of Java OP is using. Further, in the interest of learning, I wouldn't recommend it since the compile and run mantra will be useful to understand how it actually works. As far as the ".class" thing goes, you're 100% right. No idea why I did that.
okay thank you - I've now done this but I'm getting a different error - its the java.lang.noclassdeffound error - i've had a google and it seems to do with the classpath which i'm not too entirely sure how to set and check if anyone could link me to useful resource or impart any words of wisdom then thank you!!!! - thanks for answering this original question anyway was really helpful :)

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.