I’m having trouble compiling a java code using Packages on linux

Asked

Viewed 400 times

1

I don’t know exactly the command to compile a java file on linux that shares the same package. I tried to follow this tutorial:https://www.webucator.com/how-to/how-compile-packages-java.cfm but the following error is appearing:

Error: Could not find or load main class Classe01
Caused by: java.lang.NoClassDefFoundError: pacote1/Classe01 (wrong name:Classe01)

Follow the code below:

/* Classe01.java */
package pacote1;

public class Classe01{

   public static void main(String[] args){
      Classe02 obj1 = new Classe02("abc");
      Classe02 obj2 = new Classe02("abc");
      System.out.println(obj1==obj2);
   }
}


/* Classe02.java */
package pacote1;

public class Classe02{
  private String valor;

  public Classe02(String s){
    valor = s;
  }
  public String getValor(){
    return valor;
  } 
}
  • Larissa, just so we understand your question, are you trying to compile the classes at hand or are you using an IDE (Netbeans or Eclipse)? Other than that, I imagine the structure of your project is like this, a folder called pacote1 and inside this folder you have 2 files Classe01.java and Classe02.java, that’s right?

  • 1

    Make sure you are not missing add extension .java to the class name at the time it calls the compiler javac. Already to run the application in the JVM (command java) that’s not necessary.

  • @Muriloportugal to try to compile the classes in hand. That’s right.

  • @Piovezan did not say add the extension . java.

1 answer

2


Larissa, as you are compiling in hand, have to take into account some considerations:

1° You write the code in the archives .java, but before creating the JAR executable you must compile the files .java in .class.

2° Create a file indicating to JAVA where its method is public static void main(String[ ] args), this file is known as MANIFEST.

3° After the compilation and creation of the MANIFEST you can already pack everything in one executable .jar.

With these considerations in mind, first let’s compile your . java classes into . class.
To do this, by terminal or CMD go to the same level of your folder pacote1 (do not enter in pacote1, just be at the same level as her) and type in the terminal:

javac pacote1/*.java

This command will compile all files with extension .java that are inside the folder pacote1, you will see that 2 more files appeared, Classe01.class and Class02.class.

Now let’s create the MANIFEST, still at the same level as your folder pacote1 create a MANIFEST.txt file and within it put the content below:

Main-Class: pacote1.Classe01
Name: pacote1/Classe01.class
Java-Bean: True

Now you just need to run the command below so that your compiled classes and manifest are packaged in a file .jar.

jar cfm seuArquivo.jar manifest.txt pacote1/*.class

And to test run the command:

java -jar seuArquivo.jar

Note: Doing this compilation by hand is very laborious, since any changes you make in the code will have to compile the classes and generate the .jar again, except that if you are going to use third-party libraries, you will have to inform them on MANIFEST, I recommend using an IDE as it already automates this whole process.

  • 1

    Just exchange the MAINFEST for MANIFEST and it’s all right :) +1

  • @Piovezan thanks for warning, nor had I touched that I wrote wrong! rsrs

Browser other questions tagged

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