Created a project in NetBeans (according to the book by P. Naughton, G. Shildt "Java2. The most complete guide").

 /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package example2; /** * * @author Asus */ /* * Другой короткий пример. * Файл "Example2.java" */ public class Example2 { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here int num; //объявляет переменную с именем num num=100; //присваивает num значение 100 System.out.println("Значение num: "+num); num*=2; System.out.print("Удвоенное значение num: "); System.out.println(num); } } 

When creating a project, the class example2.Example2 was created with the project folder D:\ExamplesFromBook\Example2\Example2 (and the project location is D:\ExamplesFromBook\Example2 ). Created bytecode using the command line

 D:\ExamplesFromBook\Example2\Example2\src\example2>javac Example2.java 

as a result, the file Example2.class was created. Example2.class I tried to launch a .class file from the command line in a variety of ways, including, for example:

 D:\ExamplesFromBook\Example2\Example2>java example2.Example2 

resulting in an error

 Error: Could not find or load main class example2.Example2 

Which command should I use and from which folder should I run?

  • Hmm java example2 / Example2 - alexlz
  • @alexlz from which folder to run this command (specify a specific path)? - ivan89
  • Well, probably a level higher. True, according to the tutorial, the command will be java example2.Example2 (as there, in Windows, there will be java example2/Example2 - I don't know, I don't have any Windows). And in the package, it seems that the way is given.) - alexlz
  • @alexlz D: \ ExamplesFromBook \ Example2> java example2.Example2 does not help. Option c / also does not work. If there is NetBeans , then having done everything from the very beginning (having set the location and the same path and name of the project in the project settings), you can make sure that it does not work and gives the same error. - ivan89
  • The reluctance of netbeans to run. But from / home / alex java -classpath ./tmp example2.Example2 (directory example2 is located in / home / alex / tmp) - alexlz

1 answer 1

In the directory D: \ ExamplesFromBook \ Example2 \ Example2 create a classes folder.

Then,

 D:\ExamplesFromBook\Example2\Example2>javac -classpath ./classes -d ./classes src/example2/Example2.java 

Thereafter:

 D:\ExamplesFromBook\Example2\Example2>java -classpath ./classes example2.Example2 

Carefully read the article:

http://skipy.ru/technics/likbez.html