Run . java that imports another class in the terminal

Asked

Viewed 250 times

0

I am trying to run my program on the terminal, as it matters another class I am using the command:

javac -cp ./IO.jar MeuPrograma.java

The program compiles the .class. However when running with:

java -cp ./IO.jar MeuPrograma

me is returned in the terminal the following error:

Error: Unable to locate or load the main class Myscript Caused by: java.lang.Classnotfoundexception: Myscript

How can I run my program on the terminal using this other class?

NOTE: the class q I use in the program is in the same directory as . java and . class

Imagem do erro no terminal

  • What is the full name of the class MeuPrograma?

  • Guia_02 when I compile/execute I use the name of the class that in the program this: public class Guia_02{}

2 answers

2


The directory in which the .class shall also be informed in the cp.

The full name of the class must be specified, something like snickers.guia.MeuPrograma.

In your case, enough should be:

java -cp ./:./IO.jar MeuPrograma

If this doesn’t work, the problem that usually occurs is to define a package, and put her as the way of classpath:

package snickers;
class Guia_02 {
}

Calling java -cp ./:./IO.jar snickers.MeuPrograma won’t work. Java expects the class to be inside the directory snickers. Even changing to absolute paths java -cp "/home/bluorchid/Área de Trabalho/snickers/:/home/bluorchid/IO.jar" snickers.MeuPrograma won’t work.

Right is something like java -cp "/home/bluorchid/Área de Trabalho/:/home/bluorchid/IO.jar snickers.MeuPrograma".

  • Could you please edit with the command example as I asked in the question ?

  • Your example wasn’t very clear to me. I haven’t been able to use the command yet.:/

2

You have to instantiate the class within the class being executed.

Another = new Another();

another method.();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.