If your Main class is in a package called FileManagement, then try:
java -cp . FileManagement.Main
in the parent folder of the FileManagement folder.
If your Main class is not in a package (the default package) then cd to the FileManagement folder and try:
java -cp . Main
More info about the CLASSPATH and how the JRE find classes:
- How Classes are Found
- Setting the class path (Solaris/Linux)
- http://en.wikipedia.org/wiki/Classpath_(Java)
Videos
If your Main class is in a package called FileManagement, then try:
java -cp . FileManagement.Main
in the parent folder of the FileManagement folder.
If your Main class is not in a package (the default package) then cd to the FileManagement folder and try:
java -cp . Main
More info about the CLASSPATH and how the JRE find classes:
- How Classes are Found
- Setting the class path (Solaris/Linux)
- http://en.wikipedia.org/wiki/Classpath_(Java)
Guys let's understand the syntax of it.
If class file is present in the Current Dir.
java -cp . fileName
If class file is present within the Dir. Go to the Parent Dir and enter below cmd.
java -cp . dir1.dir2.dir3.fileName
If there is a dependency on external jars then,
java -cp .:./jarName1:./jarName2 fileName
Hope this helps.
To compile the file, open your terminal and type
javac filename.java
To run the generated class file, use
java filename
But to do this you need to have the Java JDK installed in your computer. You can install it with the instructions in How do I install Java?.
OpenJDK works best for me. It's simple and I have never faced any problem with it. Just follow these simple steps:
From Terminal install open jdk
sudo apt-get install openjdk-7-jdkWrite a java program and save the file as filename.java
Now to compile use this command from the terminal
javac filename.javaIf everything works well then a new "filename.class" file should be created.
To run your program that you've just compiled type the command below in terminal:
java filename
NOTE
You can use any text editor (like gedit) ,
replace the filename with watever name you want
you need to be on same directory as the "present working directory" (got by running pwd) while running the command from terminal.