ZipException when attempting to open jar file - java

I keep getting a java.util.zip.ZipException when I try to open a jar file. I was able to reproduce the issue with the following stripped-down bit of code (ignore the weird populateSamples() method name):
import java.io.File;
import java.util.jar.JarFile;
public class Test {
public static void main(String[] args) {
Test test = new Test();
test.populateSamples();
}
private void populateSamples() {
JarFile jf = null;
try {
String s = new File(this.getClass().getResource("Test.class").getPath()).getParent().replaceAll("(!|file:\\\\)", "");
jf = new JarFile(s);
} catch(IOException e) {
e.printStackTrace();
} finally {
try {
jf.close();
} catch(Exception e) {
e.printStackTrace();
}
}
}
}
Here is the exception I get:
$ java -jar EclipseTest.jar
java.util.zip.ZipException: error in opening zip file
at java.util.zip.ZipFile.open(Native Method)
at java.util.zip.ZipFile.<init>(ZipFile.java:128)
at java.util.jar.JarFile.<init>(JarFile.java:136)
at java.util.jar.JarFile.<init>(JarFile.java:73)
at Test.populateSamples(Test.java:40)
at Test.main(Test.java:17)
java.lang.NullPointerException
at Test.populateSamples(Test.java:54)
at Test.main(Test.java:17)
I can list the contents of the jar file with jar tf just fine. Any ideas?

I have found the solution to the problem. The replaceAll("(!|file:\\\\)", ""); bit was not working as expected as the "file:" bit was not being replaced. I shall now embark on a quest to figure out why not, and complete this answer.

Related

Create a file which cannot be deleted by file.delete()

