Hi Stephen,

thanks for your inspiration.

I'm using an ANT project. I never managed how to go with Maven. So my file 
structure is:

Project
+ src
  + Java ...
+ test
  + Java ...
+ resources
  + Palm8BitColors.pal
+ TestData
  + ...

So you mean, that I should move the resource file into the src path. I was 
thinking, that there is a way to configure the NetBeans build properties, so 
that the file will be included into the JAR. But I have forgotten how to do.

Using getResourceAsInputStream() is a good idea. Actually I need the file 
content in a byte array or ByteBuffer.

-Ulf

Am 23.06.24 um 19:20 schrieb Stephen G. Parry:
Are you using a Java with Maven project? Assuming so, the process for what you 
are asking is a two step one:
1) Ensure the files you wish to access are located under the src/main/resources 
folder as shown here:
https://i0.wp.com/www.dineshonjava.com/wp-content/uploads/2016/10/Maven-dirctory-structure.png?w=728&ssl=1
 
<https://i0.wp.com/www.dineshonjava.com/wp-content/uploads/2016/10/Maven-dirctory-structure.png?w=728&ssl=1>
Doing this should ensure that the file is picked and included in the compiled 
jar file.
2) You will need to add to your code. What exactly you add will depend on 
whether you are opening the file from a static member or a proper member 
function and whether you really need an FileInputStream or just an InputStream. 
I have pasted some code to illustrate:
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import 
java.io.InputStream; import java.net.URISyntaxException; import java.net.URL; import 
java.util.Scanner; /**  *  * @author parrysg  */ public class ResourceTest {     public static void 
main(String[] args) throws URISyntaxException, FileNotFoundException {         URL url = 
ResourceTest.class.getResource("/simpleResource.txt");         FileInputStream fs = new 
FileInputStream(new File(url.toURI()));         Scanner input = new Scanner(fs);         
System.out.println(input.nextLine());         InputStream is = 
ResourceTest.class.getResourceAsStream("/simpleResource.txt");         Scanner input2 = 
new Scanner(fs);         System.out.println(input2.nextLine());     } }
In the above code, I have used main (i.e. a static member), but if you are 
opening within a non-static member, it is better to use getClass():
InputStream is = getClass().getResourceAsInputStream();

Note that, if you do just use InputStream, the Exception imports and throws are 
not needed; it's overall much simpler.

regards

Stephen Parry


On 23 June 2024 15:50:14 BST, Ulf Zibis <ulf.zi...@cosoco.de> wrote:

    Hi, I have a resource file im my project under the folder "resources". From my code, the file 
is accessed by: FileInputStream rs = new FileInputStream(new File("resources/Palm8BitColors.pal")); 
This works fine, when I run the application under NetBeans IDE. But the resource file is not included in the 
JAR file under "dist", so it won't run independently of NetBeans. How is the correct way to 
integrate and access the file into the JAR? Thanks -Ulf
    
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    To unsubscribe, e-mail: users-unsubscr...@netbeans.apache.org For 
additional commands, e-mail: users-h...@netbeans.apache.org For further 
information about the NetBeans mailing lists, visit: 
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists

Reply via email to