1
When doing a search for the websites of Stack Overflow, I realized that all topics only teach how to create the file . jar of a single class or even teach how to create the . jar of an entire package, but without the purpose of being an . executable jar, but rather a library.
My need here is different, I need to create the . jar of a project with several packages on the command line and this . jar has to be directly executable. I created an image to illustrate my problem, just follow the numbering in the photo. I also describe each step right after the image:
The project root folder has the path C:\test
Stages:
1 - I show the code of the test.java file which, in relation to the project root directory, has path test\src\pack1\test.java
package pack1;
import pack2.*;
public class test
{
public static void main(String[] args)
{
System.out.println(test2.s);
}
}
2 - I show the code of the test2.java file which, in relation to the project root directory, has path test\src\pack2\test2.java
package pack2;
public class test2
{
public static final String s = "20/20";
}
3 - I visually show the project structure by the command tree /f
4 - I compile the files. java by playing them in the bin folder with the command javac -d bin -cp src src\pack1\test.java
5 - Visually display the project structure by the command tree /f
6 - I create the . jar file with the command jar --verbose --create --file test.jar --main-class pack1.test bin
7 - I visually show the project structure by the command tree /f
8 - I try to run . jar with command java -jar test.jar
and I get:
Erro: Não foi possível localizar nem carregar a classe principal pack1.test
Causada por: java.lang.ClassNotFoundException: pack1.test
When I do this same process with just one . class, I can run . jar without any problem.