Running a java program with multiple jar files and classes [duplicate] - java

This question already has answers here:
What does "Could not find or load main class" mean?
(61 answers)
Closed 4 years ago.
I am compiling a program with multiple jar files (inside the lib folder) and classes (inside the src/com folder) with:
javac -classpath lib/\* src/com/*.java
I typed this to run the program:
java -cp lib/\* src/com/okc
But it doesn't work. Instead, I get this:
Error: Could not find or load main class src.com.okc
okc.java is the class containing the main method. How can I run a java program with multiple jar files and classes?

A Java class file is not just the file itself. The directory structure which represents a class's package is part of the class file. Your classpath needs to point to the directory which is the parent of the topmost package directory.
Assuming your class is declared with package com;, the topmost package directory is com. So you need the parent of com in your classpath:
java -classpath src:lib/\* com.okc
If your class does not contain any package statement, and you just happened to put it in a com directory, then it belongs to the null package, whose parent directory is com itself:
java -classpath src/com:lib/\* okc
An additional note: It is Java convention to have class names, and their respective file names, start with an uppercase letter. One reason is that it makes class names easy to distinguish from package components.

Try:
java -cp ../lib/\* com.okc
from the src directory (not sure...)

Assuming that your current directory has your lib/ :
java -cp lib src.com.okc

Related

Compiling java class from command line defined in a package [duplicate]

This question already has answers here:
What does "Could not find or load main class" mean?
(61 answers)
Closed 1 year ago.
I'm able to compile and run a simple hello world program from command line with
javac hello.java
java hello
however, if I had a package statement at the top package com.mypackage.myclass and try the same I get
Error: Could not find or load main class Reflection
What is exactly happening? And how do I fix it? Thanks.
UPDATE: Thanks everyone. I already had created the directory structure manually. In order for this to work I have to run java com.mypackage.myclass from the root directory, otherwise it will not work. Still don't understand the underlying mechanism. What is exactly happening?
If you have a java class like com.example.test.Main then the compiled class should be in a folder com\example\test and the class file name Main.class
You could manually do it, but compiler has a switch -d <target folder> which will create the folder structure for you.
javac -d . Main.java
In the above command the target folder is the current folder where you have the Java source file.
Once compiled you must use fully qualified name of the class to execute from the same folder you compiled the java file.
java com.example.test.Main
Screen Cap

How do you create java packages using notepad++? [duplicate]

This question already has answers here:
How to use Java packages? [duplicate]
(3 answers)
Closed 7 years ago.
I am trying to create a java file that will read in xml files but for me to do this i need to use a DOM Parser which involves creating a package and declaring it and i was just wondering how do you do that
Packages in Java classes reflect the folder structure of your project. For example, if your project is located in the project folder, and the structure of your project is:
project\
project\mypackage\
project\mypackage\MyClass.java
then the MyClass.java file should contain the corresponding package declaration:
package mypackage;
public class MyClass {
You should also read a tutorial on packages to better understand how they work: https://docs.oracle.com/javase/tutorial/java/package/index.html
A package is declared as the first non-comment line in your Java source file:
package an.example.pkg;
public class Foo {
// code here
}
Will declare a class named Foo in a package called an.example.pkg.
Of course, the package is associated with a folder on your file system, so your .java file would actually need to be located in a directory called <some directory>/an/example/pkg in your file system (where <some directory> is a directory that is in your Java classpath).
The package tells Java where to search to find your classes, i.e. in which folders relative to your classpath to look.

What does "Exception in thread \"main\" java.lang.NoClassDefFoundError" mean when executing java .class file?

Java and Gradle beginner's question.
I made a project directory for java and gradle test:
The directory hierarchy :
HelloWorld.java:
package foo.bar;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, world");
}
}
build.gradle:
apply plugin:'java'
Then,gradle build this project and generated what i need.
As you see above, my problem is why doesn't this execute correctly? Even through I cd to .class path.
======================================================================
While, if I remove package foo.bar; in HelloWorld.java, and repeat gradle commands and execute at he.bak directory then the error remained the same.
But when I cd to the directory where HelloWorld.java placed. everything goes OK!Why? something related with CLASSPATH environment variables or other causes?
////////////////////////////////////////////////////////////////////
UPDATE
////////////////////////////////////////////////////////////////////
Thought you guys' warm replies, I know that I should combine the CLASSPATH and the period-separated executable .class file to figure out what's going on when executing java class file.
I experiment my thought resulting in 2 point to this question:
The -cp option path parameter A/B plus the executable file c.d.e.class finally form the A/B/c.d.e.class full path where the class is actually located.
If I specify the package in source code file with package d,I must split the full path in the form of java -cp A/B/c/d e.class. split in other ways all will result in errors.
something I am not sure here is :
When I specify my package path in my source code file, It determined the only classpath when executing corresponding executable, right?
If it is the truth, How does a project with lots of package and sources files work?
What's the root principle?
When in build/classes/main try java foo.bar.HelloWorld instead of java HelloWorld
The reason you need to specify foo.bar.HelloWorld is because you specified package foo.bar;. This tells java that the class should be in foo/bar/HelloWorld and the fully qualified name for HelloWorld is foo.bar.HelloWorld. If you want to execute the class from a different working directory however, you can specify the classpath explicitly using the -cp option, e.g., java -cp c:\myproject\build\classes\main foo.bar.HelloWorld.
By the way, the classpath default is the current working directory (i.e., .) but java -cp c:\myproject\build\classes\main foo.bar.HelloWorld will NOT have the classpath set to the current working directory if it is explicitly set using the -cp option. If you want to include the current working directory but explicitly set it, or even add more directories, you can chain them using semicolons like this: java -cp .;c:\myproject\build\classes\main foo.bar.HelloWorld. So this will include both the current working directory and the directory I specified.

Running Java Program from Command-line

I downloaded a java program that consists of two folders src and classes containing the source files and class files respectively. Now, the src and classes folders contain a several nested sub-folders wherein the last sub-folder contains the source and class files respectively. More precisely, the path to a source and class file is src/edu/univ/.java and classes/edu/univ/.class. Given that the file containing the main function is Main.java, how can I run this program from command-line.
I have tried:
java src/edu/univ/Main but I get Exception in thread "main" java.lang.NoClassDefFoundError: src/edu/univ/Main
I have also tried:
java src.edu.univ.Main but I encounter a similar error
From the root of the project:
java -cp classes edu.univ.Main
This tells the JRE that the classes directory is the root of your package hierarchy. The JRE will load packages from there, following the directory/package naming hierarchy.
http://docs.oracle.com/javase/tutorial/java/package/managingfiles.html
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/classpath.html

Do not understand following use of -classpath by javac

I am not very clear with the following question from SCJP Book (I read the solution and explanation though) ..
Consider the following directory structure :-
foo --> test --> xcom --> A.class, B.java
Here foo, test and xcom are directories. A.class and B.java are the files in xcom directory.
Following are the source codes of corresponding files:-
A.java
package xcom;
public class A { }
B.java
package xcom;
public class B extends A { }
The default classpath is /foo.
Now, in order to compile B.java, I keep my current directory as test and give :-
javac -classpath xcom xcom/B.java
Here I give the classpath as xcom which has A.class. But still it does not find class A. Why is it so??
If your classes are in package xcom, then your classpath needs to be at the directory directly above that. In this case, the classpath should be foo/test.
And if your current directory is foo/test, then this should be your javac:
javac -classpath . xcom/B.java
Because you have to specify classpath root to -classpath argument, like javac -classpath . xcom/B.java. To compile class B java compiler requires class A, it tries to locate class A file in {classpathroot}/xcom/.
Note: . - is a current directory
I think the root cause here is a misunderstanding of a "fully-qualified name" in Java.
The fully-qualified names of your two classes are xcom.A and xcom.B. Their source is in files A.java and B.java in a directory named xcom; the fully-qualified names dictate the directory structure. When you are going to use the files, either to compile them or run them, the classpath contains one or more locations from which the fully-qualified names can be found; so java is looking for xcom\A.java and xcom\B.java (when compiling) and xcom\A.class and xcom\B.class (when running).
That is why the classpath needs to specify the directory that contains xcom.
As you progress to more complex environments: the classpath can be a list of such locations; each location is separated by a semicolon on windows and a colon on unix systems. Each location can be a directory, as you've already seen, but it can also be a jar file. jar files are in zip file format, and zip files have a directory structure just like disks do. So you could zip up your class files, maintaining their xcom parent (but not their full paths), and specify the jar file in the classpath instead of a directory.
I know the question was already answered somewhat, but thought you might like the background explanation as well.

Categories

Resources