What is the significance of this `java` command? - java

I have below command in one of my legacy script and I am struggling to understand it properly. Can anybody help me here?
java -cp //applications/programs/myjar.jar:applications/programs/ojdbc14.zip mypkg.MyClass //application
Is it copying 2 files to "applications" and then executing it with "java"?

Please read the manual page http://www.manpagez.com/man/1/java/
-cp means class path and not copy

man java explains:
-classpath classpath
-cp classpath
Specifies a list of directories, JAR archives, and ZIP archives to search for class files. Class path entries are separated by colons (:). Specifying -classpath or -cp overrides any setting of the CLASSPATH environment variable.
If -classpath and -cp are not used and CLASSPATH is not set, the user class path consists of the current directory (.).

Related

Is there a difference between the -cp and -classpath options? [duplicate]

What's the difference between using
javac -cp classes helloworld.java
and
javac -classpath classes helloworld.java
in CMD?
They are the same, check http://docs.oracle.com/javase/7/docs/technotes/tools/windows/java.html
-classpath classpath
-cp classpath Specifies a list of directories, JAR files, and ZIP archives to search for class files. Separate class path entries with
semicolons (;). Specifying -classpath or -cp overrides any setting of
the CLASSPATH environment variable.
If -classpath and -cp are not used and CLASSPATH is not set, then the
user class path consists of the current directory (.).
As a special convenience, a class path element that contains a base
name of * is considered equivalent to specifying a list of all the
files in the directory with the extension .jar or .JAR. A Java program
cannot tell the difference between the two invocations.
For example, if directory mydir contains a.jar and b.JAR, then the
class path element mydir/* is expanded to a A.jar:b.JAR, except that
the order of jar files is unspecified. All jar files in the specified
directory, even hidden ones, are included in the list. A class path
entry consisting simply of * expands to a list of all the jar files in
the current directory. The CLASSPATH environment variable, where
defined, will be similarly expanded. Any class path wildcard expansion
occurs before the Java VM is started. No Java program will ever see
wild cards that are not expanded except by querying the environment.
For example, by calling System.getenv("CLASSPATH").
There's absolutely no difference. It just tells the Java compiler you want to use a custom classpath specified on the command line argument.
So -cp and -classpath are fully equivalent.
You can find more info on javac - Java programming language compiler page.
There is none. They're both options for setting the classpath. See the man page.

java -cp option seems not to work in Java 14 (Preview Enabled) [duplicate]

I've compiled a JAR file and specified the Main-Class in the manifest (I used the Eclipse Export function). My dependencies are all in a directory labeled lib. I can't seem to get a straight answer on how to execute my JAR file while specifying it should use the lib/* as the classpath.
I've tried:
]$ java -jar -cp .:lib/* MyJar.jar
]$ java -cp .:lib/* -jar MyJar.jar
]$ java -cp .:lib/* com.somepackage.subpackage.Main
etc...
Each gives an error saying:
Error: Could not find or load main class ....
or gives the NoClassDefFoundError indicating the libraries are not being found.
I even tried remaking the JAR file and included the lib directory and contents, but still no dice...
How can I execute a JAR file from the command line and specify the classpath to use?
When you specify -jar then the -cp parameter will be ignored.
From the documentation:
When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored.
You also cannot "include" needed jar files into another jar file (you would need to extract their contents and put the .class files into your jar file)
You have two options:
include all jar files from the lib directory into the manifest (you can use relative paths there)
Specify everything (including your jar) on the commandline using -cp:
java -cp MyJar.jar:lib/* com.somepackage.subpackage.Main
Run a jar file and specify a class path like this:
java -cp <jar_name.jar:libs/*> com.test.App
jar_name.jar is the full name of the JAR you want to execute
libs/* is a path to your dependency JARs
com.test.App is the fully qualified name of the class from the JAR that has the main(String[]) method
The jar and dependent jar should have execute permissions.
You can do these in unix shell:
java -cp MyJar.jar:lib/* com.somepackage.subpackage.Main
You can do these in windows powershell:
java -cp "MyJar.jar;lib\*" com.somepackage.subpackage.Main
Alternatively, use the manifest to specify the class-path and main-class if you like, so then you don't need to use -cp or specify the main class. In your case it would contain lines like this:
Main-Class: com.test.App
Class-Path: lib/one.jar lib/two.jar
Unfortunately you need to spell out each jar in the manifest (not a biggie as you only do once, and you can use a script to build the file or use a build tool like ANT or Maven or Gradle). And the reference has to be a relative or absolute directory to where you run the java -jar MyJar.jar.
Then execute it with
java -jar MyJar.jar
You can do a Runtime.getRuntime.exec(command) to relaunch the jar including classpath with args.

How to include jar files with java file and compile in command prompt

I have 3 jar files and a .java file that depends on these jar files. How do I compile the .java file with these jar files using a command prompt?
You can include your jar files in the "javac" command using the "-cp" option.
javac -cp ".:/home/path/mail.jar:/home/path/servlet.jar;" MyJavaFile.java
Instead of "-cp" you could also use "-classpath"
javac -classpath ".:/home/path/mail.jar:/home/path/servlet.jar:" MyJavaFile.java
You could including the jars every time you compile by setting the environment variable "CLASSPATH" correctly. The environment variable will store the path where the jars and classes that needs to be used for compiling/executing any java file. You will not have to include the jars individually every time you compile you file.
Different machines have different methods to set the classpath as an environment variable.
The commands for Windows, Linux, etc are different.
You can find more details in this blog.
http://javarevisited.blogspot.com/2011/01/how-classpath-work-in-java.html
Please try on Linux
javac -cp jarfile source file
EXAMPLE :-
javac -cp .:/jars/* com/template/*.java
Syntax will work on windows dos command:
javac -cp ".;first.jar;second.jar;third.jar" MyJavaFile.java
The followings are steps,
Copy all jars and your .java file in a same folder (It will be easy to mention file names instead of mentioning long path. Though you can keep jar and .java in separate folders).
To compile,
javac -cp .:<file_1_name>.jar:<file_2_name>.jar <prog_name>.java
To execute,
java -cp .:<file_1_name>.jar:<file_2_name>.jar <prog_name>
I hope this helps!
Try to add all dependency jar files to your class path through environment variable settings or use the below steps:
Open command prompt.
Change directory to the location of you java
file that you would like compile.
Set the classpath for your dependency jar files as shown below:
set classpath=C:\Users\sarath_sivan\Desktop\jars\servlet-api.jar; C:\Users\sarath_sivan\Desktop\jars\spring-jdbc-3.0.2.RELEASE; C:\Users\sarath_sivan\Desktop\jars\spring-aop-3.0.2.RELEASE;
Now, you may compile your java file. (command: javac YourJavaFile.java)
Hope this will resolve your dependency issue.
This will create .class file:
javac -classpath "[jarname with specified path]" [java filename]
This will execute class file:
java -cp [jarname with specified path]: [java filename]
Try This.
javac -cp .:jars/jar1:jars/jar2:jars/jar3 com/source/*.java
javac -cp jars/jar1:jars/jar2:jars/jar3 abc.java
With -cp command we specify the path where to find the additional libraries which are required to compile the class. jar1, jar2 and jar3, available in jars folder are used to compile abc.java class.
You need to specify the dependencies in compile time as well as runtime
To compile use this format
javac -cp "*.jar;classfile_path" filename.java
Example:
javac -cp "ojdbc6.jar;c:\programs" Main.java
some times making following change works:
java -cp ".;%CLASSPATH%" classfilename
Note: ON Windows. For linux use $CLASSPATH instead.
If you are using Ubuntu:
/opt/JavaServices/sqlite $ export CLASSPATH=/opt/JarFiles/XXXX.jar:/opt/JarFiles/XXXX.jar:/opt/JavaServices/;javac SQLiteSample.java
Go to folder location (Out of package structure)
/opt/JavaServices $ export CLASSPATH=/opt/JarFiles/XXXXX.jar:/opt/JarFiles/XXXXX.jar:/opt/JavaServices/;java sqlite.SQLiteSample
Note: Please see the file locations and package names
Plenty of these answers helped me, but none that were exactly what I needed.
Assumptions:
Windows OS
JAR file and java file are in same directory
javac -cp <jar filename>.jar <filename>.java
java -cp <jar filename>.jar; <filename>
Keep in mind the syntax needs to exactly match. Cannot exclude file extensions or the semi colon.

Why this javac: File Not Found error?

Working with some basic java apps on CentOS 5 linux and I have my classpath set to point to home/pathToJava/bin which contains javac and java
and I have .java files in home/pathToFolderA/src
and home/pathToFolderB/gen-java
When I run javac and java in home/pathToFolderA/src everything works perfectly
But when I run javac from within home/pathToFolderB/gen-java on fileName.java I get a file not found error, specifically
javac: file Not found: fileName.java
Usage: javac <options> <source files>
Why could this be happening?
Thanks for all help
The classpath is used to find class files, not source files. (Nor is it used to find the java and javac binaries; those are found in your normal path.) You need to specify the files to compile explicitly:
javac /home/pathToFolderA/src/fileName.java
Obviously if you're already in /home/pathToFolderA/src then you can just use fileName.java because that's treated as being relative to your current directory.
You shouldn't set your classpath to point to your JDK bin directory -- instead it should be the PATH environment variable, which serves a different purpose to classpath. (The classpath defines a list of jars and directories containing compiled Java .class code; the PATH variable defines a list of paths where the shell needs to look and locate programs to execute when they are not found in the current directory -- so if you type for instance zip -- it would look in all the directories defined in PATH and figure out that zip program is located under /usr/bin)
Secondly if you want to compile sources from both directory you need to specify:
all the paths where the sources are (both home/pathToFolderA/src and home/pathToFolderB/gen-java)
the path where the compiled .class files to be generated
specify in the classpath any library you might use in your source files
To sum it up, it would be something like this to compile:
javac -d /home/pathToFolderWithResultsOfCompilation -classpath /path/to/some.jar:/path/to/another.jar home/pathToFolderA/src/*.java home/pathToFolderB/gen-java/*.java
and to run your compiled programs:
java -classpath /path/to/some.jar:/path/to/another.jar:/home/pathToFolderWithResultsOfCompilation full.name.of.your.Java
Working with some basic java apps on CentOS 5 linux and I have my classpath set to point to home/pathToJava/bin which contains javac and java
That's wrong. The classpath is used to find *.class files, not operating system specific executables. The bin directory of your JDK does not belong in the classpath. Note that the classpath is also not for finding *.java source files.
When you run javac you need to specify the path to the source file, if it isn't in the current directory.
make sure that your file name contain no spaces
Eg:
HelloWorld.java
usually the errors occur when you rename the file by copy past that will cause a space between the name and the dot (this is the mistake:HelloWorld .java).
and make sure you changed the directory to the same folder your file in
Without a listing of the directory "gen-java" and the exact command you're typing,my guess would be that you're trying to compile a file that doesn't exist. Linux is case sensitive, so maybe that's your problem. Or the file doesn't exist.

java compiler options i.e. javac -d

I'm compiling a program that has a package statement.
e.g.
package APPC_LU62.Runtime ;
I also have a pre-existing directory structure that matches the package statement.
C:\APPC_LU62\Runtime
How do I keep the javac compiler from creating the same directory structure within the pre-existing one ? i.e.
C:\APPC_LU62\Runtime\APPC_LU62\Runtime
It seems to me the compiler ought to be "smart" enough to check first for an existing directory structure before creating one.
Thanks
In general, the compiler expects the source files and outputs the class files according to the package structure.
If you don't give any -sourcepath (or -classpath if no sourcepath is given) options, the source files are searched relative to the current directory. If a source path is given, the source files are searched relative to this path (in addition to any file directly specified on the command line).
Similarly, if you don't specify any -d options, the class files will be put into directories according to the package structure, relative to the current directory. If you give an -d option, the class files will be put relative to the directory given by the option. Non-existing directories will be created here.
Thus, if you want to create the output in the same directory tree as your source files are, the usual way to go would be to change into the root of this tree (C:\ in your case), and from there call javac:
javac -classpath ... -sourcepath . APPC_LU62\Runtime\*.java
(or list only the java files you actually want to compile). Alternatively, you could add the -d C:\ and -sourcepath C:\ options, and then call the command from whereever you want:
javac -classpath ... -sourcepath C:\ -d C:\ C:\APPC_LU62\Runtime\*.java
The same is valid later for executing the classes with the java command: This also expects the classes in directories according to the package structure, with the root being a directory or jar file mentioned in the class path. Thus you will have to add C:\ to the -classpath for your java call.
(By the way, I would not use the root of some drive as the root of the package hierarchy - better move everything one directory down.)
Would have been better if you had actually posted your javac command and stated clearly where your sources are located, but here goes anyway: when you issue javac -d somedir blahblah.java, javac will create the appropriate directory structure (matching package name hierarchy) starting at directory somedir.
So in your case, you simply need to do:
javac -d c:\ your_files.java

Categories