I wonder if it's possible to run a program without packaging it into a jar.
For instance we have this:
-AppRoot
Main.class
-Misc
Math.class
OtherTools.class
-YetAnotherFolder
UsefulFunctions.class
Is this possible? The main method should be executed from command line or similar.
You can run this way from the AppRoot directory
javac -cp Misc/*:YetAnotherFolder/* Main.java //To compile
java -cp Misc/*:YetAnotherFolder/* Main // To run
Below is some documentation
-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 cur-
rent directory (.).
Add all (sub-)directories containing class files to classpath and use the class with the main method as argument of the java executable.
The directory structure is your package structure.
java -cp ./:./AppRoot:./AppRoot/Misc:./AppRoot/YetAnotherFolder AppRoot.Main
This should work if all dependencies are resolved and on the classpath.
Related
I want to run a java project in windows. I first compiled the .class file in linux. Copy back to windows. Now under the path H:\deletefiles has delete.class, delete.java, a.jar, b.jar. The package for class delete is deleteFiles.
My java class path is C:\program Files\Java\jre7\bin, Where I have no access to write.
I run in command prompt C:\program Files\Java\jre7\bin>
java -cp H:\deleteFiles\deleteFiles.delete
always has the problem could not find or load main class, what's the problem? thanks
You are missing the actual class to be run. The -cp H:\deleteFiles\deleteFiles.delete only defines the classpath to be used, but not which class you want to run (and you limit the classpath to a single class as well).
What you want is:
java -cp H:\deleteFiles\deleteFiles delete
Note the blank (space) between H:\deleteFiles\deleteFiles which means you are passing two parameters to the java command:
-cp H:\deleteFiles\deleteFiles - the classpath to use
delete - the class to run
If you need the classes that are part of the jar files, you need to add them to the classpath as well:
java -cp H:\deleteFiles\deleteFiles;H:\deleteFiles\deleteFiles\a.jar;H:\deleteFiles\deleteFiles\b.jar delete
You need to set the classpath to the location which contains the package hierarchy. If your package is named deleteFiles the location needs to contain a directory named deleteFiles which contains the class file.
In your example you would run it with
java -cp H:\ deleteFiles.delete
you should call delete.class in your java command line, like this:
java -cp H:\deleteFiles\delete
To execute a Java program you have two options. Using a class file or using a jar file.
If your program only contains a single source file, executing the class file would be fine. But if you have multiple sources, you would have to copy all of them. Then a jar would be more practicable.
For class:
java -cp <class path> <class name>
For jar (if the main class is set):
java -jar <jar file>
I am trying to learn more about javac and how to use developer tools for Java using the command line.
As far as I understood, the option -classpath is needed to specify the path where javac searches for our classes and resource files, if we are not in the current directory, because usually the class path is set to our current working directory.
This is my current working directory:
/Users/user1/Desktop
And I am trying to compile a .java file which is in:
/Users/user1/Desktop/PF/
and the file is called MainClass.java.
I am trying to compile it using the following command:
javac -classpath /PF MainClass.java
But it does not seem to work, in fact I keep receiving the following:
javac: file not found: MainClass.java
Usage: javac <options> <source files>
use -help for a list of possible options
What am I doing wrong?
Classpath is for .class files, not for .java files.
javac command needs correct path to the .java file to compile it. So
javac ./PF/MainClass.java
Will create the class file in current directory.
If your MainClass.java depends on any class files to compile correctly, then you put those class/jar files in classpath.
That isn't how the classpath works. You use the classpath to point to classes that your Java file needs in order to compile. You don't use the classpath to point to the Java file itself.
Either go into the PF directory and do this:
javac MainClass.java
That will create the MainClass.class file inside the PF directory. If instead you want to create the MainClass.class file on your desktop, then from your desktop, do this:
javac PF/MainClass.java
-classpath
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 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.
class path is used to specify the compiled sources that need to be used in your class. For example in this code if you are accessing another class then you should specify the location of the compiled sources of the that class.
In your case if don't have any class dependency then simply remove classpath option and compile using[navigate inside folder]
javac Mainclass.java
Remove the -classpath. And if you are in the place where the java file is required (which currently you arent) you can remove that PF/ too.
So I'm still a noob in Java and I'm experimenting around with a few things.
I recently created a .jar file for my class using jar cvf <name>.jar <source files> and then used that jar to compile my driver class (javac -cp <name>.jar Driver.java) though how do I now run that class using the jar?
I've tried the following 2 commands:
java Driver and,
java -cp <name>.jar Driver.
The first gives me a NoClassDefFoundError for the class used, whereas the latter just gave me a single line error.
Error: Could not find or load man class Driver
What am I doing wrong? Is it possible I'm confusing this for something else?
I'm trying to do as much as I can without the use of any IDE.
You should put jar file and compiler output into classpath and specify main class:
java -classpath "<name.jar>;classes" Driver
EDIT (thanks to Kayaman):
If you are running command from linux/unix you have to use ":" as separator (in Windows works ";"). "classes" is a path to folder containing compiler output.
When creating an executable jar ( jar which contain a class with the main method) you should tell the jar which is the mainClass to be executed and for that you should create a file called 'Manifest.mf'.
The file should contain this:
Main-Class: MyPackage.MyClass
And when creating the jar you should use this to include your manifest:
jar cfm MyJar.jar Manifest.mf MyPackage/*.class
And for launching your jar :
java -jar MyJar.jar
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.
I have both a library.jar and program.jar in Java folder.
What is the correct command line to run? One method I tried is:
C:>java -cp c:\java\library.jar;.\java\program.jar program [param]
Try
java -cp c:\java\library.jar;.\java\program.jar package.the.MainClass [param]
From http://download.oracle.com/javase/1.3/docs/tooldocs/win32/classpath.html
Folders and archive files
When classes are stored in a directory
(folder), like
c:\java\MyClasses\utility\myapp, then
the class path entry points to the
directory that contains the first
element of the package name. (in this
case,C:\java\MyClasses, since the
package name is utility.myapp.)
But when classes are stored in an
archive file (a .zip or .jar file) the
class path entry is the path to and
including the .zip or .jar file. For
example, to use a class library that
is in a .jar file, the command would
look something like this:
C:> java -classpath C:\java\MyClasses\myclasses.jar utility.myapp.Cool
Multiple specifications
To find class files in the directory
C:\java\MyClasses as well as classes
in C:\java\OtherClasses, you would set
the class path to:
C:> java -classpath C:\java\MyClasses;C:\java\OtherClasses ...
Note that the two paths are separated
by a semicolon.
If you intend for your program.jar to be an executable JAR, you'll have to run it this way (and read this):
java -jar program.jar
Classpath entries can also contain the wildcard(*) character. For example, the class path entry C:\java\* specifies all JAR files in the C:\java directory and will be expanded into C:\java\library.jar;C:\java\program.jar.