Using Java Libraries in Android Project - java

I'd like to use a Java library I wrote in an Android project. I did two things:
Add the jar to the build path.
In the package explorer now has a Referenced Library folder
Compile works
I copied the jar into a libs folder within the project
Compile works
But the program crashes during runtime for both approaches. The program does not recognize the class I am calling. A classDefNotFound is thrown.
I have tried this for various jar files. The ones I write do not work. While downloaded jars work. Perhaps there is something wrong with the classes I am writing?
Android Code:
package com.davidk.androidtest;
import com.davidk.libs.Math; //<--classDefNotFound is thrown
import com.parse.Parse; //<--works
import com.parse.ParseObject; //<--works
public void pressButtonOnClick(View v){
int answer = Math.add(1,1);
answerText.setText(answer);
}
public void pingParseButtonOnClick(View v){
ParseObject p = new ParseObject("Ping");
p.put("String", "ping");
p.saveInBackground();
}
davidk.libs.Math code:
package com.davidk.libs;
public class Math {
public static int add(int x, int y){
return x + y;
}
}

I guess you are using Eclipse. Do not try to mess with jars. The easiest way is :
In your library project, check the Is Library in the Android section of the project properties
In your dependant project, add the library in the same place (Android section). Do not add anything in the "Java build path" section ! (this is the most common mistake)

Each jar file exported contains a manifest file, to which the code tries to refer. In your case, I guess the manifest file is missing or corrupted. To generate the jar without any issues or complications, there are many open source tools available, which will help you to achieve this.
Other possible method is as suggested by #Orabig.
Please visit below link for more.
http://docs.oracle.com/javase/tutorial/deployment/jar/manifestindex.html

Related

How do I fix "class file contains wrong class"?

I'm using IntelliJ IDEA and JDK 8.
I have some compiled Java Classes, and I imported them as an external library, as the code I have uses methods inside those classes. However, I can't seem to make actually use those .class files.
Whenever I try to run this code:
public static int spendOnCokes(int sum, DrinksMachineA mach) {
int count = 0;
mach.insert(sum);
try {
while (mach.getBalance() >= mach.getPrice()) {
mach.pressCoke();
count++;
}
} catch (EmptyMachineException e) {
}
It gives me the following error:
Error:(21, 45) java: cannot access com.sanac.DrinksMachineA
bad class file: /homes/jmcv3/Documents/Java/ClassesArchive/com/sanac/DrinksMachineA.class
class file contains wrong class: DrinksMachineA
Please remove or make sure it appears in the correct subdirectory of the classpath.
I've searched around, and some people seemed to suggest that it had something to do with the folder hierarchy/package name. It looks fine to me though. I also tried switching to JDK 5 as that's what the .class file was compiled with, to no avail. (Also read that somewhere.)
Here's a screenshot outlining the directory structure:
All I want is to be able to use those compiled .class files with my code. Seems to be so simple, so I must be missing something really stupid. Any ideas?
The answer is simple: .class files aren't external libraries.
You can play with jar.exe Java tool to export "ClassesArchive" to a .jar file.
File name and public class name should he same. Its a name issue by my perspective.

How to call java classes of a compiled jar file within android studio?

Hello StackOverflow folks,
I have a jar file of java classes. I added this jar file to my android studio project under folder /libs. Now, what I want to do is use those classes within the jar file in MainActiviy.java. I just do not know how.
Some details:
My jar file is named: zombi.jar.
The class within the jar file to call is named: COMBI.class
I tried the following:
In MainActivity.java, I wrote:
// declared class variable
private COMBI mCOMBI;
Then, in OnCreate method, I wrote:
mCOMBI = new COMBI();
//to start calling method COMBIStart to launch the command-line system
mCOMBI.COMBIStart();
I actually called the classes as I would in normal Java. I think Android uses special java code that looks like java, but I don't know how to use them.
I could not get the code to work.
Can you help me?
I'm going to assume you've already written your import statement for the jar class. If you already put the jar file in the /lib folder, Android Studio should update your build.gradle file. Check to see if you have a link to the jar file in your dependencies{...}
If not, you can add it manually.
Just like in normal java you have to add an import to the correct package at the top of your file for everything that's not in the same package the file is in.
It should look something like this:
import android.view.View;
but instead of android.view.view it would be a reference to the class in the jar you're trying to add

Importing libraries in .java tabs in processing

I'm kind of new with Processing 3.*; I am (and willing to) using the Processing Development Environment (The official IDE).
Reading the official "guide", in particular this part, it is specified that you can use pure Java language inside Processing simply naming a .java tab instead of a .pde one.
This kind of solution is good for example to use enums (otherwise not usable in .pde tabs) and there are other reasons, but they are not important at the moment...
A problem with this kind of work is that the libraries of Processing are not included, so you have to import them manually.
What I noticed is that all the official Processing libraries can be imported simply by the import keyword, while for all the libraries installed by the Contribution Manager the story is different.
The error message is The package "packageName" does not exists. You might be missing a library. Libraries must be installed in a folder named 'libraries' inside the 'sketchbook' folder.
Long story short I can't import those libraries...
I tried to copy them in the standard java libraries location (%SystemRoot%\Java\lib\ext) and in some other paths, but nothing...
I read that using classpath would allow as to use it but I can't understand how to use Processing with javac.
I also tried something like "ProcessingFolder\processing-java.exe" --sketch="$(CURRENT_DIRECTORY)" --run that is the same script you can use to run Processing in Notepad++, adding the statement --classpath="$(CURRENT_DIRECTORY)" (Obviously in Notepad++) but it didn't work (processing-java.exe state I don't know anything about --classpath=.).
So here's the question: How can we import and use libraries in .java tabs using Processing Development Environment?
That doesn't sound right. You should be able to use library classes just fine by importing them in a .java tab.
Step 1: From the PDE, go to Sketch -> Import Library, then choose the library you want to include. Notice that if you haven't included a library before, it's actually two steps: first you have to install the library, then you have to include it.
Step 2: Once you've included a library in your sketch, you can use the classes from that library anywhere in your sketch. This includes .java tabs.
Here's an example that uses the minim library in a .java tab. I didn't have to copy any files or create any directories:
Main sketch tab:
void setup(){
Test test = new Test(this);
}
Test.java tab:
import processing.core.PApplet;
import ddf.minim.Minim;
import ddf.minim.AudioPlayer;
import ddf.minim.AudioInput;
public class Test {
Minim minim;
AudioPlayer player;
AudioInput input;
public Test(PApplet sketch) {
minim = new Minim(sketch);
player = minim.loadFile("song.mp3");
input = minim.getLineIn();
}
}
It sounds like you aren't properly including the library in your sketch. Make sure you go through the Sketch -> Import Library menu, and make sure you both install and include the sketch.

