问题
My code reads
package com.fyp.jwi;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import edu.mit.jwi.Dictionary;
import edu.mit.jwi.IDictionary;
import edu.mit.jwi.item.IIndexWord;
import edu.mit.jwi.item.IWord;
import edu.mit.jwi.item.IWordID;
import edu.mit.jwi.item.POS;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
----> String wnhome = "file:///android_asset/"; <----
String path = wnhome + File.separator + " dict ";
URL url = null;
try{ url = new URL("file", null, path); }
catch(MalformedURLException e){ try {
e.printStackTrace();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} }
if(url == null) return;
// construct the dictionary object and open it
IDictionary dict = new Dictionary ( url);
try {
dict.open ();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// look up first sense of the word "dog "
IIndexWord idxWord = dict . getIndexWord ("dog", POS. NOUN );
IWordID wordID = idxWord . getWordIDs ().get (0) ;
IWord word = dict . getWord ( wordID );
System .out . println ("Id = " + wordID );
System .out . println (" Lemma = " + word . getLemma ());
System .out . println (" Gloss = " + word . getSynset (). getGloss ());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
The code which I have highlighted using the '-->' was originally
String wnhome = System.getenv("WNHOME");
Where WNHOME is an environment variable that refers to the directory where certain files to help a Java wordnet api are located on your computer. This works fine when I compile the code. I need a standalone Java program on my pc.
I have attached an image to make things clearer
Now I know how to set and use environment variables on windows. How to do it on android? And am I bundling these files the right way? Is there another way to do it?
OR is there a workaround that doesn't involve setting an environment variable? Anyone familiar with this or the JWI help me out ?
来源:https://stackoverflow.com/questions/21642596/regarding-the-assets-folder-and-environment-variables-on-android