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.
Related
I'm trying to export a java project in eclipse as a runnable jar, but for some reason the runnable jar doesn't work. If I double click the executable jar, it doesn't do anything. I tried both extract and package required libraries into generated jar.
So I also tried to export some simpler projects, those worked fine. The biggest difference is my real project has files: images and xml files.
In code reference them like this:
File file = new File("Recources/test.xml");
ImageIcon imageIcon = new ImageIcon("Recources/" + num + ".gif");
The structure of the project looks like this:
But in the executable jar they look like this:
Thank you for your help.
Edit:
I have tried the 'java -jar filename.jar', but now it says it can't find my resources folder, while in eclipse it can still find it.
Files in a JAR-File aren't just like files stored in your hard-disc. If you include files in a JAR, they'll be seen as a Stream of Bytes. So you have to use different methods to access these resources.
//To read/access your XML-File
BufferedReader read = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("/test.xml")));
//To read/access your gif-Files
ImageIcon icon = new ImageIcon(this.getClass().getResource("/"+num+".gif"));
"/" is not the root-Folder of your file-system, but the root folder of the resources inside your JAR.
The issue may be that Java is not the default program to run the jar.
Try right click -> Open with, and select the Java Runtime, and it should run successfully.
Make it the default program to enable double-click running.
Right click -> Properties -> Change -> C:\Program Files\Java\jre7\bin\javaw.exe
Inspired by stratwine's answer at https://stackoverflow.com/a/8511277
So thank you all, but it seems like the problem wasn't the export only. There was an error I saw when I opened my program with cmd, I was using file name to open xml and images while I should have used inputStreams: https://docs.oracle.com/javase/tutorial/networking/urls/readingURL.html.
Where is the R.java file in Android Studio? Can someone please help with this? I already tried google for the answer but can't find any solution.
Based on the new stable release of Android Studio (3.6) we have:
So, now to find your generated resource classes you need the following steps:
1) Open your project.
2) Go to your module build path.
3) Open the outputs/apk/debug/app-name-debug.apk file.
4) Choose your classes.dex file.
5) Look at the down placed area and go to your full package path.
6) You can see all bytecoded resource classes. So, scroll down to what you are looking for.
7) Expand the resource class you need to proceed (for example, let it be R.id)
8) Go to you id's.
That's it.
UPDATE:
If you would like to see actual id integer number you should follows steps below:
1) By (7) Go to the resource class you need to proceed and right click to show the context menu
2) Choose "Show Bytecode" to see the flexible dialog "DEX Byte Code for R$id", for example, for id class
3) Scroll down to the actual id to look its number
Although the current Android Gradle Plugin doesn't generate a R.java anymore you can still inspect the corresponding class file (see Sergey V.`s answer).
However, if you (like probably in most cases) just want to look up the generated IDs for your resources, there is an easier way:
In the project pane on the left hand side switch from Android to Project view using the drop-down at the top. Then navigate to app/build/intermediates/runtime_symbol_list/<insert build type here>/R.txt. This file lists all IDs generated by AGP during the build process.
I use Android Studio 3.3.2
\app\build\generated\not_namespaced_r_class_sources\debug\processDebugResources\r\android\support\constraint\R.java
As noted above, "The new Android Gradle Plugin generates the corresponding bytecode directly and does not make the intermediate R.java file".
Using the ubuntudroid solution you can find an id also this way. Find R.txt and copy absolute path. Project > app > build > intermediates > runtime_symbol_list > debug > R.txt. Then right click and select Copy Path..., 1. Absolute Path Ctrl+Shift+C.
This way you will obtain a path to R.txt, for instance, "C:\Users\user\AndroidStudioProjects\your_project\app\build\intermediates\runtime_symbol_list\debug\R.txt". Copy the path without R.txt to a text file for future references, for example, with Notepad++.
Press Ctrl+Shift+F to find in directory. Insert an id and the path.
== Old answer ==
Change a project view from Android to Project or Project Files. Find one of these two files.
Then you can revert back to Android view.
I use Android Studio 2.3.3. The R.java file is shown in the picture above.
In the left upper side, there is a select list. Select the [project] option so that you can see all the folders and files.
R.java is the generated file by ADT or Android studio. It will be located under app\build\generated\source\r directory.
./app/build/generated/source/r/debug/android/support/v7/appcompat/R.java
./app/build/generated/source/r/debug/your/packagename/name/R.java
I am using Android Studio 2.2.3 , the R.java file is shown in the picture below.
Goto in the left upper side there is a selecting list ,
select [package]
Open "App Folder"
com.example.saeedanwar.myapplication;
r
In my project in Android Studio 4.1
R.jar that you can actually unzip resides in app build folder, as follows:
jar -xf R.jar
Once the jar is unpacked you can find R.java inside of app package:
If you're using Mac or Linux, try the following command line on your terminal:
find . -name "R.*"
It will print something like:
./app/build/intermediaries/runtime_symbol_list/debug/R.txt
./app/build/intermediaries/compile_and_runtime_not_namespaced_r_class_jar/debug/R.jar
It is inside app\build\generated\source\r folder
Project/app/build/generated/source/r/debug/com.android.'projectname'/R.java
--The R.Java that contains your xml layouts and views id's in Android Studio 3.5.3 -- Project/app/build/generated/not_namespaced_r_class_sources/debug/r/com/"your package"/R.java
This answer will be in a constant state of flux until Android standardize a method of matching their annoying decimal/hexadecimal id references to a tangible resource such as my_layout.xml.
For Android Studio Dolphin | 2021.3.1 Patch 1 have a look at this file:
/project/package/build/intermediates/stable_resource_ids_file/debug/stableIds.txt
It is produced by RUN (not just BUILD). Sample:
layout/mtrl_picker_header_fullscreen = 0x7f0d00de
string/cancel = 0x7f1101f0
style/ThemeOverlay.MaterialComponents.MaterialCalendar = 0x7f1202f3
id/gpxBtn = 0x7f0a0346
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. :)
ShowcaseView is contained in a Zip file here. But what exactly am I downloading and how do I import it in Eclipse?
After 2 days I succeed, I have created steps to import ShowcaseView in Eclipse that may be helpful to you.
How to use Showcase View in Eclipse.
Download ShowcaseView from github and extract.
Now Follow step for import.
- Right click on Project Explorer
- Import -> Android -> Existing Android Code Into Workspace
- Select Extracted "ShowcaseView-master"
- Check both project "library" and "MainActivity"
- Check "Copy projects into workspace"
- Finish.
(After importing you can see 2 projects imported namely "main" and "SampleActivity", you may change "main" as "libShowcaseView" and "SampleActivity" as "ShowcaseViewDemo" so you can easily identify. :) )
May you see error in "ShowcaseViewDemo" (Dont worry...), See src/ folder is also blank (in both "libShowcaseView" and "ShowcaseViewDemo") :) right??? Dont mind.
Make following packages in "libShowcaseView" project. See Example.
- com.github.amlcurran.showcaseview
- com.github.amlcurran.showcaseview.targets
and Move all java files from /java/com/github/amlcurran/showcaseview and /java/com/github/amlcurran/showcaseview/targets Respectively.
Make following packages in "ShowcaseViewDemo" project. See Example.
- com.github.amlcurran.showcaseview.sample
- com.github.amlcurran.showcaseview.sample.animations
- com.github.amlcurran.showcaseview.sample.legacy
- com.github.amlcurran.showcaseview.sample.v14
and move files as above (from java folder respectively).
Now right click on "libShowcaseView" project->properties->Android->Move down and check "Is Library" then add "actionbarsherlock" as Library. See Preview
Now Add as library in ShowcaseViewDemo. Goto properties of "ShowcaseViewDemo"->Android->Add->select "libShowcaseView"->Ok...
I think you didn't ran into error now. You can Run.
You can ping any time for any suggestion or help.
Happy ShowcaseViewing...
Easiest way is:
Create an empty Android Application project in your Eclipse workspace.
Copy files in ShowcaseView/library/ folder of github to your new project base folder.
Refresh project in Eclipse.
Goto project properties - Android and select "Is Library"
Clean unnecessary stuff and build.
It should be ready to add as dependency of your app.
Alternatively you can pick another library project .project file and copy it inside this library folder, edit it to match your new project name, etc... And then import in Eclipse. But this is more hardcore if you don't know what you're doing.
Soure : How do I build these libraries from GitHub
Following ツ Pratik Butani ツ's Answer and use Android Support Library to replace ActionBarSherlock
In project.properties of libShowcaseView and ShowcaseViewDemo
add target=android-21
Right click on "libShowcaseView" Properties->Android->Library->Add... select AppCompat(android support library v7)->Ok
In styles.xml of ShowcaseViewDemo
Change
android:Theme.Holo.Light, Theme.Sherlock.Light, Theme.Sherlock.Light.DarkActionBar to Theme.AppCompat
Remove <item name="android:fontFamily">sans-serif-light</item> in ListItemBigText
In ActionItemsSampleActivity.java
change SherlockActivity to ActionBarActivity, getSupportMenuInflater to getMenuInflater.
In MultipleActionItemsSampleActivity.java
change SherlockActivity to ActionBarActivity
change com.actionbarsherlock.app.ActionBar.OnNavigationListener to android.support.v7.app.ActionBar.OnNavigationListener
changegetSupportMenuInflater to getMenuInflater
Do it in there steps:
Download ShowcaseView from github`
Create package com.github.amlcurran.schowcaseview and com.github.amlcurran.schowcaseview.targets in your eclipse project.
Extract ShowcaseView-master.zip or rar.
Go to library/src/main/java/ and copy com/github/amlcurran/showcaseview java files in com.github.amlcurran.schowcaseview package.
Do same thing for com/github/amlcurran/showcaseview/targets in com.github.amlcurran.schowcaseview.targets.
Finally, copy others files in differents folder drawables, layouts, value(copy in the old value files, style.xml for example).
Yesterday, I had a problem because I couldn't manage to open a xml file (it owuld give me a FileNotFoundException) located in the ressources folder of my .jar file, which I managed to open on eclipse using the following lines of code. You can see my old problem here. This was my code with the problem :
File xmlFile = new File("ressources/emitter.xml");
ConfigurableEmitter emitter = ParticleIO.loadEmitter(xmlFile);
Someone told me it that one way was to use getClassLoader().getRessourceAsStream method to open a xml file in a .jar file that was exported
InputStream i= this.getClass().getClassLoader().getResourceAsStream("ressources/emitter.xml");
ConfigurableEmitter emitter = ParticleIO.loadEmitter(i);
Unfortunately, that solution only works when I export my project into a .jar file, so if I want to go back debugging my program, I have to take the old code that would only works on eclipse.
My question is: is there any better way to do this without having to change my code if I want to export it or if I want to debug it?
Thank you
edit :
Thank you all, it works perfectly fine now
my problem was that I put my ressources folder like that :
+project
+src
+ressources
+emitter.xml
InputStream i= this.getClass().getClassLoader().getResourceAsStream("/ressources/emitter.xml");
The above should work in both cases (Note is is /resources/.... This is assuming say your directory structure is below:
MyProject
+src
+ressources
emitter.xml
Place the file alongside your source files, then you can use the getResourceAsStream() method in both cases. Don't forget to update the path (which should be the package name of your class, but with slashes instead of dots).
My question is: is there any better way to do this without having to
change my code if I want to export it or if I want to debug it?
Yes, use Maven. Maven will handle that and it hooks into Eclipse beautifully (NetBeans too!) What you do is place the resource in src/main/resources and then you can have Eclipse run the test goal of the Maven project or you can just run mvn test from the command line. Another advantage of using Maven here is that you can also have src/test/resources/emitter.xml which overrides the one in src/main with environment-specific test instructions and it won't affect your deployment.
InputStream i= getClass().getClassLoader().getResourceAsStream("ressources/emitter.xml");
or
InputStream i= getClass().getResourceAsStream("/ressources/emitter.xml");
(note the absolute positioning)
both work when the class is in the same jar, on the same class path.
In the jar the names must be case sensitive, but as the jar already works. Ensure that the ressources directory is on the class path too, or copied to the target directory.
As "ressources" is probably configured yourself (not named "resources" as in English), you probably need to add it to the build somehow.