I have a SpringBoot app in which I am trying to test generation of barcodes but I am getting this error java.io.FileNotFoundException: (Read-only file system) Mac
.
Here's the code to accomplish this task:
pom.xml
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.sf.barcode4j</groupId>
<artifactId>barcode4j</artifactId>
<version>2.1</version>
</dependency>
Test Class
public class FooTest extends TestCase {
@Test
public void testP() {
try {
Code128Bean bean = new Code128Bean();
final int dpi = 160;
//Configure the barcode generator
bean.setModuleWidth(UnitConv.in2mm(2.8f / dpi));
bean.doQuietZone(false);
//Open output file
File outputFile = new File("/" + "test" + ".JPG");
FileOutputStream out = new FileOutputStream(outputFile);
BitmapCanvasProvider canvas = new BitmapCanvasProvider(
out, "image/x-png", dpi, BufferedImage.TYPE_BYTE_BINARY, false, 0);
//Generate the barcode
bean.generateBarcode(canvas, "test");
//Signal end of generation
canvas.finish();
System.out.println("Bar Code is generated successfully…");
}
catch (IOException ex) {
ex.printStackTrace();
}
}
}
Error
java.io.FileNotFoundException: /test.JPG (Read-only file system)
at java.io.FileOutputStream.open0(Native Method)
at java.io.FileOutputStream.open(FileOutputStream.java:270)
at java.io.FileOutputStream.<init>(FileOutputStream.java:213)
at java.io.FileOutputStream.<init>(FileOutputStream.java:162)
Any ideas on how I could make this work on my machine (MacBook)? Would the configuration be different for Linux?
The problem is this:
File outputFile = new File("/" + "test" + ".JPG");
Note that "/" is the root directory.
The root directory on Mac OS is apparently in a read-only file system. That means you cannot write to it.
On Linux / UNIX systems, the root filesystem is typically not read-only, but your application won't have permission to write to the root directory anyway.
Any ideas on how I could make this work.
Don't try to write files into the root directory "/". Find somewhere more appropriate; e.g. the current working directory, the user's home directory, a temporary directory, etcetera.