Basic questions about Java CLASSPATH - java

Suppose I just created a package "example" and have two classes inside it, "Main" and "Helper".
With the simplest possible compilation (e.g., $javac Main.java Helper.java) I am already able to run it fine as long as I am in the directory containing the example package, by typing this in the command line:
$java example.Main
Questions:
Why would I want to set a CLASSPATH given I can already run the program? I am guessing to be able to type "$java example.Main" from any directory on my machine, but I am not sure.
What happens when I type "java -cp /path/to/your/java/class/file Main" on the command line? Right now I picture there's file containing all the different classpaths, and that command will just add another one to it. Is it the case?
Is there a difference between using "CLASSPATH=/path/to/your/java/class/file" and "java -cp /path/to/your/java/class/file Main" on the command line? How come the second one has the name of the class (i.e. Main) in the end?

Yea, pretty much. That of course assumes you have the path to java in your PATH variable
-cp or -classpath adds it's option (a string) in front of whatever is in your CLASSPATH
Yes, there is a difference. Using CLASSPATH is often more convenient as you tend to set your CLASSPATH once. From then on, java Main is enough to execute the main class. With java -cp /path/to/your/java/class/file Main you have to type the -cp /path/to/your/java/class/file every time.
That being said, both CLASSPATH and -cp or -classpath options usually contain entries pointing to directories containing java libraries used by your program, not the directory of your program itself.

Related

Can I use -cp argument twice when executing Java?

I am trying to run a Java application which has many dependencies. In the past I have use the following command to launch the application
java -cp "program.jar:jar1.jar:jar2.jar:jar3.jar:[...]" program
However as the list of dependencies have grown, I have moved them into an arguments file, the contents of this file are:
-cp "\
program.jar:\
jar1.jar:\
jar2.jar:\
jar3.jar:\
[...]"
And I am running the application with
java #arguments-file program
Everything up to this point works fine.
Sometimes I end up with beta versions of program.jar, they share all of the same dependencies, but program.jar is renamed program-beta.jar.
So to run the jar the following command would be used
java -cp "program-beta.jar:jar1.jar:jar2.jar:jar3.jar:[...]" program
or more specifically, I would use an environment variable, so that the same script can be used, and the variable would be set to either program.jar, or program-beta.jar, depending on the circumstance
java -cp "$PROGRAM_JAR:jar1.jar:jar2.jar:jar3.jar:[...]" program
Now that I am using an arguments file I was hoping to be able to be able to do something like:
java -cp "$PROGRAM_JAR" #arguments-file program
However using -cp twice causes one of the two to be ignored, resulting in a java.lang.ClassNotFoundException exception.
Is there any way around this that allows me to specify one jar file by name, but abstract away all of the others so that my java command isn't thousands of characters?
This will be running entirely on Linux, so any command line "magic", such as using grep is fine, so long as the resulting code is easily readable
You could just write two bash scripts production.sh and beta.sh that contain a reference on program.jar and program-beta.jar, respectively.
Also, the classpath can contain wildcards (see man-page), so if you can ensure that on disk exists only one of the two versions, you can write it like this:
java -cp "program*:jar1.jar:jar2.jar:jar3.jar:[...]"
In the long term, you might think about building/running it with Maven or Gradle, depending on your requirements.

Compiling packages in java at windows command line

I have trying to compile java files at the windows command line using commands such as:
java myProg once I have used javac to create class files.
Problems arise when I use packages with a number of source files.
Often but not always I get main not found errors even though a main exists.
I am not quite sure what some of the directives mean and that is why it seems hit or miss.
Question
what does -cp mean exactly? java -cp src\myDirectory.myfile
sometimes I see:
./ infront of source eg .\src\myDirectory.myfile
on other sites I have found
% javac -cp .;stdlib.jar MyProgram.java
% java -cp .;stdlib.jar MyProgram
while compiling a jar library with java source files
what doesthe ".;" mean?
basically how do I compile three java source java files in one package at the windows command line and what does -cp and .; mean?
-cp means class path if I'm not mistaken.
try reading the following java docs
-classpath path
Specifies the path javac uses to look up classes needed to run javac or being referenced by other classes you are compiling. Overrides the default or the CLASSPATH environment variable if it is set. Directories are separated by semi-colons. It is often useful for the directory containing the source files to be on the class path. You should always include the system classes at the end of the path. For example:
javac -classpath .;C:\users\dac\classes;C:\tools\java\classes ...
https://www.cis.upenn.edu/~bcpierce/courses/629/jdkdocs/tooldocs/win32/javac.html
Answering your question directly, -cp means classpath or path.
Details on commandline arguments used while compiling and running a Java application can be found here: javac - Java programming language compiler
Extracting the description of -cp from that page:
-cp path or -classpath path:
Specify where to find user class files, and (optionally) annotation processors and source files. This class path overrides the user class path in the CLASSPATH environment variable. If neither CLASSPATH, -cp nor -classpath is specified, the user class path consists of the current directory. See Setting the Class Path for more details.
. means the current directory.
To compile multiple files in a directory use the following:
javac *.java // compliles all java files in the dir
java MyClass // runs the particular file
There are also a bunch of other related questions that should help you resolve this:
How to run a java program from the command line
How do I run java program with multiple classes from cmd?
Problems running a java program from the command line interface
Can't run multiple-class program from command line using packages

How do I run java program with multiple classes from cmd?

