Using methods from a .jar file in Java - java

Question about Java. I'm using Eclipse, and I've created a simple project called Main. In this project there's a class called MainClass.
public class MainClass
{
}
I have another project called Math. It has only one class called Functions, and it's got one method called add:
public class Functions
{
public int add(int a, int b)
{
return a+b;
}
}
I've exported this project called Math into a .jar file. I want to use the add method from the .jar file in the project Main, for example:
public class MainClass
{
int x = add(1, 2);
}
What do I have to do? Thanks for your time.
Edit: Ok, I've added the jar in the lib folder, but now, how do I use add(int a, int b)? It says it is "undefined".
2nd Edit: I created an instance of the class Functions, so now I can use the methods. Thanks for your replies!

create folderlib add your jar to it. goto Project->Properties->Java Build Path-> Libraries->Add Jar and you are done.

You should include the .jar in your .classpath file.
Once this step is done, you can use the method.

You need to include the .jar file in Main's classpath.
Right click on the Main project in Eclipse, click properties, click "Java Build Path", click "Libraries", click add jar or add external jar and find the .jar file. That's it!

Related

Using Java Libraries in Android Project

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

how to import file from one eclipse project to another

I know this question must have been asked before and i have gone through the solution but that didnt seem to solve my question.
Suppose I have 2 projects in eclipse .ProjectA and ProjectB and ProjectA has file named FileA in package PackageA and ProjectB has a file named FileB in PackageB.Suppose i want to use a function of FileA in projectB.Is there any import statement which will allow me to do so.I dont want to copy paste the entire fileA in projectB.I just want something like
import ProjectA.packageA.fileA
class fileB
{
//calling function from fileA
int somethingsomething = fileA.somefunction();
}
I have done things like go to project right click and click on import and import the file.But i dont want the file to be present in my filelist of ProjectB.Please ,Can someone help me with this problem.Is this even possible?
First way
Right click on ProjectA and export as jar file.
Right click on ProjectB, Goto Build path -> Configure build path -> Libraries
Add external jar of ProjectA
Second Way,
Right click on ProjectB, Goto Build path -> Configure build path -> Projects
Click on Add, select ProjectA from list.
After this, you will be able to import classes in ProjectB which are defined in ProjectA by,
import packageA.fileA
Read more at : Java build path
Well your Project B needs the code of File A to execute it. You can either copy the file, or a .jar, or you copy the relevant code into the new project. You cannot just reference a piece of code but not add it to your Project in some way. When building your project to a .jar for example, this piece of information must be present and accessible for the program.

How to build jar for a project which depends on other project

I am using C#(Unity) to call java function in jar. My class is like:
Class A{
auth(){
}
share(){
StatusesAPI mStatusesAPI;
mStatusesAPI.upload("hey", bitmap, null, null, mListener);
........
}
}
. auth() can be call in c# and works correctly if I comment everything related to StatusesAPI, such like,
//private StatusesAPI mStatusesAPI;
//mStatusesAPI.upload("hey", bitmap, null, null, mListener);
Now, because I need this class(StatusesAPI ) to do something, I have to uncomment it and then import it. This Class is in project B and it depends on other classes in project B. So I add project B in Eclipse JAVA BUILD PATH. NO compile error. I enter
jar -cvf plugin.jar *
in cmd under folder myproject/bin/class
and I move the plugin.jar to unity /bin folder.
But when c# call auth(), error happens.
I doubt StatusesAPI code is not include into my plugin.jar, that makes class A all function don't work. I have tested calling my other class functions in plugin.jar, they works.
Any one know how to deal with this? How to make both auth() and share() works.
why don't you try import the jar file by
right click on your project main folder.
select Build Path then configure build path.
Under libraries choose the required jar file usi(plugin.jar) by Add External Jar's(go to the destination folder).
Click ok and once restart the IDE.
Hope this helps you

Creating jars from a class and using in in other programs

I am a newbie in Java.
I have a java file A which I want to call in another java program B.
I want to create jar for the A and use it in B by creating the objects and calling the methods for A.
What type of jar is to be created and how can I add it to the library. Please help how to do it in Eclipse.
Also, how to import the jar in B.
You can go to File->Export->jar-File
name your jar-file (you do not need a runnable jar)
after that you can open your other project B
right-click -> properties-> java build path
select libraries and click on add external jars
choose your exported jar-file. Click ok
and it is imported to your actual project B
now you can use classes and methods of this jar-File
First create a project in eclipse with class A (Creating project in Eclipse)
Export this project as jar (Exporting jar in Eclipse)
Create another project with class B and set class path of for exported jar (Setting class path in Eclipse)
Now you can use object of A in B.
import com.A;
public class B{
public static void main(String[] args){
A a=new A();
//.......
}
}

java project problem

I have a java project. I can run it through command prompt but can not able to run through Eclipse or NetBeans. When I run it, the error come main class is not found.
What can i do ?
How are you trying to run it in Eclipse and Netbeans? Basically you need to tell them which class to execute - which class has the main method in.
In Eclipse you can just go to the relevant class and hit Alt-Shift-X, J to launch it.
A few steps for eclipse
create a new project: Menu File/New/Project...
place your java source in the src folder of your project
through the context menu (right click the project name in navigator) you define the build path, and add required libraries.
now your code should be ready to run using the green (>) button
Is your project using libraries? I had the opposite problem where I could run my program in Netbeans and not from the jar (or command line) because the libraries were in my Netbeans folder and not my "distribution" folder.
EDIT: By libraries I mean third party libraries.
When you create a project in NetBeans, it will create a default Main class for you, complete with a main(String[] args) method. To access your code, just rename your class main method, copy it (and any dependnt classes into the project and change the package names to reflect the project name) and instantiate the class containing it in the default NetBeans main method and call the renamed main method e.g. if your class is called HelloWorld and the main method was renamed "hello" the call would look like:
HelloWorld hw = new HelloWorld();
hw.hello();
Simples :)

Categories

Resources