I want to acess class file from bin folder in android.
I was doing it using
File f = new File("/bin/filename.class");
Its working fine in java but in android giving path doesnt work, so
please suggest me other way to access class file of any java file in android.
You can't access file like
File f = new File("/bin/filename.class");
from bin folder. Android can't recognize this path.
Java .class files are converted to Dalvik Executable (.dex), in your apk there won't be any .class files, just one single dex file called classes.dex.
I think this post from Android Developers blog might help you: http://android-developers.blogspot.com/2011/07/custom-class-loading-in-dalvik.html
Related
I want to convert a wav file into flac on Android platform using Unity3d Game Engine. So I downloaded the latest version of javaFlacEncoder and converted the .jar file into .dll using ikvmc.exe so I can use its functionality in a C# script. Then I put the DLL file into Plugins/Android folder. But it gives the following error:
Assets/Core.cs(70,3): error CS0012: The type 'java.lang.Object' is defined in an assembly that is not referenced. Consider adding a reference to assembly 'IKVM.OpenJDK.Core, Version=7.2.4630.5, Culture=neutral, PublicKeyToken=13235d27fcbfff58'
Corresponding line in Core.cs:
FLAC_FileEncoder flacEncoder = new FLAC_FileEncoder();
How can I fix it? If there is a way to use the .jar file directly, I'm also okay with that.
Fixed it by copying IKVM.OpenJDK.Core.dll to the same folder (Plugins/Android).
I want to read a file that is saved in my java folder (same where my activities are) in package com.example so it's java-->com.example--> data.ser. However I'm getting java.io.FileNotFoundException: /java/com/example/data.ser: open failed: ENOENT (No such file or directory) when I call
ObjectInputStream in = new ObjectInputStream(new FileInputStream(new File("java/com/example/data.ser")));. I tried removing the java part or just leaving the file name on its own but nothing works. I am not loading it from external/internal storage but just from within the app. How to do it? I'm using android studio. Thanks for help
Files has to be stored on 'assets' or 'raw' folder, depending on what kind of file you are going to store. On 'java' you should set code class only. You can find an easy example here:
raw file example
I have a audio file stored in a folder within the project and I'm trying to call the sound file within the folder.
I am able to call the file using the full URL
new java.net.URL("file:C:\NetBeansProjects\Puzzle\audio\applause2.wav"));
But is there a way to shorten it so that if I move the project I don't require changing the code?
Thanks
File file = new File("audio\applause2.wav");
Assuming you keep the audio folder at the same level relative to the jar
How would you read a file into a program that's compiled into a jar next to it through its local directory? The type read would be a simple .txt file.
It depends on what the usage of the program is. Do you know how the jar is supposed to be executed? When you try to run it, does it spit out a "usage: somejar firstarg secondarg" type message?
Also, if its a jar that you've compiled and you know how it should be executed, then you may have forgot to set its main class or manifest.
Check this: http://www.mkyong.com/java/how-to-make-an-executable-jar-file/
If you want to read a file that exists within an external .jar file, you will need to unzip the .jar file first in your code and then retrieve the file. You can do this using Java's zip APIs. See this answer if this is the case: Easiest way to unpack a jar in java
If you want to read a file that is in the same .jar file that your code is executing, you can get the file as a resource. See this answer: Get a resource using getResource()
If the file is simply in the exact same directory as the executable .jar, create a new file like so:
File input = new File("myfile.txt");
I had problems while finding the path of file(s) in Netbeans..
Problem is already solved (checked answer).
Today I noticed another problem: When project is finished,
I have to execute the generated .jar to launch the program, but it doesn't work because an error occurs: NullPointer (where to load a file) when accessing/openning jar outside Netbeans.
Is it possible to open a file with the class file in Java/Netbeans which works in Netbeans and even in any directory?
I've found already some threads about my problem in site but none was helpful.
Code:
File file = new File(URLDecoder.decode(this.getClass().getResource("file.xml").getFile(), "UTF-8"));
The problem you have is that File only refer to files on the filesystem, not files in jars.
If you want a more generic locator, use a URL which is what getResource provides. However, usually you don't need to know the location of the file, you just need its contents, in which case you can use getResourceAsInputStream()
This all assumes your class path is configured correctly.
Yes, you should be able to load a file anywhere on your file system that the java process has access to. You just need to have the path explicitly set in your getResource call.
For example:
File file = new File(URLDecoder.decode(this.getClass().getResource("C:\\foo\\bar\\file.xml").getFile(), "UTF-8"));