Get Directory After JAVA Compiled

Asked

Viewed 267 times

0

Beforehand I would like to say that I have already searched in several links indicated here in stackoverflow and other sites as well and I still could not get concrete help (it may be my mistake too).

I need to get a file found in folder res, that folder res this inside the project folder (outside the folder src to make it clearer).

Right in my program I can do the following ("while in Netbeans")

   public static void obter(){
       File f = new File("res\\file.txt");
   }

in the above method it can read while running in netbeans. Not outside the netbeans. Error generated outside of Netbeans:

res\file.txt (O sistema não pode encontrar o caminho especificado)

Elsewhere in my code I use the code below and it also works in netbeans

File f = new File(this.getClass().getResource("/file.txt").getFile());

in the above method even without specifying the folder res it can still find while in netbeans. However outside netbeans returns this to me:

file:\C:\Users\User\Documents\NetBeansProjects\TEST\dist\TEST.jar!\file.txt (A sintaxe do nome do arquivo, do nome do diretório ou do rótulo do volume está incorreta)

How to get this file.txt of the briefcase res even if I code and pass it to any computer?

PS: If I haven’t made it clear please let me know and try to improve my question.

2 answers

0


I’d like to thank Rafael Guasselli for the help, I would like it to be corrected the answer so I can approve and make it clear to future people who were having the same problem.

Follows the answer:

After building the project in Netbeans it generates a folder dist and there contain the files needed to run it. Soon if your project needs to access some other folder it should be put there also after compiled.

In the example below we need to find the folder res.

Ex: let’s say the folder res is inside the src.... So after compiled, I go to the folder dist and create a folder src and glue the paste res inside.

Case the folder res is out of the src then after compiled just copy it to the folder dist.

This is also for lib that usually give problem when distributing the application.

For your libs to go along with the project, just create a folder called lib within your project (outside the src).

Ex: C:\Users\User\Documents\NetBeansProjects\MeuProjeto\ (here I create the folder lib and glue all the libs I need). (Yes! Colo)

Soon after in Netbeans I go on to add Library >> JAR/Folder add

Here I find the lib folder we created, and inside it we select all the libs we need. NOTE: Leave Absolute Path(Absolute Path) marked.

Why is that?

After compiled the .jar will already have a folder named lib in the same location as it. Remember that folder lib created by compiling it in dist? So, you guarantee that the path he is seeking is exactly what he will get after compiled. That is to say, lib/"as bibliotecas que inclui"

0

To be able to run the jar after compiled you need to take the file along with it. You can either place the jar in a folder and within that folder create the folder res again and put the file there or you can compile open the . jar with Winrar and put the folder res inside the . jar.

  • If I open the . jar compiled with Winrar and put the folder res inside, with getResourse I can get access to res? or I can simply create File with "res/file.txt path" ?

  • Actually, I did, the part about putting it in the jar wasn’t right. , but you can still do it like this: leaves the file inside a res folder inside src to pick up when you’re all in netbeans. and after compiling you copy src only with the files to the same folder as the jar is. and Dae tu usa o primeiro comando File file = new File("src/res/file.txt");

  • I’ll do a test when I have more time, and see if it works, although the res folder is in the netbeans Folder package, that is, if I put File file = new File("res/file.txt") it can already find, so if I compile and put only the folder res along with the . jar he will find only by doing it right? soon I’ll be back with more results..

  • The way we commented here in the comments worked, just compile and create a src/res/ folder and put the files there that works. but if I don’t want to use the src folder and want to directly use the res folder just include it in the netbeanse Folder package after compiled include that res folder where the . jar, so you don’t need the src folder.. If you want to edit your answer and put this I will already flag as answer. xD vlw

Browser other questions tagged

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