Note: I am aware there are several questions similar to this one, however, I cannot find any which explain how to resolve the situation I am trying to resolve. I will ask this question with a specific example, for which I need a solution.
Consider the code:
private final void writeToFile(final File parent, final String filename, final Charset charset, final String content) throws IOException {
final File file = new File(parent, filename);
if (file.exists()) {
LOG.warn("File {} already exists, file will be replaced.", file.getCanonicalPath());
if (!file.delete()) {
logAndThrow(String.format("Cannot delete file '%s'.", file.getCanonicalPath()), null);
}
}
try (final FileOutputStream fos = new FileOutputStream(file);
OutputStreamWriter writer = new OutputStreamWriter(fos, charset)) {
writer.write(content);
}
}
I am trying to write a unit test to provoke the IOException being thrown when the code cannnot delete the file. The unit test I have tried is as follows:
#Test public void testFileNotDeletable() throws IOException {
final File file = new File(folder.getRoot(), formattedFile.getMetaData().getFormattedCaptureFileName());
file.createNewFile();
try {
file.setReadOnly();
exception.expect(IOException.class);
exception.expectMessage(String.format("Cannot delete file '%s'.", file.getCanonicalPath()));
writer.write(formattedFile);
} finally {
file.setWritable(true);
}
}
I have also tried locking the file:
#Test public void testFileNotDeletable() throws IOException {
final File file = new File(folder.getRoot(), formattedFile.getMetaData().getFormattedCaptureFileName());
file.createNewFile();
try (FileInputStream fis = new FileInputStream(file)) {
final FileLock lock = fis.getChannel().tryLock(0L, Long.MAX_VALUE, true);
try {
exception.expect(IOException.class);
exception.expectMessage(String.format("Cannot delete file '%s'.", file.getCanonicalPath()));
writer.write(formattedFile);
} finally {
lock.release();
}
}
}
No matter what I try, the file.delete() successfully deletes the file, and the test fails, as the expected IOException was not thrown.
Any help greatly appreciated.
Note: Added for clarification, some extra code that shows that the File object is completely separate in the environments. The formattedFile being passed to the write method is not a File or sub-class of File, it is one of our internal classes. The File in the JUnit test is using a TemporaryFolder for the root, the formattedFile has a MetaData item, which determines the filename. In my JUnit test I am trying to create an empty file, which cannot be deleted, in the location that my actual code will attempt to write the file. I need file.delete() to return false, so that I can test the exception is being thrown. I therefore cannot mock a File object.
There are two solutions to your question, I recommend the first one.
Solution 1
You are not testing the java file I/O operations/class here, you are testing your code's functional behaviour in response to file operation. So, ideally in your JUnit you should be mocking the File object & its respective calls, and only focus on testing your code.
Solution 2
If you still wish to test full integration with java file IO, open file in write mode before attempting to delete, and it will take care of your test case.
NOTE: Code tested in CENTOS, WINDOWS, UBUNTU, MAC OS-X
Subject Class:
public class FileSolution {
public void fileHandler(File file) throws IOException, Exception {
if (file.exists()) {
LOG.warn("File {} already exists, file will be replaced.",
file.getCanonicalPath());
if (!file.delete()) {
logAndThrow(String.format("Cannot delete file '%s'.",
file.getCanonicalPath()),
new IOException(String.format("Cannot delete file '%s'.",
file.getCanonicalPath())));
}
}
}
}
Subject Uner Test:
import static org.mockito.BDDMockito.*;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
public class FileSolutionTest {
#Rule
public final ExpectedException exception = ExpectedException.none();
/**
* Solution 1
*
* #throws Exception
*/
#Test
public void testFileNotDeletableWithMock() throws Exception {
final File file = mock(File.class);
file.createNewFile();
// mock file & IO operations
given(file.exists()).willReturn(true);
given(file.delete()).willReturn(false);
given(file.getCanonicalPath()).willReturn("test.txt");
exception.expect(IOException.class);
exception.expectMessage(String.format("Cannot delete file '%s'.", file.getCanonicalPath()));
new FileSolution().fileHandler(file);
}
/**
* Solution 2
*
* #throws Exception
*/
#Test
public void testFileNotDeletable() throws Exception {
File file = null;
FileWriter fileWriter = null;
try{
file = new File("test.txt");
file.createNewFile();
file.deleteOnExit();
exception.expect(IOException.class);
exception.expectMessage(String.format("Cannot delete file '%s'.", file.getCanonicalPath()));
// open file with another process for writing
fileWriter = new FileWriter(file, true);
new FileSolution().fileHandler(file);
} finally{
if(fileWriter != null){
fileWriter.flush();
fileWriter.close();
}
}
}
}
I totally agree with Turing85 about using mockito.
Let's imagine you have an original class with a method similar to the one you want to test:
public class FileDel {
public void logOnIOException(File file) throws IOException {
if (file.exists()) {
LOG.warn("File {} already exists, file will be replaced.", file.getCanonicalPath());
if (!file.delete()) {
logAndThrow(String.format("Cannot delete file '%s'.", file.getCanonicalPath()), null);
}
}
}
public void logAndThrow(String msg, String s) {
//Do nothing
}
private static class LOG {
public static void warn(String msg, String path) {
}
}
}
Then you can trigger an inner exception in this way:
#RunWith(MockitoJUnitRunner.class)
public class FileDelTest {
#Test(expected = IOException.class)
public void testFileNotDeletable() throws IOException {
File file = mock(File.class);
when(file.exists()).thenReturn(true);
when(file.delete()).thenAnswer(new Answer<Boolean>() {
#Override
public Boolean answer(InvocationOnMock iom) throws Throwable {
throw new IOException();
}
});
FileDel f = new FileDel();
try {
f.methodToTest(file);
} finally {
}
}
}
What about open InputStream for this file and do not close it. Until file's descriptor will not be closed, file will not be deleted.
In order to prevent a file from being deleted you have to deny the security permission in windows. From the UI we would need to do something like
Right-click the file or document in your PC => Choose Properties;
In Security, tab Edit to change permission => Select Add and enter Everyone;
Press OK and select the group to change Full control permission to Deny;
Press Yes to confirm.
The only way I know of to change file permissions with Java are:
file.setExecutable(true|false);
file.setReadable(true|false);
file.setWritable(true|false);
and
File file = new File("test.txt");
if(file.exists())
{
//Setting file permissions for owner, group and others using PosixFilePermission
HashSet<PosixFilePermission> set = new HashSet<PosixFilePermission>();
//Adding owner's file permissions
set.add(PosixFilePermission.OWNER_EXECUTE);
set.add(PosixFilePermission.OWNER_READ);
set.add(PosixFilePermission.OWNER_WRITE);
//Adding group's file permissions
set.add(PosixFilePermission.GROUP_EXECUTE);
set.add(PosixFilePermission.GROUP_READ);
set.add(PosixFilePermission.GROUP_WRITE);
//Adding other's file permissions
set.add(PosixFilePermission.OTHERS_EXECUTE);
set.add(PosixFilePermission.OTHERS_READ);
set.add(PosixFilePermission.OTHERS_WRITE);
Files.setPosixFilePermissions(Paths.get("test.txt"), set);
}
else
{
System.out.println("Sorry...File doesn't exist.");
}
So, preventing a file from being deleted I would assume would have to do with file writing permissions. Try disabling the writable and maybe the executable permissions before trying to delete the file.
If this doesn't work then I do not believe it can be done with the Java language yet as these are the only methods available at the moment for changing file permissions. I could be wrong, but I have been unable to find anything else.
UPDATE
For Linux try the following:
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class ExecuteShellComand {
public static void main(String[] args) {
ExecuteShellComand obj = new ExecuteShellComand();
String command = "sudo chattr +i /backups/passwd";
// OR try
//String command = "sudo chattr +i -V /backups/passwd";
String output = obj.executeCommand(command);
System.out.println(output);
}
private String executeCommand(String command) {
StringBuffer output = new StringBuffer();
Process p;
try {
p = Runtime.getRuntime().exec(command);
p.waitFor();
BufferedReader reader =
new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
while ((line = reader.readLine())!= null) {
output.append(line + "\n");
}
} catch (Exception e) {
e.printStackTrace();
}
return output.toString();
}
}
The above makes /backups/passwd file immutable (or undeletable). This implies that the file can’t be modified in any way: it can’t be deleted or renamed. You can’t even create a link to it and no data can be written to the file as well.
That's about the only thing I can think of.
Hope this helps.
In Linux you can, with the chattr command set a file that is "immutable" that cannot be deleted even by root. Someone else said "set file permissions" which is right, but did not give specific detail.
Cheers
D

