0

I just created a .java file.

(my IDE is NetBeans, java version java version "1.8.0_101"

Java(TM) SE Runtime Environment (build 1.8.0_101-b13)

Java HotSpot(TM) 64-Bit Server VM (build 25.101-b13, mixed mode))

I want to compile it and then execute it from windows CMD because of the command line arguments. So i write on CMD: javac className.java and everything works as expected. After that i write the command: java className and this error pops out on CMD: Error: Could not find or load main class AlgorithmsProject

Now on docs.oracle.com it says clearly on Common Problems:

"A common mistake made by beginner programmers is to try and run the java launcher on the .class file that was created by the compiler. For example, you'll get this error if you try to run your program with java HelloWorldApp.class instead of java HelloWorldApp. Remember, the argument is the name of the class that you want to use, not the filename"

Obviously i write and the className only and it gives me the same ERROR.

On youTube some videos show that you have to write the following commands on CMD:

set path="C:\Program Files\Java\jdk1.8.0_101\bin";

set classpath="C:\Program Files\Java\jre1.8.0_101\lib\rt.jar";

and the same error still pops out.

Any suggestions?

DIMITRIOS
  • 305
  • 1
  • 3
  • 10
  • 1
    Read the answers on [What is the reason for 'sort' is not recognized as an internal or external command, operable program or batch file?](http://stackoverflow.com/questions/41454769/) and answer on [Why is no string output with 'echo %var%' after using 'set var = text' on command line?](http://stackoverflow.com/a/26388460/3074564) Then you should know why `set path="C:\Program Files\Java\jdk1.8.0_101\bin";` is a very, very bad idea and `set classpath="C:\Program Files\Java\jre1.8.0_101\lib\rt.jar";` is also no good idea. – Mofi Mar 22 '17 at 11:33
  • oh yes - do **not** overwrite / set your path variable, thats somewhat equal to throwing a wrench into a running engine *(unless you _**append**_ a path)*. You also should probably never look at these videos again. This is how you might append a path : `set path="%path%; YOUR_APPENDED_PATH_HERE"` – specializt Mar 22 '17 at 12:42
  • 1 "A common error..." does not mean that the error message is just for that case; 2 YouTube video is not very accurate - normally there is no reason/need to have rt.jar in the classpath (this was eventually needed very long time ago) - probably would be better to have it empty... – user85421 Mar 22 '17 at 13:48
  • recommended reading: [How the Java Runtime Finds Classes](http://docs.oracle.com/javase/8/docs/technotes/tools/windows/findingclasses.html#A1012444) and [Setting the Class Path](http://docs.oracle.com/javase/8/docs/technotes/tools/windows/classpath.html) – user85421 Mar 22 '17 at 14:04
  • So do i have to delete these paths? Are they goingg to cause any problem? – DIMITRIOS Mar 22 '17 at 15:00
  • Yes and no : deleting the paths wont be necessary since you can simply **reboot**. Java does not need any path entries anymore, thats done automatically. – specializt Mar 22 '17 at 16:12
  • @specializt `set path="%path%; YOUR_APPENDED_PATH_HERE"` is also wrong. Right is `set "PATH=%PATH%;YOUR_APPENDED_PATH_HERE"`. It makes a big difference where the first double quote is set: at beginning of parameter string of command __SET__ or at beginning of the string value assigned to the environment variable. – Mofi Mar 22 '17 at 17:47
  • maybe next time do a bit of research : https://ss64.com/nt/set.html – specializt Mar 23 '17 at 08:48

1 Answers1

0

I'm Java newbie, but I had the same problem. I don't know if it helps you but you can just try it because it helped me:

  1. javac className.java (just like you did earlier)

  2. go one directory up (back) and copy its path to clipboard (for example C:\Users\XYZ\Documents\Exercise1\src)

  3. in cmd write cd and paste the copied path with right mouse button and then click "Paste" so in cmd you are now moved one directory up (it looked this way im my example: cd C:\Users\XYZ\Documents\Exercise1\src)

  4. java packageName.className (where packageName is the name of the folder containing your java file and the name you gave to the package when creating new project). In my example java file was under the following path: C:\Users\XYZ\Documents\Exercise1\src\Exercise1\className.java)

Guido
  • 46,642
  • 28
  • 120
  • 174