Cannot resolve symbol 'IOUtils' - java

I have used the following code to create a temporary file in my android app:
public File streamToFile (InputStream in) throws IOException {
File tempFile = File.createTempFile("sample", ".tmp");
tempFile.deleteOnExit();
FileOutputStream out = new FileOutputStream(tempFile);
IOUtils.copy(in, out);
return tempFile;
}
Now the problem is Cannot resolve symbol 'IOUtils'. I did a little bit of googling and discovered that for using IOUtils I need to download and include a jar file. I downloaded the jar file from here(commons-io-2.4-bin.zip). I added the jar named commons-io-2.4.jar from the zip to my bundle and when I tried to import it using:
import org.apache.commons.io.IOUtils;
It is showing error Cannot resolve symbol 'io'. So I tried to import it like:
import org.apache.commons.*
But still I am getting the error Cannot resolve symbol 'IOUtils'.
Question 1 : Why am I getting this error? How to resolve it?
Question 2 : Is there any way to create a temp file from an InputStream without using an external library? Or is this the most efficient way to do that? I am using android studio.

For Android Studio:
File -> Project Structure... -> Dependencies
Click '+' in the upper right corner and select "Library dependency"
In the search field type: "org.apache.commons.io" and click Search
Select "org.apache.directory.studio:org.apache.commons.io:<X.X>
Or
Add implementation 'org.apache.directory.studio:org.apache.commons.io:2.4 to the build.gradle file

Right clicking on the commons-io-2.4.jar file in project navigator and clicking 'Add to project' solved the issue.

Adding below dependency solved issue
implementation 'org.apache.directory.studio:org.apache.commons.io:2.4'

For those who encountered this while working on a Netbeans Java project. What I did is I downloaded the Binaries Zip of IO here
Commons IO Util Jar Download Here
Then Right clicked on my Project>Properties>
On the Categories Pane, Click Libraries
Then Click Add Jar/Folder
Locate the jar file/folder of the extracted IO Util that was downloaded.
Then click Ok. That fixed mine. :)

Related

Can't resovle a class in jar that already added in my project

As you can see from the pic, StdOut is a error for can't be resolved. But when I click it with holding Ctrl, I can still open the StdOut.class(Also in pic,you can see it) And StdOut class is in stdlib.jar which is in Referenced Libraries in left of pic. I just want to use StdOut.class. What should I DO?
Try adding the jar as an external jar
BuildPath>>Configure BuildPath >>Libraries>> Add External Jar
Also try importing the full path of StdOut before using it.
I found a stupid solution: find the StdOut.class and copy the content from it, make a new package in my project and paste it. Although it's stupid, but this time StdOut.class can be resolved (the Eclipse can resolve my knew package).

CLOSED - How do I import another class not in Java Library

I have this code in my java project, which reads a file and converts it into a string.
String txt = FileUtils.readFileToString(text);
It uses this class https://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/FileUtils.html
How do I import this into my project?
Thanks :)
If you are using Ant as a build in tool then below solution works,
Step - 1: download .jar file from here,
Step - 2: Then after add it into your class path likewise,
Project right click -> properties
Step 3 : find Jar from you machine, and add it to your class path. likewise,
Click -> OK.
Now, Your problem has been resolved.
First of all you are looking for deprecated method. I suggest you should not use deprecated methods if possible.
Secondly, if you just want to get content of file in String, you can do it in following way with java.nio.file.Files and without using any third party library.
File file = new File("abc.txt");
String txt = new String(Files.readAllBytes(file.toPath()));
Include commons-io jar in build path of your project by downloading it from Apache site -
https://commons.apache.org/proper/commons-io/download_io.cgi
Try following what Rene said here:
Add commons-io dependency to gradle project in Android Studio
You can also try to drag the jar file under Jar folder of lib after downloading it, then right click on the Jar file and select the "Add as library" option. Then select your app from the dialog box.

Add JLayer Library Eclipse

I have a problem with JLayer Library on Eclipse. With JDev it works fine but I have to work in Eclipse for a project and I can't install this Library.
I did Builds Path -> Add Library and import JLayer.zip
As you can see I can't import Javazoom. Thanks for your help
You need to unpack the JLayer zip file to extract the jar file contains the Java class files (jl1.0.1.jar). Use this jar file in the build path as an external jar instead of the zip file.
Also 'import' statements come before the class file definition:
import javazoom.jl.player.AudioDevice;
public class MyClass
{
....
}

How to delete a file inside zip file in android

I want to delete a file from a zip file without extracting it in android. I first did it with java using the following code
Path zipFilePath = Paths.get(filePaths); //gave the path of the zip with zip file name
try( FileSystem fs = FileSystems.newFileSystem(zipFilePath, null) ){
Path pathInZipfile = fs.getPath(reVsl3); //reVsl3 String which holds the name of the file to be deleted in the zip file
zipDelLoc=reVsl3; //just assigning it for future use
System.out.println("About to delete an entry from ZIP File" + pathInZipfile.toUri() );
// Execute Delete
Files.delete(pathInZipfile);
System.out.println("File successfully deleted");
}
catch (IOException e) {
System.out.println(e+"Error here?");
}
This is working perfectly in netbeans, but its not working in ADT, the error occured in logcat
java.lang.NoClassDefFoundError: java.nio.file.Paths
Isearched for a resolution in and got a hint that android does not have ' java.nio.file.Paths' this thing,
I am looking for an alternate solution can zip4j or TrueZip will do the trick here,
i tried with truezip, but with this code
TFile archive = new TFile("archive.zip");
for (String member : archive.list())
System.out.println(member);
but archive.list() is returning null, but archive.length is returning its correct file size.
I dont know what i am doing wrong in here, i downloaded truezip 7.7 all in one jar. but i am getting an error when i give this import de.schlichtherle.io.File
please help
What version of Java are you using?
Double-check your import. It should be something something like de.schlichtherle.truezip.nio.file.TPath; When working with TrueZip, you need to import java.nio.paths even if you aren't using it and instead using truezip's flavor of paths.
Make sure you're including all the proper dependencies. I'm assuming you're using Gradle, so the dependency would look something like this:
'de.schlichtherle.truezip:truezip-file:7.7.9'
I handled a similar error with importing dependencies in a Java project using Maven. Try these things and provide your Java version.

Java - Rome rss reader?

I am trying to read rss.
I copied the jar file in to my libs folder, and I added the jar file to my eclipse project as a library.
In order and export i checked my jar file.
Now I am trying to use the rss reader provided by rome
import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.io.SyndFeedInput;
import com.sun.syndication.io.XmlReader;
URL url = new URL("http://myUrlGoesHere");
XmlReader reader = new XmlReader(url);
SyndFeed feed = new SyndFeedInput().build(reader);
I get the following error on the last line:
The type org.jdom.Document cannot be resolved. It is indirectly referenced from required .class file
What does this mean? What is the solution? * no error on imports *
In order to get it to compile I had to use the jdom-1.1.3.jar file from http://www.jdom.org/dist/binary/archive/. After the zip file is extracted it can be found in the directory jdom-1.1.3\jdom\build.
You will need to right click on your project| Select Properties | Java Build Path | Libraries | Add External JARs.
In addition to copying Rome's jar file to your libs folder, you also need to add JDOM's library (jar) to your class path.
The error you are getting says that someone is indirectly referencing JDOM's jar. Probably someone on Rome or any other library you don't control.

Categories

Resources