NoClassDefFoundError using Eclipse/Android - java

I've been trying to use Thrift in my android app to handle RPC's with a server. I add the thrift .jar file to my class path (Properties > Java Build Path > Libraries). I get no problems during compilation but during runtime I get a NoClassDefFoundError when using anything from the jar.
Do I have to do anything else to tell eclipse/android where libraries are located?
Cheers!
TSocket transport = null;
try {
transport = new org.apache.thrift.transport.TSocket(sock); <--- OFFENDING CODE
} catch (TTransportException e1) {e1.printStackTrace(); }

Please copy the jar file to project then add it to build path. I think you added from external path so it was not added in apk file.

Related

Spark-java how to import keystore file with path inside jar

I have an Spark-java application that has a couple of endpoints. Now i created a keystore file with a SSL certificate for my domain.
This is how i added it in spark:
secure("src/deploy/letsencrypt.jks", "mysecretpassword", null, null);
If i run my application from inside intteliJ it is able to run, but once i package my application in a jar file it can't find the keystore file. I have searched a lot on the web and the answer to the question is: use an inputstream. Now spark only lets you give an path, not an inputstream.
Is there a way to still access a keystore file from within a jar, just by specifying an path? Or is this not possible atm with spark?
When you package an application in a jar file, the src path could not be found because it doesn't exist anymore.
In Intelij IDEA / Project Structure windows / Modules section you can find Resource directory. Every file/folder you put in Resource directory, would be copied in Jar file.
But hard coding keyStorePath, keyStoreKey in your code is not secure. I suggest set these variable as parameters in run-time. something like this code:
String keyStorePath = System.getProperty("app.httpskey.path");
String keyStoreKey = System.getProperty("app.httpskey.key");
if (keyStoreKey != null && keyStorePath != null) {
try {
secure(keyStorePath, keyStoreKey, null, null);
Logger.log("Web server will started in HTTPS mode.");
} catch (Exception e) {
Logger.log(e);
}
}

Get directory path of maven plugin in its own Mojo

I am creating a custom maven plugin. In one of its Mojos, I am reading a Xpp3Dom object from XML file using following code piece:
File pluginsFile = new File(
"absolute-path-to-file/plugins.xml");
Xpp3Dom Xpp3DomObject = new Xpp3Dom("plugins");
try {
FileReader reader = new FileReader(pluginsFile);
Xpp3DomObject = Xpp3DomBuilder.build(reader);
reader.close();
} catch (Exception e) {
// TODO throw exception
}
The XML file from which I am reading (plugins.xml) is stored in src/main/resources of the maven plugin itself. My question is, how do I point to that XML file without explicitly stating the absolute path to that file?
To be clear: I want this file to be under the directory of my maven plugin. It cannot be outside the maven plugin as it is a necessary part of the maven plugin and should not be changed by other maven projects that consume this plugin.
I have searched for a variable/method in Maven Mojo that would give me the absolute location of the maven plugin itself. If I get that, then I can just give the location as value-of-that-variable/src/main/resources/plugins.xml. But I am unable to find such variable. I have also tried for a way to pass properties from Maven plugin POM to one of its Mojos so that I can pass project.build.directory, but cannot find a way.
To be clear: I want to access a file that is under the maven plugin directory, in one of its Mojos.
Any suggestions will help.
I think the easiest form to read some of the own plugin's resources files is through the getResourceAsStream() API, because sooner or later, your plugin will be delivered as a JAR, and then the src directory will dissapear and there will remain only classpath resources:
try (InputStream input=getClass().getClassLoader().getResourceAsStream("plugins.xml")){
try(Reader reader=new InputStreamReader(input));
{
Xpp3Dom Xpp3DomObject = Xpp3DomBuilder.build(input);
} catch (Exception e) {
...
}
}
Anyway, in this way there is a risk that some other JAR of the classpath should contain a plugins.xml file by chance. To avoid (or at least reduce) this risk, you should package it:
src\
main\
resources\
foo\
bar\
MyMojo.java
plugins.xml
... and in this case, you must read it through getClass().getResourceAsInputStream().

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.

Unable to load .so file

I have an application which uses one native library "libSample.so" which is depend upon another .so file.I am trying to load that library using following code
File File1 = new File("libSample.so");
static
{
try {
System.load(File1.getAbsolutePath());
} catch (UnsatisfiedLinkError e) {
System.out.println("Link Error");
}
}
Before loading library I have tried setting up LD_LIBRARY_PATH where the library is located using command line.
export LD_LIBRARY_PATH=/home/usb:${LD_LIBRARY_PATH}
But still the library not get load.
What should I do now?
Please help.
static {
System.loadLibrary("libSample.so");
}
I assumed that you have your jars in /libs directory and .so file in /libs/armeabi directory so the system finds them. You do not have to add .so files in your eclipse build path.

How to read file with different canonical paths

I am making an java application which reads a file from a particular location. The location of the file is in the folder retrived from
getCanonicalPath().
The problem i am facing is that when i am running my application in Eclipse the canonical path is different from the one which Dr Java sees. So, what should i do before delivering my application to the client to make sure that it sees the file no matter which ide/command prompt is used to run the application. Obviously it would not be a good idea to copy the same file across all possible folders to cover different possibilities of getCanonicalPath.
Thanks
One of the solution is to have this file in your classpath and load it from your classpath, with a code like
URL url = getClass().getClassLoader().getResource(path);
if(url != null) {
try {
return new File(url.toURI().getPath());
} catch (URISyntaxException e) {
return null;
}
}
This is standard if this file is a configuration file. Usually in a standard java project layout you put this in the folder src/main/resources.
If this is more of a data file, you should put in a configuration file its path, and have different configurations, one for your station and one for production on the client machine. Of course in this case the configuration file is in the class path ;).
A common solution is to place the file in a directory which is in the class path. If you use getResource or getResourceAsInputStream you can find the file regardless of where it is provided its in the class path. if you use maven you can be sure how the classpath is setup regardless of the IDE used.
You should always load file ClassLoader using API like Test.class.getClassLoader().getResource(name),Test.class.getClassLoader().getResourceAsStream(name) More Information available here

Categories

Resources