A
A
Anonymous Androidovich2015-10-01 16:32:19
Java
Anonymous Androidovich, 2015-10-01 16:32:19

Why is a ClassNotFoundException thrown?

Good day.
While studying java, I ran into a question about the requirement for fundamental knowledge, which I do not have enough.
The essence of the problem - we have macos, 2 folders - src and bin
In src I created a text file souttest.java in which the simplest HeloWorld Compiled
javac -d bin src / souttest.java using the terminal, after which a test.class file was created in the bin folder
The problem itself - if the program is not launched from a folder, then an error occurs - namely,
yr-2: test NG$ java bin/test.class
Exception in thread "main" java.lang.NoClassDefFoundError: bin/test/class
Caused by: java.lang .ClassNotFoundException: bin.test.class
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader .loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
But if you execute the following cd bin commands, and after java test (without .class) - it is executed correctly.
Please help me to sort out the problem

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
abs0lut, 2015-10-01
@abbath0767

Add the -cp flag to the java command invocation, followed by the absolute path to the .class file.
ps Read about classpath

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question