I know this question was asked earlier but there is no proper solution anywhere right now so i am going to ask it again.
How can i load an owl file in my android project?
The code works in java but they are useless in android . When i try them in an android project then the file can not be accesed . I am using OWLApi 3.4.10.
I am loading the ontology from my mainActivity class. The loading is performed in loadOntology method in OntologyClass class.
In main method the coding is as
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ontologyClass ontology;
ontology = new ontologyClass();
try {
ontology.ontologyLoad();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
The coding of ontologyClass is as:
#Ignore
#SuppressWarnings("javadoc")
public class ontologyClass {
OWLOntology pizza;
OWLOntologyManager manager;
public ontologyClass ontologyLoad() throws OWLOntologyCreationException {
manager= OWLManager.createOWLOntologyManager();
File file= new File("assets/Pizza.owl");
pizza = manager.loadOntologyFromOntologyDocument(file);
return this;
}
The pizza.owl file is inside the assets folder.
In logcat i receive warnings like
FileNotFountException: /assets/Pizza.owl: open failed:ENOENT (no such file or directory)
can anyone fix this file loading problem?
Thanks
Tackling Build path error:
Normally if you are using owl api in java then all you need is just to import the owl api library. But in android if you do only this you will still get error stating "method not found". So you need to perform a second step i.e right click your android project and then
properties-> Java Build Path -> order and export tab
and there check mark the OWLAPi 3.4.10.jar
the answer for the file path in assets folder is as follows:
The directory/path of theowl file in asserts folder can be accessed via InputStream class the File class doesn't work for this folder, so instead of using
File file= new File("assets/Pizza.owl");
use this code
InputStream is= myContext.getAssets().open("Pizza.owl");
and finally use the InputStream instance is, this represents the correct path of the file in assets folder ie
pizza = manager.loadOntologyFromOntologyDocument(is);
Related
In my project, I have created a package that am calling htmlRepository. In this package, I have created an Html file called mapping.html. I would like to access this file, convert it to a url so I can use the setPage method to diplay it in a JEditorPan.How can I access this file in order to get its url.
I have used the code below to do this if the file resides outside the project package i.e on some folder on my computer.
NodeName = Node;
try {
NodeURL = new File(filename).toURI().toURL();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
However, this means that when I create the jar then I will have to have another folder with the HTML.So I created the Html package so I can carry the Html files in the jar and access them from there. How can I go about this. Thank you in advance.
You should load it as a resource file using the context class loader as so:
URL url = Thread.currentThread().getContextClassLoader().getResource(fileName);
and by filename you should include the full nested directories before your file starting from the resources dir
I can't seem to find a solid answer for this specific question.
I'm trying to create a symbolic link programmatically of a directory in my assets folder in another location within the same application's asset directory. Essentially, I'm looking to do the same thing as what the createSymbolicLink method of Java.nio.Files would do.
Is there an available way of doing this with the Android SDK? If not, is it possible in the NDK?
For Android API 21 and above, just use:
Os.symlink(originalFilePath,symLinkFilePath);
There is no public API to do this. You can however use some dirty reflection to create your symbolic link. I just tested the following code and it worked for me:
// static factory method to transfer a file from assets to package files directory
AssetUtils.transferAsset(this, "test.png");
// The file that was transferred
File file = new File(getFilesDir(), "test.png");
// The file that I want as my symlink
File symlink = new File(getFilesDir(), "symlink.png");
// do some dirty reflection to create the symbolic link
try {
final Class<?> libcore = Class.forName("libcore.io.Libcore");
final Field fOs = libcore.getDeclaredField("os");
fOs.setAccessible(true);
final Object os = fOs.get(null);
final Method method = os.getClass().getMethod("symlink", String.class, String.class);
method.invoke(os, file.getAbsolutePath(), symlink.getAbsolutePath());
} catch (Exception e) {
// TODO handle the exception
}
A quick Google search showed this answer if you don't want to use reflection: http://androidwarzone.blogspot.com/2012/03/creating-symbolic-links-on-android-from.html
I recently just start to using Android Studio for my programming study.
I've faced a problem today which is keep getting "null" when I using getResourceAsStream to read a properties file from JUNIT TEST.
I used to locate the .properties file under "src" directory when I was using Eclipse. But in Android Studio, it won't work.
Here is the code that part of a class called BeanFactory,:
private static Properties properties;
static {
properties = new Properties();
InputStream is = BeanFactory.class.getClassLoader().getResourceAsStream("bean.properties");
try {
properties.load(is);
}catch (IOException e) {
e.printStackTrace();
}
}
In Eclipse, I located the the prop file under src, and it works just fine.
But in Android Studio, I've tried put the "bean.properties" file in several different directory. Such as /src, or/src/main/java, nothing worked out.
Where should I put the prop file? Is there any configurations that I should do for this?
Placing resources into assets is not acceptable in some cases. I solved this problem by packing required resourses into special jar file and placing this jar into libs directory.Thus you still can access them via standard Java technique.
There are two issues. In general resources are stored in a jar, with the compiled .class files.
getResource and getResourceAsStream are methods of a class. The resource is searched in the jar of this class (in general). So with inheritance getClass().getResource("...") might be dangerous.
The second, more grave pitfall is, that the path is relative to the package (directory) of the class, unless you use "/...".
Another way is to use ResourceBundle instead of Properties. ResourceBundle has a PropertiesResourceBundle for *.properties.
ResourceBundle properties = ResourceBundle.getBundle("bean");
try this:
1.put your own bean.properties file in app/src/main/assets
2.change you code to:
private static Properties prop;
static {
prop = new Properties();
try {
InputStream is = IWorkApplication.getInstance().getResources().getAssets().open("bean.properties", Context.MODE_PRIVATE);
prop.load(is);
} catch (IOException e) {
e.printStackTrace();
}
PS.IWorkApplication.getInstance() is define in my own application class.
Hope to be useful
Hi i have exported my java project as executable jar file. inside my project I am accessing a Excel file containing some data. Now I am not able to access the Excel file when I am trying to access the file.
My project structure is:
Java_Project_Folder
- src_Folder
- resources_Folder(Containing excel file)
I am accessing the excel file like
FileInputStream file=new FileInputStream(new File(System.getProperty("user.dir")
+File.separator+"resources"+File.separator+"Excel.xlsx"));
I have tried accessing this file using getResourceAsStream like:
FileInputStream file=(FileInputStream) this.getClass().getResourceAsStream
("/resources/Excel.xlsx");
But i am getting in is null exception. whats wrong can anyone help?
I bet you have no package called resources in your project.
Trying to use Class.#getResourceAsStream is the way to go. But this method does not return a FileInputStream. It returns an InputStream wich is an interface.
You should be passing the absolute name of the resource
InputStream is = getClass().getResourceAsStream("my/pack/age/Excel.xlsx");
where the excel file is located in the directory
resources/my/pack/age
The first step is to include the excel file itself in your project. You can create a resources folder like you show, but to make sure this gets included in your jar, you add the resources folder in along with your source code files so that it gets built into the jar.
Then
InputStream excelContent = this.getClass().getResourceAsStream("/resources/Excel.xlsx");
should work. From one post at least, the leading forward slash may also mess things up if you use the ClassLoader.
getClass().getResourceAsStream("/a/b/c.xml") ==> a/b/c.xml
getClass().getResourceAsStream("a/b/c.xml") ==> com/example/a/b/c.xml
getClass().getClassLoader().getResourceAsStream("a/b/c.xml") ==> a/b/c.xml
getClass().getClassLoader().getResourceAsStream("/a/b/c.xml") ==> Incorrect
ref: getResourceAsStream fails under new environment?
Also in eclipse you can set the resources folder as a source folder like this:
in the properties of your eclipse project, go to java build path, select sources, and check to see if all needed source fodlers are added (as source folders). If some are missing, just add them manually using add sources... button
ref: Java Resources Folder Error In Eclipse
I tried this and it is working for me.
My Test1 class is in default package, just check where your accessing class is in any package, if it is then go back to exact resource folder from classpath like this "../"
public class Test1 {
public static void main(String[] args) {
new Test1();
}
Test1(){
BufferedInputStream file= (BufferedInputStream) this.getClass().getResourceAsStream("resources/a.txt");
try {
System.out.println((char)file.read());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
FileInputStream file= (FileInputStream)
this.getClass().getResourceAsStream("/resources/Excel.xlsx");
Why do you need FileInputStream? Use
InputStream is = getClass().getResourceAsStream..
Secondly use "resources/Excel.xlsx"
Thirdly when constructing file like this
new
File(System.getProperty("user.dir")+File.separator+"resources"+File.separator+"Excel.xlsx"));
is hard to control slashes. use
new File("parent (userdir property)", "child (resources\Excel.xlsx)")
I've been having problems exporting my java project to a jar (from Eclipse). There is a file that I've included in the jar named images. It contains all of the image files used by my project. The problem is, my references to those images only work when the project isn't in jar form. I can't figure out why and I'm wondering if I need to change the wording of the image references. This is what I'm doing to reference images:
URL treeURL = null;
Image tree = null;
File pathToTheTree = new File("images/tree.png");
try {
treeURL = pathToTheTree.toURL();
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
tree = ImageIO.read(treeURL);
} catch(IOException e) {
e.printStackTrace();
}
Most likely it is a simple problem, as I'm a beginner at coding. What do I need to change to make these references work when it's all in a jar?
It is indeed simple: you use the various getResource() methods in java.lang.Class and java.lang.ClassLoader. For example, in your app, you could just write
treeURL = getClass().getResource("/images/tree.png");
This would find the file in an images directory at the root of the jar file. The nice thing about the getResource() methods is that they work whether the files are in a jar or not -- if the images directory is a real directory on disk, this will still work (as long as the parent of images is part of your class path.)