Java command terminal - java

Alright so i am trying to run my java file but it's not doing what I want it to from terminal.
I have a main directory called packageTester.
packageTester contains src and bin
src has packageA packageB
pacakgeA has HelloA.java
packageB has HelloB.java
bin has my class files so
bin has packageA packageB
pacakgeA has HelloA.class
packageB has HelloB.class
To compile the files I used the following command when I was in the pacakageTester directory:
javac -d bin -sourcepath source src/package*/* , which works !
Now how do I run HelloB.class which contains the main method and has an object of HelloA.
I thought when at the packageTester directory, I can do:
java bin/packageB/HelloB
but that does not work because it cannot seem to find the .class file.
HELP will be greatly appreciated to figure out how to execute the file correctly

The root of bin should be in your classpath in order for packageB.HelloB to be found as packageB/HelloB.class while parsing the classpath.
The easiest way to do so is to change directory to bin and execute java packageB.HelloB from there.
Alternatively, you can execute java -cp bin packageB.HelloB from your packageTester directory, or from somewhere else if you replace bin by an absolute path.

Related

How do I get a JAR file in the current directory in the class path for a Java execution?

I have a class file Main.class which needs a JAR file abc.jar to run.
Both files are in the same directory. Now I try to run the class file with
java -cp "." Main
but I get a java.lang/NoClassDefFoundError.
I thought -cp "." tells the classpath to include the current directory, but somehow it doesn't.
How do I get this JAR file in the current directory on the class path?
Thanks to patrinox' comment I figured it out:
The JAR itself needs to be in the CLASSPATH property, not only the directory containing the JAR. Therefore the command line has to read:
java -cp ".:./abc.jar" Main

How to change user.dir ( or current directory in command prompt ) in java?

When I go to a directory and make a jar file in command prompt, it works correctly. But, when I am in dir1 and make a jar file from dir2, it makes the jar file in dir1 and the jar file has error :
couldn't find main class.
I thought it works if I change current directory by java code.
If it works from command prompt, it will be correct in java code.
C:\Users\username\Desktop>jar cfe Main.jar Main F:\Java\Files\Main.class
C:\Users\username\Desktop>java -jar F:\Java\Files\Main.jar
Error: Could not find or load main class Main
You are specifying that the Main.jar be generated in the current directory. Include the full path of the jar file when you are generating it:
C:\Users\username\Desktop>jar cfe F:\Java\Files\Main.jar Main F:\Java\Files\Main.class

Compiling a java file using javac and the command line

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.

-sourcepath vs -classpath

Studying for the oracle certification I am trying all the possible scenarios that might occur during the exam.
For example, here there is a little doubt about shell command line (unix based):
Let's imagine there is a folder called myProject and a sub folder called myProject/source.
File SubFile.java is in the folder myProject/source and another file File.java is in myProject folder.
By typing the following commands I get to different behaviors:
cd source (therefore, currently I am on "myProject/source")
javac -sourcepath ../ File.java
// The command ../ does not work to access "Folder"
then after compiling File.java from myProject folder and returning to the sub Folder if I try:
javac -classpath ../ SubFile.java
// with the flag -classpath it seems to accept the ../ syntax to access the super folder.
Do you know why it works like this? and moreover is there any chance to access the super folder with the -sourcepath flag?
It depends on whether SubFile also references File.
Consider the following code:
public class SubFile {
private static File file = new File();
}
Assumed that this file is located in your source folder, and assumed that you are in the source folder, then
javac -sourcepath ../ SubFile.java
will compile SubFile.java into SubFile.class inside the source folder, and will compile File.java into File.class in the parent folder. If there is no dependency between those files, then the compiler will not compile File.java (means, the compiler will not automatically compile all files on the sourcepath).
When compiling with -classpath, then the classpath is also searched for source files, unless you explicitly specify a separate sourcepath - in the following case, the compiler will throw an error (assumed that you have cleaned the File.class file before):
javac -classpath .. -sourcepath \temp SubFile.java
See also javac - Java programming language compiler and Differences between classpath and sourcepath options of javac for more information.
The important point from these two links is:
Note: Classes found through the class path may be subject to automatic recompilation if their sources are also found.

Java: tips to add classpath

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.

Categories

Resources