-2

I can't run any java program with terminal:

Screen shot of the error

openjdk version "10.0.2" 2018-07-17 OpenJDK Runtime Environment (build
10.0.2+13-Ubuntu-1ubuntu0.18.04.2) OpenJDK 64-Bit Server VM (build 10.0.2+13-Ubuntu-1ubuntu0.18.04.2, mixed mode)

Any ideas?

6
  • And where is the command that gave this error? Commented Oct 13, 2018 at 11:05
  • Down in screenshot, i use command "java" to run javafile. Commented Oct 13, 2018 at 11:12
  • 2
    Compile first before running, and you're using an IDE so use that to compile and run it. Images of this sort aren't advised copy and paste! Commented Oct 13, 2018 at 11:35
  • This has nothing to do with Ubutnu. Learn to use javac, and java. Or just use IDE to run. Commented Oct 13, 2018 at 12:39
  • I'm using Intellij, but i need to learn some terminal commands, who can help me in the future :) I was reading about java and javac commands, but totally forgot about that packages are not folders :) Commented Oct 13, 2018 at 12:50

1 Answer 1

1

To compile Java source code (usually with a file ending in .java) you need to enact the Java Compiler, a.k.a. javac. This compiles the source to a bytecode binary file ending in .class.

When running bytecode binaries, you refer to the full classname, not the file. Java will then look for the appropriate .class file relative to the current location, load the appropriate class, and call the function main(). This allows there to be many (even nested) callable starts to a program from the one compiled binary classfile.

Furthermore, Java classes can exist in packages which are organised by directory, and declared at the top of the file, e.g.

package Arrays;

So, once you have compiled your program, you need to be in the folder above the Arrays folder, and then remove the .java from your java command and prefix the classname with 'Arrays.'. e.g.

$ cd /path/to/src
$ java Arrays.Speeds
4

Not the answer you're looking for? Browse other questions tagged .