I am trying to do the usual Hello, World , as stated in one of the manuals.

class HelloWorld { public static void main(String args []) { System.out.println("Hello World"); } } 

I run into the translator like this:

 javac C:\Java\HelloWorld.java 

Everything goes well. At least, no crash messages are displayed.

But when I already want to get the result, run like this:

 java HelloWorld 

Then the console displays an error:

Java helloworld

How to run it, what's the problem?

Here is the complete log of what is happening in the console:

C: \ Program Files \ Java \ jdk1.7.0_07 \ bin> javac C: \ Java \ HelloWorld.java

C: \ Program Files \ Java \ jdk1.7.0_07 \ bin> java HelloWorld

C: \ Program Files \ Java \ jdk1.7.0_07 \ bin>

  • Transfer HelloWorld.class to the folder C: \ Program Files \ Java \ jdk1.7.0_07 \ bin - user19574
  • If you remove the package string from the source, everything will work - user200736
  • one
    @ ArthurLomako: Do ​​not teach the bad. But if the program is more difficult - what to do with it? It is necessary to comprehend language on simple programs. - Pavel Mayorov
  • @ ArthurLomako: By the way, the author never wrote that he generally has the package string in the source. - Pavel Mayorov

8 answers 8

Try this:

 java -classpath . HelloWorld 

or so:

 java -cp . HelloWorld 

Otherwise, java cannot find the compiled class.

  • strange, sort of deleted this question. There I actually ran java not from the java directory I created, in which the compiled binary was stored. - Smash

I had a case when even the team

 java -classpath . HelloWorld 

gave the error "Could not find or load main class" decided this way

 java -classpath "jar_name" com.list_of_your_packages.launcher 

That is, it was necessary to specify the jarnik itself in the classpath, and then specify the full path to the class with the main method

    Maybe someone will come in handy. Found another answer that helped me:

    Sometimes it is useful to read not only books, but also documentation. http://docs.oracle.com/javase/7/docs/technotes/tools/windows/java.html

    The java command does not take your e:\MyJava\example as a path at all.

    She believes that this is the name of the class. The paths are set differently. This is a large and complex topic - the classpath.

     java -cp e:\MyJava\ example 

    We have to look for classes in e:\MyJava\ run the example class.

    • one
      Try to form the answer, it is difficult to read. - 0xdb

    The problem is solved if you change the launch string

    instead of the javac C: \ Java \ HelloWorld.java command, run javac C: \ Java \ HelloWorld

    With the removal of the * .java file extension, the main class is detected. The program starts. Why so, I did not begin to understand.

      Spent a lot of time to solve this issue, and the solution is this. It is necessary to prescribe the variable environment. It is necessary in the environment variables to specify the path to the bin and the path to the lib . About lib rarely speaks. If you run from the command line and the path to the libraries is not specified in the environment variables, then jvm does not know where to get them. Win 10. For example, c:\Program Files\Java\jdk-10.0.1\lib\ and c:\Program Files\Java\jdk-10.0.1\bin\
      Successes!

        In the environment variables, where you write the path to the JDK to the CLASSPATH variable, put a ".;" Before the path. I did it .;C:\Program Files\Java\jdk1.8.0_201\bin The dot indicates the current directory. After editing, do not forget to restart Windows.

        • Rebooting does not seem necessary, just open a new window of the command interpreter. Check if the answer is complete. - 0xdb pm

        For a class in the src directory and in the basic package,

         package basic; public class Main { public static void main(String[] args) { System.out.println("Hello from Main"); } } 

        it works for me

         $ javac src/basic/Main.java $ java -cp src basic.Main Hello from Main 
          • Navigate to the correct directory using the cd console command ;
          • You can also add the desired path to the CLASSPATH environment variable;
          • The easiest way is to call CMD from TotalCommander by going to the correct directory.