using java class from another directory

-1

Well friends, I was using two classes:

Funcionario.java (class 1, from the /home/lcs/Documentos/java/exercicio3 directory) and UtilizandoFuncionario.java (class 2, from the /home/lcs/Documentos/java directory).

So far so good, I used in class 2 the command import exercicio3.Funcionario; and it worked perfectly, but when I put class 2 in the directory /home/lcs/Documentos/java/exercicio4 the command stopped working and started to give error in the terminal. >

I would like to know how to use the Employee class in the Employee class in the new directory, in the /home/lcs/Documentos/java/exercicio4 ?

package exercicio3; 

public class Funcionario { 
  private int matricula; 
  private double salario; 
  //RESTO DO CÓDIGO ... 
} 



package java; 
import exercicio3.Funcionario; 
public class UtilizandoFuncionario { 
  public static void main(String ... args){ 
    //RESTO DO CÓDIGO ... 
  } 
} 

I used it to compile:

Documentos/java/exercicio4$ javac UtilizndoFuncionario.java
    
asked by anonymous 19.03.2018 / 21:07

2 answers

0

By the terminal, in the directory where the file Funcionario.java was located I gave the following command:

$javac -cp . Funcionario.java -d /home/lcs/Documentos/java/exercicio4/

The above command causes a folder (with the name of the Java class to be automatically created, in which case a folder with name exercise ) , in the exercise4 folder, which contains the file UtilizandoFuncionario.java .

  

Note: -d causes .class to be created in the intended directory, in this case it was / home / lcs / Documents / java / exercise4 /

I just imported into the class Using the Functional class with the import, so it was:

import exercicio3.Funcionario;
//Ou caso tenha mais de uma classe
//import exercicio3.*;

public class UtilizandoFuncionario{
    public static void main(String ... args){
       //Resto do código
    }
}

In the terminal, just go to the Using Employee.java directory and put the command:

$javac UtilizandoFuncionario.java

It worked all straight when I tested the program with the command:

$java UtilizandoFuncionario
    
21.03.2018 / 14:05
1

When you attempt to compile, the error arises because the Funcionario.java class has not yet been compiled, or if it was already in the same folder or in the classpath for compilation of the UtilizandoFuncionario.java class, so the compiler does not find the class Funcionario.class .

What is the procedure?

  • Compile the Official.java class and put .class in the same folder:

    $javac -cp . /home/lcs/Documentos/java/exercicio3/Funcionario.java

  • Compile the main class

    $javac -cp . UtilizandoFuncionario.java

  • Rotate the main class

    $java -cp . UtilizandoFuncionario

  • From step 2, the compiler will already check that the Funcionario.class file will be in the folder and will then be able to compile.

    Because these compilation difficulties exist, compiling tools such as ant or maven are used in Java.

    In your case that is starting, I advise the good old Eclipse and a Caelumapostila excellent.

        
    20.03.2018 / 17:26