Creating jars from a class and using in in other programs - java

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();
//.......
}
}

Related

How can I call a method parameter from jar in java program without cmd

How can I call a method parameter from jar in a java program without cmd?
Your question is not clear:
what is your mean of calling a parameter. calling is for methods not parameters
what is your mean of cmd
if you want to use a method or class or function from a jar file just add the jar file to your project
import the needed classes from it and use them in your program
How to add jar to project depends on IDE that you use for example in eclipse:
https://www.wikihow.com/Add-JARs-to-Project-Build-Paths-in-Eclipse-(Java)
http://www.java67.com/2017/04/how-to-add-jar-file-in-eclipse-project.html
Add your jar to your project
in your main class (one that has main method) import the needed class from your jar:
import the.package.of.your.jar.library.className
public static void main(String[] args){
// here use your imported class
}
you need to know the package of jar the classes and how to use them. you should study descriptions of the developer of the jar for this. this depends on the jar library which you imported

What is included when making a jar file?

When I build my project (using the Netbeans IDE) will it only take the main class, or will it include the other classes in the project even though they are not explicitly declared within the code for the main class?
All classes in a package are included in a jar file. You can cross-check it by following this:
Go to dist folder of the project.
Extract the jar file with any file archiver(eg: 7-zip, winrar, winzip).
The extracted folder contains all the contents that are included in a jar file.
Example:
Test is a main class
Test.java:
package test;
public class Test
{
public static void main(String[] args) {
// TODO code application logic here
}
}
There is another class: class1
class1.java:
package test;
public class class1
{
}
class1.java is not explicitly declared within the code for the main class(Test).But when I build this project in a Net-beans IDE and extracted the jar file(Test.jar).The jar contains:
class1.class
Test.class
What goes in a JAR and what remains out depends on the packaging agent, so this is definitely IDE-dependent, and also depends on the configuration options used to build the archive (for example if it's a lib JAR, if it's an executable JAR, if it's a WAR, and so on).
For example, Eclipse can package your compiled sources along with the JAR dependencies, but this also requires including some special class to load classes from JARs inside the distributed JAR.
Anyway, you can always customize the packaging phase, and you can extract the archive to see what it actually contains.

Using methods from a .jar file in 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!

Java: simple JAR project, when run, cannot find an imported class in a second simple JAR project even though second JAR passed via -classpath

I have written two simple Java classes (one of them containing "main()", and the other called by "main()").
Class #1 (containing "main()"):
package daniel347x.outerjar;
import daniel347x.innerjar.Funky;
public class App
{
public static void main( String[] args )
{
Funky.foo();
}
}
Class #2 (called by "main()"):
package daniel347x.innerjar;
public class Funky
{
public static void foo()
{
System.out.println( "Funky!" );
}
}
The above classes appear in different project root folders, and use Maven as the build system (each project has its own POM). The pom.xml file for the main project includes the proper entry to add daniel347x.outerjar.App as the main class, and it properly includes the dependency on daniel347x.innerjar. Both projects build successfully into JAR files.
I use NetBeans to wrap these as Maven projects (in fact, I used NetBeans to create both projects). When I run the main project from within NetBeans, it runs successfully and I see Funky! as the output.
However, when I attempt to run the main class straight from the Windows command line (cmd.exe), passing the JAR file containing Funky on the command line's classpath, as such:
java -classpath "P:\_Dan\work\JavaProjects\JarFuckup\innerjar\target\innerjar-1.0-SNAPSHOT.jar" -jar "P:\_Dan\work\JavaProjects\JarFuckup\outerjar\target\outerjar-1.0-SNAPSHOT.jar"
... I receive the dreaded NoClassDefFoundError:
Exception in thread "main" java.lang.NoClassDefFoundError: daniel347x/innerjar/Funky
at daniel347x.outerjar.App.main(App.java:7)
I have carefully confirmed that, inside the innerjar JAR file noted above containing Funky, that the path structure is daniel347x\innerjar and that inside the innerjar folder is the Funky.class file - and this file looks correct within a HEX editor where I can see the ASCII strings representing the name of the class.
The fact that the class can't be found defies my understanding of Java, which I thought allows you to pass JAR files as a -classpath parameter and it will be able to find classes inside those JAR files.
This very basic point has me flummoxed - an answer that explains what I am doing wrong would be greatly appreciated.
The classpath is ignored when using the -jar option. A way to run your app would be java -classpath "P:\_Dan\work\JavaProjects\JarFuckup\innerjar\target\innerjar-1.0-SNAPSHOT.jar";"P:\_Dan\work\JavaProjects\JarFuckup\outerjar\target\outerjar-1.0-SNAPSHOT.jar" daniel347x.outerjar.App
Perhaps a better approach would be to add a manifest file to the Jar that specifies the class path of the dependent Jars by relative paths. Then..
java -jar "P:\_Dan\...\outerjar-1.0-SNAPSHOT.jar"
..should do it.
Double clicking the main Jar will also launch it. That is mostly useful for GUIs.

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