How to display message box using java and JAR

I'm trying to build executable jar file. I'm fairly new to this and not really sure how it is suppose to work. I'm using eclipse 64 bit and windows 7. I'm exporting the project to jar file. This is project code. when I double click the file it doesn't do anything and I'm expecting to see a message box. Can you please tell me what am I doing wrong and help me fix it. thanks.
import javax.swing.*;
public class Starter {
public static void main(String[] args)
{
String st="Welcome";
JOptionPane.showMessageDialog(null,st);
}
}
It sounds as if there is no Main-Class defined in the MANIFEST.MF in your jar file.
There are numerous guides on how to create an executable jar available such as this one.

JPype Headaches

I've found several instructions on how to import user-built .class and .jar files to JPype, but I seem to be having a lot of trouble getting anything working at all.
What works: I can import standard java stuff and print HELLO WORLD and such.
Some of what I've tried:
I've tried adding -Djava.class.path with the path to a jar containing the relevant class files, to a directory structure containing (several folders down) the relevant .class files, as well as '-Djava.ext.dirs'. I've recompiled and re-installed with a different JVM location. The class I am attempting to instantiate is Outer, public, and has a public constructor.
I'm using Python 2.6.1 on OSX 10.6.
My current test file:
from jpype import *
startJVM(getDefaultJVMPath(), '-Djava.class.path=/Users/gestalt/Documents/msmexplorer_git/msmexplorer/MSMExplorer/build/classes')
java.lang.System.out.println("hello world")
msmexplorer = JPackage('org.joofee.meh.msmexplorer')
T = msmexplorer.MSMExplorer()
shutdownJVM()
If I use JClass I always get ClassNotFound exceptions from JPype; if I use JPackage I get Package not callable errors. Basically, JPype can't find my stuff.
Thanks so much!
EDIT (possibly helpful debugging stuff...):
Is there a straightforward way to print which third party java classes are available/imported?
Package not callable errors are referenced in this link) it would seem you need to make sure the java class file is accessible from the working directory. I am not sure how the jvm classpath comes into play, I would have thought how you did it would work.
You could also try loading the org package and then getting to the other packages through that one as the link I shared shows:
msmexplorer = JPackage('org').joofee.meh.msmexplorer
T = msmexplorer.MSMExplorer()

Categories

Resources