Untitled.jpg
I have configured the CLASSPATH and JAVA_HOME environmental variables in windows. Yet I'm not able to run my java file using the command prompt..
Untitled.jpg
I have configured the CLASSPATH and JAVA_HOME environmental variables in windows. Yet I'm not able to run my java file using the command prompt. Please help.
Apparently it doesn't work.. It says file not found.
I've set all the variables. Is there anything els I can do?I've set all the variables. Is there anything els I can do?
Anyhow...
Here are the steps I took on my Windows XP workstation.
1. Open a command window and navigate to some suitable place. On my system I use F:\home\Zaphod\java
I strongly recommend that people select path names and file names without spaces or other special characters for everything they are going to be doing with a command line, not just Java.
2. Use a text editor to create a brand new file named Example.java (I use gvim since that's the same editor I use all day, every day on my Linux systems.)
Note that if the public class name is Example, the file name must (yes, really: must) be Example.java
3. Paste the following into the edit window and save the file (Copy/Paste from here. Don't retype it.)
4. Execute the commands that I enumerated in my previous post.
Here: I'll show the entire enchilada from the command line (after saving the file in the text editor).
For this post, I made computer output blue and user input black:
======Begin stuff pasted from my console=======
F:\home\Zaphod\java>dir
Volume in drive F is Disk1a_40Gig
Volume Serial Number is 44FD-261A
Directory of F:\home\Zaphod\java
07/24/2012 11:35 AM <DIR> .
07/24/2012 11:35 AM <DIR> ..
07/24/2012 11:35 AM 139 Example.java
1 File(s) 139 bytes
2 Dir(s) 5,902,274,560 bytes free
F:\home\Zaphod\java>javac Example.java
F:\home\Zaphod\java>dir
Volume in drive F is Disk1a_40Gig
Volume Serial Number is 44FD-261A
Directory of F:\home\Zaphod\java
07/24/2012 11:36 AM <DIR> .
07/24/2012 11:36 AM <DIR> ..
07/24/2012 11:36 AM 427 Example.class
07/24/2012 11:35 AM 139 Example.java
2 File(s) 566 bytes
2 Dir(s) 5,902,274,560 bytes free
F:\home\Zaphod\java>java Example
This is an example.
=======End stuff pasted from my console========
If you don't get the same results, show us what happened on your system.
In particular, if there are any compiler messages, paste them into your post in their entirety. Don't paraphrase.
You might also tell us what version of Operating System you are using, and what version of Java.
For java versions, here's what I got:
F:\home\Zaphod\java>java -version
java version "1.6.0_33"
Java(TM) SE Runtime Environment (build 1.6.0_33-b03)
Java HotSpot(TM) Client VM (build 20.8-b03, mixed mode, sharing)
F:\home\Zaphod\java>javac -version
javac 1.6.0_33
Cheers!
Z
Last edited by Zaphod_b; July 24th, 2012 at 03:09 PM.
Untitled.jpgUntitled1.jpg
I'm atttaching screenprint of the package and console. I think there's an issue with the commands I'm using. Could someone please review?
Thanks.
Sowmya.
Anyhow...
Can you post your Test.java file? Paste the file into your post. Don't retype anything. Don't show a screen shot.
Put [code=java] before the first source line and put [/code] after the last source line.
Show us the results from "java -version" and "javac -version" in a command window. Show the results from a "dir" command in a command window. (Don't paste from Windows explorer or whatever...)
From a command line: Just put your project file(s) in a directory. Compile and execute from the command line in that directory.
In other words:
"Packages? We ain't got no packages! We don't need no packages! I don't have to show you any stinkin' packages!"
---Gold Hat in "Treasure of the Sierra Madre"
For more elaborate projects, especially when running from an IDE, packages are a very important organizational tool, but for now, let's just get something running, OK?
Cheers!
Z
Last edited by Zaphod_b; July 26th, 2012 at 07:01 PM.
Yes, when I followed the exact steps it worked. Thanks.
Could you please explain to me creating packages for a simple calculation like factorial?
Steps I'm following:
1. Created a dir java_learn/fact.
2. Created two packages algos and test.
3. I've the following java file in algos.
Untitled.jpg
4. I've the following file in test.
Untitled1.jpg
5. I'm navigating to test package and executing the Example file. Got the following errors.
Untitled1.jpg
Compile and run Java program in package from command line | Shiva BlogCompile and run Java program in package from command line | Shiva Blog
Originally Posted by JavaEnthusiastOriginally Posted by JavaEnthusiast
Java Practices -> Command line operations
Cheers!
Z
package test;
import math.Factorial;
public class TestMe {
public static void main
(String args[]) {
/**Factorial of a num- Main**/
Factorial ex = new
Factorial();
System.out.println("Factorial of 3
is "+ ex.fact(8));
}
}
--------------------------------------------
package math;
public class Factorial{
public int fact(int num){
int result;
if (num==1) return 1;
result = fact(num -1) * num;
return result;
}
}
--------------------------------------------------
Untitled1.jpg
I agree.. I'm sorry for being vague. Could you please check my recent post? I really appreciate your help.
Did you look at the links that I gave? I mean different people have different ways of organizing the projects, and I thought those were pretty representative (and I also thought they gave enough information...)
Oh, well...
Create a brand new empty project directory somewhere convenient.
(For the sanity of anyone whom you might inveigle into helping you in the future, don't create directories or file names with spaces.)
On my Windows XP workstation I have a directory named F:\home\Zaphod.
I'll create a new directory named Project1 in F:\home\Zaphod. You do something appropriate for your system. Create a brand new directory named Project1
Then here's the drill:
Navigate to that directory and create two new directories to hold your source files and create a directory to hold your class tree:
mkdir math
mkdir test
mkdir classes
cd into directory Project1\test
Create a new file named TestMe.java
Paste the following into TestMe.java
cd into directory Project1\math.
Create a new file named Factorial.java and paste the following into that file:
// // File Project1\math\Factorial.java // // Zaphod_b // package math; public class Factorial { public int fact(int num) { int result; if (num < 2) { return 1; } result = fact(num - 1) * num; return result; } }
Now cd back into Project1
From the Project1 directory, execute the following:
javac -d classes -classpath classes math/Factorial.java
If all went well, there will be a file named Factorial.class in the Projects1\classes\math directory
Then from the Project1 directory, execute:
javac -d classes -classpath classes test/TestMe.java
Now there should be a file named TestMe.class in the Projects1\classes\test directory
Finally: Drum roll please!
From the Project1 directory, execute:
java -classpath classes test.TestMe
My output (taa-daa):
Factorial of 8 is 40320
Cheers!
Z
Hey Z,
Thanks a bunch. It worked like charm.
I think u mistale to ur enviroment.I think u mistale to ur enviroment.
Last edited by sidhanath.chavan; August 2nd, 2012 at 11:17 PM.