What is included when making a jar file? - java

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.

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

IntelliJ: Why can't I use a class from external jar

When I created the jar file I wrote the following java file:
package myjar;
public class MyClass {
public static void hello() {
System.out.println("hello");
}
public static void main(String[] args) {
MyClass.hello();
}
}
I named the file MyClass.java.
I created a class file by typing "javac src/main/java/myjar/MyClass.java"
I generated a jar file by the command:
"jar cvf myjar.jar src/main/java/myjar/MyClass.class"
Now, I took the jar file and added it to Project Structure. The name of the jar is 'myjar':
and in the IDE I wrote "import myjar.MyClass" and the word 'myjar' was marked in red.
When I use 'MyClass.hello()' in the project, I get an error:
no main manifest
Do you know what I can to to load it successfully ?
You can only import some classes from your libraries on classpath into a Java source file, not a contents of a whole Java archive (JAR) file. So if you have inside myjar.jar file class Foo.class inside package (folder) bar, you can do
import bar.Foo;
After adding the .jar as a library in IntelliJ, you still need to add it to the module.
Then the library will appear in the module's list of dependencies.

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: 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.

How to use jar files without package information?

I have a jar called "MyTools". The jar is in c:\data folder. I created a new file in the same folder called "UseTools.java". Now I would like to use some of the classes from the MyTools.jar in my UseTools.java. I tried this but it doesnt seem to work:
import MyTools.*;
public class UseTools
{
public static void main(String[] args)
{
MyTools.SomeClass foo = new SomeClass();
SomeClass.doSomething();
}
}
I tried to compile this with:
javac -cp . UseTools.java
and got this error message:
UseTools.java:1: package MyTools does not exist
import MyTools.*;
^
UseTools.java:7: package MyTools does not exist
MyTools.SomeClass foo = new SomeClass()
^
2 errors
I did not set the package name in any class.
Do I have to set a package name in my jar classes?
To mention something that relates more to the title of the question:
In Java, you can't access classes in the default package from code within a named package.
This means, if the classes in your jar file do not belong explicitly to any package and inside the jar your files are directly in the root folder without subfolders, they are in the default package. This is not very elaborated and lacks modularity as well as extensibility, but is technically alright.
Then, you can only use these classes from code which also is in the default package. But this does not necessarily mean it has to be in the same jar. If you have multiple src or class folders they could all contain classes in the default package which can interact. The organization in JAR files and the package structure in your project are independent of each other.
However, I'd strictly encourage you to use explicit package information.
In your MyTools.jar there should be a package with the name MyTools. And before compiling you should add the jar to the classpath.
You need to add -cp file.jar instead of -cp .
The latter one will pick up .class files only. BTW: why not using an IDE like netbeans, eclipse or intelliJ?

Categories

Resources