java.lang.NoClassDefFoundError: com/sun/star/lang/XEventListener

I am using pdfbox(pdfbox-app-2.0.0-RC3.jar) to convert any file to .pdf file. I am also using jodconverter-2.2.1.jar library.
Code I wrote (following this):
import com.artofsolving.jodconverter.openoffice.connection.*;
import com.artofsolving.jodconverter.openoffice.converter.*;
import com.artofsolving.jodconverter.*;
import java.io.File;
public class PdfBox {
public static void main(String[] args) throws Exception{
try {
OpenOfficeConnection con=new SocketOpenOfficeConnection(8100);
con.connect();
File inputFile=new File("x.docx");
File outputFile=new File("x.pdf");
DocumentConverter converter=new OpenOfficeDocumentConverter(con);
converter.convert(inputFile,outputFile);
con.disconnect();
} catch (Exception e) {
System.out.println(e);
}
}
}
Error message I get:
Any idea to get rid of this will be appreciated.
You need to add openoffice-ridl-2.0.3.jar (or some version of openoffice's jar) to your classpath.

Java: file cannot be resolved

the following code is returning the following error message:
package demo3;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
public class App {
public static void main(String[] args) {
try {
openFile();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("File not found: " + file.toString());
}
}
public static void openFile() throws FileNotFoundException {
File file = new File("test.txt");
FileReader fr = new FileReader(file);
}
}
Error:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
file cannot be resolved
at demo3.App.main(App.java:13)
I'm not sure whether this is because the file is in a different class to the try catch loop or if it's caused by something else. Any help would be much appreciated. Thanks
Instead of handling the FileNotFound exception in your main method, handle it in your openFile() method.
Right now you're trying to access the file method where the variable has not been defined. The file variable has only been defined in the openFile() method.
You can also define it above your main method. If you do that, every method in your class will have access to it.
Either solution will solve your problem. Choose the one that best fits your needs.

Loading images using an InputStream

In my IDE I'm able to get the path of an image that is in my resource folder and make that path a new file object by doing this:
URL imagePath = getClass().getResource("/image.png");
try
{
//Convert the URLs into URIs and make a file object with that path
File image = new File(imagePath.toURI());
}
catch (URISyntaxException e)
{
e.printStackTrace();
}
But when I make a jar file of my program I get the error URI is not hierarchical. And I have done some research and found out that I have to create an InputStream using the getResourceAsStream() method. But I do not know how to make that work for an image. I just need to be able to get the path of the image from my resource folder. How would I make this work even if its a jar.
Don't convert the URL to a File reference, this defeats the point of having the embedded resource and embedded resources are simply entries inside a zip file, so they can't be treated as a File.
Instead, use something like ImageIO.read(imagePath)
See Reading/Loading an Image for more details
I think the best solution in this case would be to ask the ClassLoader directly for an InputStream (using ClassLoader.getResourceAsStream) and pass that to ImageIO.read.
Here is a complete example.
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
public final class Main {
public static void main(final String[] args) {
final ClassLoader clsldr = Main.class.getClassLoader();
for (final String path : args) {
try {
InputStream is = null;
BufferedImage image = null;
try {
is = clsldr.getResourceAsStream(path);
if (is != null) {
image = ImageIO.read(is);
if (image != null) {
// Do something with the image.
System.out.printf("%s: %d x %d%n",
path,
image.getWidth(),
image.getHeight());
} else {
System.err.printf("error: %s: %s%n",
path,
"not a valid image file");
}
} else {
System.err.printf("error: %s: %s%n",
path,
"no such resource");
}
} finally {
if (is != null) {
is.close();
}
}
} catch (final IOException e) {
System.err.printf("error: %s: %s%n", path, e.getMessage());
}
}
}
}
Say I have a picture file photo.jpg and then compile above file and create a JAR file like this
$ javac *.java
$ jar -cfe example.jar Main *.class photo.jpg
then I can run the program like this and get the following output.
$ java -jar example NoSuchThing Main.class photo.jpg
error: NoSuchThing: no such resource
error: Main.class: not a valid image file
photo.jpg: 328 x 328

Java problems with accessing resource files when application is used as library

I have two Java applications. One application will contain resource files and will be used as library to other Java application.
First app com.test.resourceusing.MainClass.java which contains res/base.xml resource file.
package com.test.resourceusing;
import java.io.BufferedInputStream;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;
public class MainClass {
public MainClass() {
super();
}
public static void main(String[] args) {
MainClass main = new MainClass();
try {
main.start();
} catch (MalformedURLException e) {
}
}
public void start() throws MalformedURLException {
URL url = getClass().getResource("res/base.xml");
System.out.println(url.getPath());
System.out.println(url.getFile());
File f = new File(url.getFile());
if (f.exists()) {
System.out.println("File exist!");
BufferedInputStream result = (BufferedInputStream)
getClass().getResourceAsStream("res/base.xml");
Scanner scn = new Scanner(result);
while(scn.hasNext()){
System.out.println(scn.next());
}
} else {
System.out.println("Not working! :(");
}
}
}
Result is:
/C:/Work/Projects/ResourceUsing/classes/com/test/resourceusing/res/base.xml
/C:/Work/Projects/ResourceUsing/classes/com/test/resourceusing/res/base.xml
File exist!
<?xml
version='1.0'
encoding='utf-8'?>
<schema>
</schema>
Then I create .jar file which contains all resource files and try to use it as library in other application.
Second app:
resourcetest.MainClassTest.java
package resourcetest;
import com.test.resourceusing.MainClass;
import java.net.MalformedURLException;
public class MainClassTest {
public MainClassTest() {
super();
}
public static void main(String[] args) {
MainClass main = new MainClass();
try {
main.start();
} catch (MalformedURLException e) {
}
}
}
Result is:
file:/C:/Work/Projects/ResourceUsing/deploy/archive1.jar!/com/test/resourceusing/res/base.xml
file:/C:/Work/Projects/ResourceUsing/deploy/archive1.jar!/com/test/resourceusing/res/base.xml
Not working! :(
I don't understand why it's not working, is there problems in my code? Or this solution is not possible in Java?
Do you see the difference in the location of those files?
/C:/Work/Projects/ResourceUsing/classes/com/test/resourceusing/res/base.xml
file:/C:/Work/Projects/ResourceUsing/deploy/archive1.jar!/com/test/resourceusing/res/base.xml
You cannot access a resource that is located in a JAR file with the File API.
Your code is already on the way. A simple edit should work:
public void start() throws IOException {
URL url = getClass().getResource("res/base.xml");
if (url != null) {
System.out.println(url.getPath());
System.out.println(url.getFile());
System.out.println("File exist!");
try(InputStream result = url.openStream()) {
try(Scanner scn = new Scanner(result)) {
while(scn.hasNext()) {
System.out.println(scn.next());
}
}
}
} else {
System.out.println("Not working! :(");
}
}
After deeper searching looks like I found a reason - https://stackoverflow.com/a/10605316/1117515.
In this case I need to use getResourceAsStream().

Categories