using java class from another directory

Asked

Viewed 773 times

-2

Well friends, I was using two classes:

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

So far so good, I used the class 2 command import exercicio3.Funcionario; and it worked fine, 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 Work class in the Using class /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 to compile:

Documentos/java/exercicio4$ javac UtilizndoFuncionario.java
  • sends the directory structure, import code and your call to the console.

  • Have you changed your import? You have to change it tb, if you changed the address of your files...

  • Sorry it takes... The directory for the Funcionario.java class is: /home/lcs/Documents/java/exercicio3 package exercicio3; public class Funcionario{ private int matricula; private double salario; //REST OF THE CODE ... } While the Using class.java was in: /home/lcs/Documents/java package java; import exercicio3.Funcionaio; public class Utilisingwork{ public Static void main(String ... args){ //REST OF THE CODE ... } }

  • In the terminal I used the following command, with the terminal inside the directory: lcs@LcsMarlon:~/Documents/java$ javac Usefull.java Compiles everything right, but when I changed the file Usefull.java to the other directory: /home/lcs/Documents/java/exercicio4 Started to give error when I typed in the terminal, in the new directory of course: lcs@LcsMarlon:~/Documents/java/exercicio4$ javac Utilizndofuncionaio.java Presented error: Utilizandofuncionario.java:1: error: package exercicio3 does not exist import exercicio3.Funcionario; 1 error @ulima69

  • How would I change my import @Diegosantos??? I tried, but I can’t manage!

2 answers

1

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

What is the procedure?

  1. Compile the class Funcionario.java and putting the .class in the same folder:

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

  2. Compile the main class

    $javac -cp . UtilizandoFuncionario.java

  3. Rotate the main class

    $java -cp . UtilizandoFuncionario

Starting from step 2, the compiler will already check that the file Funcionario.class will be in the folder and then you can compile.

Because there are these compilation difficulties it is used in Java build tools like the ant or Maven.

In your case that is beginning, I advise the good old Eclipse and a booklet by Caelum excellent.

0


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

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

The above command automatically creates a folder (with the name of the class "package" Java, in case a folder with name was created exercicio3), in the briefcase exercicio4, which is the file UtilizandoFuncionario.java.

Note: the -d causes the .class be created in the directory intended, in this case was the /home/lcs/documents/java/exercicio4/

All I had to do was import in the Using class:

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, I just went to the file directory Utilizing employee.java and put the command:

$javac UtilizandoFuncionario.java

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

$java UtilizandoFuncionario

Browser other questions tagged

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