I am trying to create .jar files in java which can be used by including it in other projects. It doesn't contain any main but only classes and methods. I am using th following command to create .jar file from command line
jar cf firstjar.jar *
I am including the jar file in my project but still the class is not available in the project.
What's the problem in this.
My guess is that you've got a problem with the directory structure.
Suppose you have a class like this:
package org.foo;
public class Bar {}
There should be Bar.class flie in a directory org/foo. That structure has to be in the jar file as well - so you should go to the directory containing org and run:
jar cf firstjar.jar org
(Or whatever your top-level package name is.)
If you just include the classfiles from the foo directory then Java's expectations of where to find things will be invalid.
Related
I am trying to include a .class file through an import in a .java class I am creating. I need the .java class to look the same as what I need to deploy on the application server. I have created a directory structure to put the Context.class file in.
I updated the build path by using "Add External Class Folder..." and selected the "oracle" directory containing apps>fnd>common.
However, Eclipse still can't find the Context.class file referenced in the import.
What am I doing wrong?
You need to select the classes (C:\Users\badams\java\classes) directory instead of the oracle directory when using Add External Class Folder....
You should provide the root of the directory that represent your java package structure.
As an alternative, you can create a jar archive with the contents of the C:\Users\badams\java\classes directory and add that archive as a jar dependency as well.
You can use for this purpose the builtin SDK jar utility. Please, change you current directory to C:\Users\badams\java\classes and run from a command prompt or PowerShell terminal something like:
jar cvf oracle-fnd.jar *
Then add the generated archive to your project as usual.
I have some Java code on my machine that I run from the command line. I want to create a runnable .jar file from this code so that I can distribute my application more easily.
The code is in four folders, called fol_a, fol_b, fol_c, and fol_d. Each of these contains a /bin subfolder, containing the .class files, and two of them (fol_a and fol_b) also contain a /lib folder, containing some .jar files that the code needs.
fol_d contains the class to run, Demo, which is in a package called machineLearning. The full path to the class is
fol_d/bin/machineLearning/Demo
I currently run the code from the command line as follows:
$ cd fol_d/bin
$ java -cp ".:../../fol_a/bin:../../fol_a/lib/*:../../fol_b/bin:../../fol_b/lib/*:../../fol_c/bin" machineLearning.Demo <param_1> <param_2> ... <param_5>
where <param_1> to <param_5> are the arguments given to the Main method in Demo.
What I want is to create one single .jar file that contains all the code that is necessary to execute Demo successfully, i.e., the code in fol_a through fol_d. I then want to be able to run this .jar file from the command line, giving it the arguments that go to the Main method in Demo. Something like this:
$ java -jar MyApplication.jar <param_1> ... <param_5>
Is this possible? How would I do this? I've been trying to find an answer online, but the amount of information confuses me.
UPDATE
Right! So it seems that all I needed to do was this:
copy the contents of the bin directories to a new dir myapp
make a manifest.txt file that specifies the main class to run, as well as the classpath
jar myapp: $ jar cmf manifest.txt myapp.jar -C myapp/ .
execute the jar: $ java -jar myapp.jar <arg_1> <arg_2> ... <arg_n>
Yes it is possible.
Use "cp -R" to copy all 4 folders' bin directories into one directory ... preserving the subdirectory structures. (Read man cp if you don't understand how. Install the manual entries if they are not installed.)
Use the jar command to create the JAR file from the consolidated directory.
UPDATE
When you create the JAR file, the paths within the JAR (i.e. in the JAR file index) must match the respective classes fully qualified names.
If you are creating an executable JAR, the Main Class attribute must specify the fully qualified class name.
If you misname the JAR file entries then either java won't find the classes, or it will refuse to load them because the pathname and classname don't match.
These requirements apply for all JARs, but from your comments it seems that you have overlooked this.
In your comment, you seem to have used the wrong classname in the Main Class attribute ... unless you declared the class in the fol_d.bin package!
UPDATE 2
Here is an example to illustrate my point about fully qualified classnames
package foo.bar;
public class Main {
...
}
The simple class name is Main. The fully qualified classname is foo.bar.Main. If you put the ".class" file for this class into a JAR, the pathname in the JAR file for the class must be:
/foo/bar/Main.class
The package name (foo.bar) maps to the directory path in the JAR file index; i.e. "/foo/bar".
If the pathname in the JAR file isn't that, then the classloader won't find it.
I have a Java project with 5 packages and 30 classes. I want to test this project on a different computer, but I can't install any sotware on that computer so I can't use things like Maven, Eclipse etc. Is there a way I can execute the program on that computer?
What I tried to do, is to compile the project using Eclipse on my computer, then went to the other computer and tried to execute the project main class via the folder that the main class .class file is at.
I.E., say that the main class name is Hello in package Greetings and Hello.class is at folder named folder. So I opened the command line window at folder and typed the command:
java Greetings.Hello
That didn't work....
Edit: After doing this I got the message: Error: Could not find or load main class Greetings.Hello
If the package name is Greetings and you want to run Hello.class
Hello class must have main method.
Hello.class must in folder name Greetings (package name).
Execute java Greetings.Hello from the one level above of Greetings folder
It seems to me Hello.class is not inside of Greetings folder
If javac is installed on the system you can directly compile on the system. you can compile even large projects including many packages with choosing different options provided by javac.
The javac tool reads class and interface definitions, written in the Java programming language, and compiles them into bytecode class files. It can also process annotations in Java source files and classes.
There are two ways to pass source code file names to javac:
For a small number of source files, simply list the file names on the command line.
For a large number of source files, list the file names in a file, separated by blanks or line breaks. Then use the list file name on the javac command line, preceded by an # character.
Source code file names must have .java suffixes, class file names must have .class suffixes, and both source and class files must have root names that identify the class. For example, a class called MyClass would be written in a source file called MyClass.java and compiled into a bytecode class file called MyClass.class.
Inner class definitions produce additional class files. These class files have names combining the inner and outer class names, such as MyClass$MyInnerClass.class.
You should arrange source files in a directory tree that reflects their package tree. For example, if you keep all your source files in C:\workspace, the source code for com.mysoft.mypack.MyClass should be in C:\workspace\com\mysoft\mypack\MyClass.java.
By default, the compiler puts each class file in the same directory as its source file. You can specify a separate destination directory with -d source
Given that you have Eclipse and you ran the code in Eclipse: the quickest way is to use Eclipse and export it to executable JAR.
If you have Run Configuration (e.g. named Hello) that you use for running the code:
Menu -> Export -> Runnable JAR file
Launch Configuration: Hello
Select export destination: (e.g. C:\tmp\Hello.jar)
Set Extract required libraries into generated JAR
Click finish.
This will create Hello.jar file you can execute typing in:
java -jar Hello.jar
I have a .jar and I would like to add my own .class to it and recompile. Is there anywhere I can do it?
Yes, and it's simple (if you already have the .class file - then there is no recompilation).
(Assuming you have the .class file already and just want to add it to a .jar. If you don't have the .class file, you need to write a .java source file and compile it to .class first using javac)
Jar files are actually zip files - you can use zip/unzip to create and unpack them.
unzip the jar file using a unzip program
add your class to the unzipped directory (*)
zip again (possibly to another name.jar)
(*) In step 2, you must put your .class file in the correct directory - correct meaning that the package name of your class must match the directory path where you .class file resides, relative to the .jar archive root.
For example if your My.class defines that it is in package com.nicky, then it must be found at com/nicky/My.class (where com is a directory in the archive root directory). If My.class has no package, they it must be in archive root directory.
No recompilation is needed - Java does linking dynamically at runtime - if the rest of the program needs to use class com.nicky.My, it will do so successfully if the class file is in the correct place in the .jar file.
You use the classpath parameter of the virtual machine (java -cp a.jar:b.jar:... ) to tell the java process where to look for classes - in case it would make more sense to package your class(es) in a separate .jar file..
--
Edit 1 / responding to your comment:
In case you a writing a new .java file, you need to first compile it into a .class using the javac command line compiler that comes with the jdk.
Every Java class belongs to a package, which is usually declared in the first line of the .java source file. Package names must match directory path location of the class (like in the example above), for both .java source files (for compilation to succeed), and for .class files (for dynamic loading / running to succeed).
You could also download and use a Java IDE (eg. http://www.eclipse.org/downloads/packages/eclipse-ide-java-developers/mars2 )to create your .class file. Create a new Java Project in Eclipse, write your class source code, click build (if it already doesn't build automatically) and then use a file explorer to take your .class file from the 'target' directory of the project on the disk.
If your new .java class depends on classes from the .jar you are trying to add it to, use
javac -cp your.jar com/nicky/My.java
to tell the compiler where the additional compile-time dependencies are.
If you are compiling from Eclipse, you need to configure that your project depends on your.jar: Right Click on your Project -> Properties -> Java Build Path -> Libraries -> Add Jars..
I created a jar file from a bunch of java files. Folder structure was org/ax/redis. I used the command jar cvf jedis.jar org/*. Then I imported this jar file in my netbeans project. Then when I tried importing classes from it, by writing import org.ax.redis.*. However, netbeans shows error that no such package exists.
Now I opened another jar file of log4j to see how it is from inside. Only difference was in manifest file. It had a bunch of directives like Name: org/apache/log4j/. So I created a manifest file for my jar file by including Name: org/ax/redis/. Used this command to add manifest information in my jar jar cvfm jedis.jar META-INF/manifest.txt org/*. Still nothing works. Please help me
Jar files typically contain class files (the results of compilation) rather than source files (*.java). While some jar files may contain both, only the class files are available to compile or run against (e.g. using -cp library.jar).
So basically, before you build your jar file, you need to compile your code - and then include the class files in the jar file. If you include the source files as well (in the same directory structure) then some IDEs may be able to detect that, which can be useful.
Even If u don't generate .class file , the package still can be imported and it won't show any compilation error(If U don't specify any particular class) . However when U try to use a particular class function of that package , it will throw an error .