Understanding JARs and Packages in Java - java

I'm trying to understand how jars and packages work in Java. So to do this, I created a simple test JAR and am trying to use a class contained in that jar. Simple enough, but it is giving me errors like "class not found". Here's the setup:
1) I have a file called MyHelloWorld.java, which will be packaged in a JAR:
package com.mytest;
public class MyHelloWorld {
public String getHello() {
return "Hello";
}
public static void main(String[] args) {
System.out.println("Hello world!");
}
}
2) I have another file called 'HelloHello.java' which uses the function getHello() in com.mytest.MyHelloWorld
import com.mytest.*;
public class HelloHello {
public static void main (String[] args) {
MyHelloWorld hello = new MyHelloWorld();
System.out.println(hello.getHello());
}
}
3) To package the MyHelloWorld class inside a JAR, I created the folders com/mytest in the current directory, and moved MyHelloWorld.java to that folder
4) I compiled MyHelloWorld.java in that folder using javac MyHelloWorld.java
5) I ran jar -cf myhello.jar ./com/mytest/*.class from the root folder to create the JAR file (as described in http://www.javacoffeebreak.com/faq/faq0028.html)
6) I copied HelloHello.java and myhello.jar to a new folder with nothing else in it, to test this setup
7) javac -cp ./*.jar HelloHello.java [succeeds]
8) java -cp ./*.jar HelloHello [FAILS] (I also tried just `java HelloWorld', which failed too, with a different error message)
This last statement fails with the message:
$java -cp ./*.jar HelloHello
Exception in thread "main" java.lang.NoClassDefFoundError: HelloHello
Caused by: java.lang.ClassNotFoundException: HelloHello
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:315)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:330)
at java.lang.ClassLoader.loadClass(ClassLoader.java:250)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:398)
Any idea why it's failing? Any insights you can provide on why it works this way, and how package names are defined inside a JAR etc. would also be appreciated!

I believe it is looking in the jar for your HelloHello class. You probably need the current folder on the classpath too.
java -cp .:myhello.jar HelloHello

You should use:
java -cp .:./* HelloHello
java and javac treat -cp argument a bit differently. With java the * in cp will automatically load all the jars it finds in the given location.
Also, the colon : is the separator between different classpath elements.

Make sure if HelloHello.class is in appropriate directories structure (com/mytest) than change your 8th step:
8) java com.mytest.HelloHello //or java -cp .;*.jar com.mytest.HelloHello

well, java HelloHello works too

Related

How to include a third-party jar-file in compilation and runtime using command prompt in Windows 10?

I'm trying to understand the inclusion of third party jar files in a java project using only the command line in Windows 10.
Specifically, I try to include the file json-20200518.jar in my "project" so that I can use the java object JSONObject in the project.
My java file:
package com.mypackage.example;
import org.json.JSONObject;
class Example {
public static void main(String[] args) {
// ... program logic
}
}
location of my java file (Examp.java):
./com/mypackage/example
location of jar file:
./jars
using cmd win10 I compile:
javac -cp "C:\Users\pfort\Desktop\java\jars\json-20200518.jar" "C:\Users\pfort\Desktop\java\com\mypackage\example\Examp.java"
compilation is successful.
Run:
java -cp "C:\Users\pfort\Desktop\java\jars\json-20200518.jar" com.mypackage.example.Examp
I get a report:
Error: Could not find or load main class com.mypackage.example.Pokus
Caused by: java.lang.ClassNotFoundException: com.mypackage.example.Pokus
Second attempt:
java -cp "C:\Users\pfort\Desktop\java\jars\json-20200518.jar" "C:\Users\pfort\Desktop\java\com\mypackage\example\Pokus"
But the same error message comes back to me.
Where am I going wrong? Is it the wrong structure? I don't get it, the compilation is successful but the run does not work.
The compiled Examp.class file isn't part of json-20200518.jar, so you'll need to add the directory containing it to the command line. Assuming it's the current directory (.):
java -cp "C:\Users\pfort\Desktop\java\jars\json-20200518.jar;." com.mypackage.example.Examp

NoClassDefFoundError on simple jar file

public class RunScript {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
I created a Java project and a package called com.klong
Inside of the package I have one .java file with the above code in it.
I export it into a runnable jar file. Then I try to run the jar in a command line using this:
java test.jar
When I try that, I get the following error
C:\Users\IBM_ADMIN\Tracing stuff>java test.jar
Exception in thread "main" java.lang.NoClassDefFoundError: test.jar
Caused by: java.lang.ClassNotFoundException: test.jar
at java.net.URLClassLoader.findClass(URLClassLoader.java:434)
at java.lang.ClassLoader.loadClass(ClassLoader.java:653)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:358)
at java.lang.ClassLoader.loadClass(ClassLoader.java:619)
Could not find the main class: test.jar. Program will exit.
I've tinkered with exporting into a normal jar file and such. I've looked at other questions about this error. I've tried using commands such as set classpath=BLEH
This project is as simple as can be so hopefully you can help me figure out this pesky error!
You need to run java -jar test.jar, what you're currently doing is asking java to look for compiled classes with the name test.jar.

How do I run a Java class in a package?

I have two java classes as follows
App1 without a package:
class App1 {
public static void main(String[] args) {
System.out.println("App1 hello world...");
}
}
App2 in a package:
package java.java.package1;
class App2 {
public static void main(String[] args) {
System.out.println("App2 hello world...");
}
}
I can compile them both:
D:\javaTest>javac App1.java
D:\javaTest>javac App2.java
However, I can only run the first one:
D:\javaTest>java App1
App1 hello world...
D:\javaTest>java java.java.package1.App2
Exception in thread "main" java.lang.NoClassDefFoundError: java/java/package1/App2
Caused by: java.lang.ClassNotFoundException: java.java.package1.App2
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: java.java.package1.App2. Program will exit.
How can I run App2?
If you put the source in an appropriate directory hierarchy matching the package name (D:\javaTest\java\java\package1\App1.java), and compile/run from the root of the hierarchy (D:\javaTest), you wouldn't have this problem:
D:\javaTest>javac java\java\package1\App1.java
D:\javaTest>java java.java.package1.App1
App2 hello world...
You can also compile using the -d option so that the classes are moved into such a directory hierarchy:
javac -d . App2.java
java java.java.package1.App2
Note you shouldn't use a package name starting with java, and later versions of the JDK will throw a SecurityException. See this question for more information.
You create a new directory. This is the directory containing your work, and is not the start of your packages.
For instance, I create folder /terri to start.
I then create the folder structure /clarie/andrea under it.
My package is going to be called claire.andrea in this example.
Normal package names start with com and then a company name or something like that (or java for standard java packages, so don't use that: like java.lang.*).
In the andrea folder, I create a java file called Saluton.java with the class Saluton (which just print hello). The class name and the filename must match.
To compile, from the terri/ folder: javac .\claire\andrea\Saluton.java
This will create a Saluton.class in the \terri\claire\andrea\Saluton.class
To run: (again from /terri), I do: java -cp . claire.andrea.Saluton
Which says, use class path from my current directory.
My main program is in the package claire.andrea and the Class name is Saluton.
Here's the run:
\terri
java -cp . claire.andrea.Saluton
"Hello World".
To sum it up, the package name much match the underlying directory structure.
The file (if it references a package) must live inside the directory stucture it is refering. If I compile Saluton.java in /terri with package claire.andrea I have not found a way to run it, it does compile fine.
Also, the filename for the class must match the public class in that file.
To run, package.Class. In general, packages are not capitalized and Classes are.
You have to put -d directory Options..because
Set the destination directory for class files. If a class is part of a
package,
javac puts the class file in a subdirectory reflecting the package name,
creating directories as needed. For example, if you specify -d c:\myclasses
and the class is called com.mypackage.MyClass, then the class file is called
c:\myclasses\com\mypackage\MyClass.class.
App2 needs to be in the java/java/package1 directory. In java, the package name and the directory for the source code must match. Did you not get a compilation error?
Check what is your classpath value by below command in command prompt
echo %CLASSPATH%
check where your class is getting created.
to compile a java source program you need to check the path you are giving , whether java file is available there or not.

Error: Could not find or load main class [duplicate]

This question already has answers here:
What does "Could not find or load main class" mean?
(61 answers)
Closed 7 years ago.
I am having trouble compiling and running my Java code, intended to allow me to interface Java with a shared object for Vensim, a simulation modeling package.
The following code compiles without error:
javac -d . -cp ./apache-log4j-1.2.16/log4j-1.2.16.jar:./vensim.jar SpatialModel.java VensimHelper.java VensimException.java VensimContextRepository.java
However, when I try to run the following:
java -cp ./apache-log4j-1.2.16/log4j-1.2.16.jar:./vensim.jar SpatialModel vars
I get the following error: "Error: Could not find or load main class SpatialModel
". My SpatialModel.java code does contain a 'main' method (below), so I'm not sure what the problem is - can anyone please help me out? Thanks.
import java.io.File;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.log4j.Logger;
public class SpatialModel {
private VensimHelper vh;
public static final String DLL_LIBNAME_PARAM = "vensim_lib_nam";
public static final String MODEL_PATH_PARAM = "vensim_model_path";
private final static int VENSIM_CONTEXT_CREATION_MAX_FAILURE_COUNT = 10;
public SpatialModel() throws SpatialException {
String libName = System.getProperty(DLL_LIBNAME_PARAM);
String modelPath = System.getProperty(MODEL_PATH_PARAM);
if(libName == null || libName.trim().equals("")) {
log.error("Vensim library name has to be set with -D" + DLL_LIBNAME_PARAM);
throw new SpatialException("Vensim library name has to be set with -D" + DLL_LIBNAME_PARAM);
}
if(modelPath == null || modelPath.trim().equals("")) {
log.error("Model path has to set with -D" + MODEL_PATH_PARAM);
throw new SpatialException("Model path ahs to be set with -D" + MODEL_PATH_PARAM);
}
for (int i = 0; i < VENSIM_CONTEXT_CREATION_MAX_FAILURE_COUNT && vh == null; i++) {
try {
log.info("creating new vensim helper\n\tdll lib: " + libName + "\n\tmodel path: " + modelPath);
vh = new VensimHelper(libName, modelPath);
} catch (Throwable e) {
log.error("An exception was thrown when initializing Vensim, try: " + i, e);
}
}
if (vh == null) {
throw new SpatialException("Can't initialize Vensim");
}
}
public static void main(String[] args) throws VensimException {
long before = System.currentTimeMillis();
String libName = System.getProperty(DLL_LIBNAME_PARAM);
String modelPath = System.getProperty(MODEL_PATH_PARAM);
if (libName == null) {
libName = "libvensim";
}
if(modelPath == null) {
modelPath = "~/BassModel.vmf";
}
System.setProperty(DLL_LIBNAME_PARAM, libName);
System.setProperty(MODEL_PATH_PARAM, modelPath);
if (args.length > 0 && args[0].equals("info")) {
System.out.println(new VensimHelper(libName, modelPath).getVensimInfo());
} else if (args.length > 0 && args[0].equals("vars")) {
VensimHelper helper = new VensimHelper(libName, modelPath);
String[] vars = helper.getVariables();
for (String var : vars) {
System.out.println(helper.getVariableInfo(var));
}
} else {
File f = new File(".");
System.out.println(f.getAbsolutePath());
SpatialModel sm = new SpatialModel();
}
System.out.println("Execution time: " + (System.currentTimeMillis() - before));
}
}
You must ensure that you add the location of your .class file to your classpath. So, if its in the current folder, add . to your classpath.
Note that the Windows classpath separator is a semi-colon, i.e. a ;.
If the class is in a package
package thepackagename;
public class TheClassName {
public static final void main(String[] cmd_lineParams) {
System.out.println("Hello World!");
}
}
Then calling:
java -classpath . TheClassName
results in Error: Could not find or load main class TheClassName. This is because it must be called with its fully-qualified name:
java -classpath . thepackagename.TheClassName
And this thepackagename directory must exist in the classpath. In this example, ., meaning the current directory, is the entirety of classpath. Therefore this particular example must be called from the directory in which thepackagename exists.
To be clear, the name of this class is not TheClassName, It's thepackagename.TheClassName. Attempting to execute TheClassName does not work, because no class having that name exists. Not on the current classpath anyway.
Finally, note that the compiled (.class) version is executed, not the source code (.java) version. Hence “CLASSPATH.”
You can try these two when you are getting the error: 'could not find or load main class'
If your class file is saved in following directory with HelloWorld program name
d:\sample
java -cp d:\sample HelloWorld
java -cp . HelloWorld
I believe you need to add the current directory to the Java classpath
java -cp .:./apache-log4j-1.2.16/log4j-1.2.16.jar:./vensim.jar SpatialModel vars
You have to include classpath to your javac and java commands
javac -cp . PackageName/*.java
java -cp . PackageName/ClassName_Having_main
suppose you have the following
Package Named: com.test
Class Name: Hello (Having main)
file is located inside "src/com/test/Hello.java"
from outside directory:
$ cd src
$ javac -cp . com/test/*.java
$ java -cp . com/test/Hello
In windows the same thing will be working too, I already tried
If you work in Eclipse, just make a cleanup (project\clean.. clean all projects) of the project.
You have to set the classpath if you get the error:
Could not find or load main class XYZ
For example:
E:\>set path="c:\programfiles\Java\jdk1.7.0_17\bin"
E:\>set classpath=%classpath%;.;
E:\>javac XYZ.java
E:\>java XYZ
I got this error because I was trying to run
javac HelloWorld.java && java HelloWorld.class
when I should have removed .class:
javac HelloWorld.java && java HelloWorld
Check your BuildPath, it could be that you are referencing a library that does not exist anymore.
If you're getting this error and you are using Maven to build your Jars, then there is a good chance that you simply do not have your Java classes in src/main/java/.
In my case I created my project in Eclipse which defaults to src (rather than src/main/java/.
So I ended up with something like mypackage.morepackage.myclass and a directory structure looking like src/mypackage/morepackage/myclass, which inherently has nothing wrong. But when you run mvn clean install it will look for src/main/java/mypackage/morepackage/myclass. It will not find the class but it won't error either. So it will successfully build and you when you run your outputted Jar the result is:
Error: Could not find or load main class mypackage.morepackage.myclass
Because it simply never included your class in the packaged Jar.
I know this question was tagged with linux, but on windows, you might need to separate your cp args with a ; instead of a :.
java -cp ./apache-log4j-1.2.16/log4j-1.2.16.jar;./vensim.jar SpatialModel vars
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/classpath.html
If you try to run a java application which needs JDK 1.6 and you are trying to run on JDK 1.4, you will come across this error. In general, trying to run a Java application on old JRE may fail. Try installing new JRE/JDK.
Problem is not about your main function. Check out for
javac -d . -cp ./apache-log4j-1.2.16/log4j-1.2.16.jar:./vensim.jar SpatialModel.java VensimHelper.java VensimException.java VensimContextRepository.java
output and run it.
Project > Clean and then make sure BuildPath > Libraries has the correct Library.
java -verbose:class HelloWorld might help you understand which classes are being loaded.
Also, as mentioned before, remember to call the full qualified name (i.e. include package).
I was using Java 1.8, and this error suddenly occurred when I pressed "Build and clean" in NetBeans. I switched for a brief moment to 1.7 again, clicked OK, re-opened properties and switched back to 1.8, and everything worked perfectly.
I hope I can help someone out with this, as these errors can be quite time-consuming.
This problem occurred for me when I imported an existing project into eclipse. What happens is it copied all the files not in the package, but outside the package. Hence, when I tried run > run configurations, it couldn't find the main method because it was not in the package. All I did was copy the files into the package and Eclipse was then able to detect the main method. So ultimately make sure that Eclipse can find your main method, by making sure that your java files are in the right package.
If so simple than many people think, me included :)
cd to Project Folder/src/package there you should see yourClass.java then run javac yourClass.java which will create yourClass.class then cd out of the src folder and into the build folder there you can run java package.youClass
I am using the Terminal on Mac or you can accomplish the same task using Command Prompt on windows
If you are using Eclipse... I renamed my main class file and got that error. I went to "Run As" configurator and under the class path for that project, it had listed both files in the class path. I removed old class that I renamed and left the class that had the new name and it compiled and ran just fine.
This solved the issue for me today:
cd /path/to/project
cd build
rm -r classes
Then clean&build it and run the individual files you need.
I have a similar problem in Windows, it's related to the classpath. From the command line, navigate until the directory where it's located your Java file (*.java and *.class), then try again with your commands.
I use Anypoint Studio (an Eclipse based IDE). In my case everything worked well, until I found out that while running the java code, something totally different is executed. Then I have deleted the .class files. After this point I got the error message from this question's title. Cleaning the project didn't solve the problem.
After restarting the IDE everything worked well again.

Using a jar in a Java project?

I'm trying to use the public methods/classed from a project provided as a jar file (called Hello.jar for instance) wrapped in a package called hello.
package hello;
public class Hello
{
public static void main(String[] args)
{
coucou();
}
public static void coucou()
{
System.out.println("Hello there");
}
}
In a separate project called Tool, I want to be able to call the method Hello.coucou() so I wrote something like this:
import hello.*;
public class Tool
{
public static void main(String[] args)
{
System.out.println("main program running");
Hello.coucou();
}
}
and I compiled Tool.java with the following command (under linux):
$ javac Tool.java -classpath .:./extern/:
where Hello.jar is located in the folder ./extern
This seems to compile fine but when I launch it (i.e. java Tool), I get this:
main program running
Exception in thread "main" java.lang.NoClassDefFoundError: hello/Hello
at Tool.main(Tool.java:9)
Caused by: java.lang.ClassNotFoundException: hello.Hello
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:323)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:268)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:336)
... 1 more
I am new to Java (C/C++ background) and I don't understand what I'm doing wrong.
Any ideas?
Cheers
David
Edit: I tried adding Hello.jar to the classpath on the command line, but I still get the same error:
$ javac Tool.java -classpath .:./extern/Hello.jar:
$ java Tool -classpath .:./extern/Hello.jar:
main program running
Exception in thread "main" java.lang.NoClassDefFoundError: hello/Hello
at Tool.main(Tool.java:9)
Caused by: java.lang.ClassNotFoundException: hello.Hello
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:323)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:268)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:336)
... 1 more
You need the Hello.jar on the classpath when you run as well as when you compile.
Actually the trick was in the order of the arguments in the command line:
Is the -cp (or -classpath) is set last, then it doesn't work
java Tool -cp .:extern/Hello.jar
It has to be first like:
java -cp .:extern/Hello.jar Tool
!!!
Java uses dynamic late binding, so putting the JAR in the classpath during compilation is only necessary to ensure that your code is using the classes from it correctly, but it does not actually embed them into your code as the linker would in C/C++. Thus, you need to set the classpath also when executing the code.
However, this:
$ javac Tool.java -classpath .:./extern/:
should not work either, since JARs need to be put into the classpath directly, not just the directory they live in:
$ javac Tool.java -classpath .:./extern/Hello.jar
Finally, you are placing your code in the default nameless package. This is OK for fooling around, but will cause problems in the long run (for one thing, you cannot import classes FROM the default package anywhere else).
When you run Java you must add the jar file too (adding the directory path only does not work).
See classpath information.
It should be something like this:
java -classpath /java/MyClasses/myclasses.jar utility.myapp.Cool
You need to include the Hello.jar file in the classpath when you launch it too.
java -cp xxx.jar hello where xxx is the jar you want to have in your classpath, if you want multiple jars then separate them using ;
karl

Categories