At the moment I am looking for another way to run my Java program from command line, other than adding it to a JAR file. My program has the following number of classes:
The name of the program file - MyProgram
Main class - Server1
second class - Client Handler
Package name - Items
3rd class - User1
4th class - User2
The main class and client handler alongside the package will have to run first in order for user 1 & user 2 to run, because they are client classes and are dependent on the main class.
javac *.java // compliles all java files in the dir
java MyClass // runs the particular file
If one class is dependent on another class that hasn't been compiled yet, the program won't run. So you should compile all files before trying to run the program dependent on other files.
If your files are packaged, then something like this
javac com.mypackage/.*java
java com.mypackage.MyClass
you must ensure that you add the location of your .class file to your classpath. So, if its in the current folder then add . to your classpath. Note that the windows classpath separator is a semi-colon ie ;
javac -cp . PackageName/*.java
java -cp . PackageName/ClassName_Having_main
Example. Suppose you have the following
Package Named: com.test
Class Name: Hello (Having main)
Java file is located inside "src/com/test/Hello.java"
then, from outside directory:
$ cd src
$ javac -cp . com/test/*.java
$ java -cp . com/test/Hello
Note that you can add -d to specify output directory of your class files whenever compiling
$ javac -d output_directory -cp . com/test/Hello
In windows the same thing will be working too, I already tried
Check out this from Oracle official site
Once you compile your code, you then run this from the top level:
java -cp . com.myprogram.MyProgram
That order thing you describe doesn't matter. They all get compiled together, and MyProgram will reference Server1, etc.
It may be more then you want to tackle right now but you might want to consider a build system like Maven. To start try out; How do I make my first Maven project?
You can use it to predefine the build order and if you want have it create a jar for you (or not).
Sounds like you will just need to open multiple command prompts and compile and run them in the order you need them to run. Let me know if I misunderstood question.
TO EXECUTE TWO JAVA PROGRAMS WHICH DEPENDS TO EACH OTHER.
(for example:two files Complex.java and Solution.java, where Soultion.java depends upon Complex.java.
So Complex.java should be compiled first and then the class file of Complex must be linked with Solution.java and then Solution.class must be executed for Output.)
REFER THE IMAGE WITH SYNTAX.
STEP 1:
COMPILE Complex.java
compiling Complex.java
syntax-
javac -d [path_where_class_File_build] [path_of_the_file\filename.java]
(Solution.java and Complex.java are Linked. ie-Solution.java calls Complex.java)
STEP 2:
COMPILE Solution.java
compiling Solution.java with linking Complex.class
with linking Complex.class(above created in step 1)
syntax-
javac -d [path_where_class_File_build] -cp [path_of_the_first_class_created] [path_of_the_file\filename.java]]
STEP 3:
EXECUTE THE Solution.class
java -cp [path_of_second_class_created] [class_Name]
(created in Step 3)

Basic Java - Packaging

I decided to post this question after trying to find an answer for it, and couldn't find one.
I'm studying for OCJP and tried few simple codes. This is what I did and need to do.
Created two .java sources, say TestOne.java, TestTwo.java [using
notepad++]
Created a directory named "package1" and placed the two sources in
them.
Both the source files have "package package1;" as their first statement.
TestOne.java has one public class and one class with default access.
TestTwo.java has one default class with an object of the default
class in TestOne.java.
The main method is in this default class in TestTwo.java. It tries to invoke a method in
the referred object that was created, using TestOne.java default class.
So after all that was set up compiled TestOne.java then TestTwo.java by setting the flag "classpath" in javac [ javac -classpath ]. Complied. But when I tried to run it gave me an exception "Exception in thread "main" java.lang.NoClassDefFoundError". Does anyone know what's wrong ?
run the code after compile
compile javac TestTwo.java
run after compile java TestTwo
try this
javac -d path cname.java
so write the code like this
javac -d c:\main testone.java javac -d c:\main testtwo.java
c:\main should exist in your pc
then while executing
java -cp path pn.classname
so whichever class contains main (say test2)
java -cp c:\main package1.testtwo

Runtime error "wrong name: applicationarbre / Main"

I add the path of jdk in the environment variable "path", my position is on the directory of the application: "C:\Users\20900994t\Desktop\applicationArbre_2_1\src\applicationarbre"
I have 4 .java files .
I tried 2 methodes.
The first method were:
1. "javac Main.java" 2. "java Main"
The second method were:
1. "javac *. java" 2. "java Main.java"
These methods generate all of the files . classes of all the classes I have, but at the command line it shows me that the purpose of the other classes are not recognized and when I did "java Main", it shows me "NoClasseDefFoundError"
Actually, those method works well in environment Net Beans, however I need to execute it on command line with .bat file .
Thank's
I suspect you're running it like this:
// In directory applicationarbre
$ java Main
You should be running it like this, in the parent directory:
$ java applicationarbe.Main
You say you've tried several solutions, but you haven't explained what you've tried, or what happened when you tried them, which makes it hard to help you further.
Basically, you need to tell Java the fully-qualified name of the class you want to launch, and that class has to be available on the classpath, which is "the current directory" by default. Anything more than that and you'll need to give us more information.
Not like this:
java applicationarbre/Main
but like this:
java applicationarbre.Main
As for the classpath, maybe in your case this will be enough (if you are in the correct base directory below which are your classfiles):
java -cp . applicationarbre.Main
first compile the class in which main method resides
C:\foldername>javac ClassName.java
then run with statement
C:\foldername>java ClassName
hope it will work :)

Categories