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.
Related
I'm reading some book on advanced Java, and I'm instructed to compile .java file with the following command:
javac -d classes -classpath c:\somepath\somejar.jar;classes:.
I wonder:
Are :. these symbols represent one argument or : and . separately are different arguments after the classes keyword? what do they stand for?
Why do we need to provide classes:. at all after somejar.jar? as all of these combinations:
javac -d classes -classpath c:\somepath\somejar.jar;classes:.
javac -d classes -classpath c:\somepath\somejar.jar;classes:
javac -d classes -classpath c:\somepath\somejar.jar;classes
javac -d classes -classpath c:\somepath\somejar.jar;
work absolutely same. They produce precisely same .class files (226 bytes each) and when I deploy them within Web container, they work identically.
So what's the point in those symbols? how above four javac .. commands differ? Anyone who can explain this clearly, please?
The ; and :delimiters used in the classpath value are used in order to delimit multiple folders as the values of the classpath.
; is used for delimiting folders under the Windows OS.
: is used for delimiting folders under *nix OSs.
The -d classes argument in conjunction with classes:. implies that the compiled .class files will be placed in the classes folder instead of the standard placement based on the package hierarchy.
Therefore, the inclusion of classes as an additional argument of the classpath allows the referencing of previously compiled class files when compiling new java classes. The . is a classpath reference to the directory from which the javac command has been executed - the current working directory.
The only issue here is the mixing of the two delimiter types. Why this is done - I do not know.
References:
How to specify multiple class search directories in the classpath option of javac
Oracle documentation on classpath
Oracle documentation on javac
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 (.).
I am just wondering, if java has something similar to /usr/lib in C/C++, then we won't have to do
java -cp "lib/*" mypackage.MyClass
Instead we can put all our favorite jar files into some folder like /java/lib/ and do
java mypackage.MyClass
Standardization of such a location also saves you the trouble of having to put the same set of frequently used libraries into many projects repeatedly.
Does such a feature exist already?
What you are talking about is called the extension folder. Here are summarized some pros and cons about its usage.
Possible other ways ways:
Use the CLASSPATH environment variable. If -cp or -classpath parameters are not specified, the value of the CLASSPATH environment variable will be used as classpath (if it exists). More on setting the CLASSPATH: PATH and CLASSPATH
The META-INF/MANIFEST.MF file inside jars can specify the class path. Add the following line to specify required libraries:
Class-Path: lib/myjar.jar lib/someotherjar.jar
More on this: Adding Classes to the JAR File's Classpath
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.
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