Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: Trouble running a java file from the windows command prompt.

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    9
    My Mood
    Busy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Trouble running a java file from the windows 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.


  2. #2
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Trouble running a java file from the windows command prompt.

    Quote Originally Posted by JavaEnthusiast View Post
    ...Please help.
    Here's the sequence:

    javac Example.java

    If no errors then

    java Example


    Cheers!

    Z

  3. #3
    Junior Member
    Join Date
    Jul 2012
    Posts
    9
    My Mood
    Busy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trouble running a java file from the windows command prompt.

    Apparently it doesn't work.. It says file not found. I've set all the variables. Is there anything els I can do?

  4. #4
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Trouble running a java file from the windows command prompt.

    Quote Originally Posted by JavaEnthusiast View Post
    ...doesn't work...
    Instead of "doesn't work," I think we might save some bandwidth if you tell us exactly what doesn't work. Describe the steps you took and paste exact error messages into your post (don't abbreviate; don't paraphrase).

    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.)
    public class Example
    {
        public static void main(String [] args)
        {
            System.out.println("This is an example.");
        }
    }

    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 02:09 PM.

  5. #5
    Junior Member
    Join Date
    Jul 2012
    Posts
    9
    My Mood
    Busy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trouble running a java file from the windows command prompt.

    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.

  6. #6
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Trouble running a java file from the windows command prompt.

    Quote Originally Posted by JavaEnthusiast View Post
    ...
    I'm atttaching screenprint of the package and console. I think there's an issue ...
    I tried to give the most complete set of step-by-step instructions that I could come up with. Did you try those steps, starting with a completely empty directory? Did those steps work? Did any part of those steps work? (I wasn't using a package. Just compile and run the exact file that I posted. Don't edit it!)


    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 06:01 PM.

  7. #7
    Junior Member
    Join Date
    Jul 2012
    Posts
    9
    My Mood
    Busy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Re: Trouble running a java file from the windows command prompt.

    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

  8. #8
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Trouble running a java file from the windows command prompt.

    Quote Originally Posted by JavaEnthusiast View Post
    Yes, when I followed the exact steps it worked...
    All I wanted to do was to make sure your installation files and paths were correct. When you changed things and didn't tell me what you changed (like having a package statement) it made it a matter of guesswork.
    Quote Originally Posted by JavaEnthusiast
    Could you please explain...]
    Compile and run Java program in package from command line | Shiva Blog

    Java Practices -> Command line operations


    Cheers!

    Z

  9. #9
    Junior Member
    Join Date
    Jul 2012
    Posts
    9
    My Mood
    Busy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trouble running a java file from the windows command prompt.

    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

  10. #10
    Junior Member
    Join Date
    Jul 2012
    Posts
    9
    My Mood
    Busy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trouble running a java file from the windows command prompt.

    I agree.. I'm sorry for being vague. Could you please check my recent post? I really appreciate your help.

  11. #11
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Trouble running a java file from the windows command prompt.

    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

    //
    // File: Project1\test\TestMe.java
    //
    //  Zaphod_b
    //
    package test;
     
    import math.Factorial;
     
    public class TestMe
    {
     
        public static void main (String [] args)
        {
            Factorial ex = new Factorial();
     
            int n = 8;
     
            System.out.println("Factorial of " + n + " is " + ex.fact(8));
        }
    }

    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

  12. #12
    Junior Member
    Join Date
    Jul 2012
    Posts
    9
    My Mood
    Busy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trouble running a java file from the windows command prompt.

    Hey Z,

    Thanks a bunch. It worked like charm.

  13. #13
    Junior Member
    Join Date
    Aug 2012
    Location
    pune
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trouble running a java file from the windows command prompt.

    I think u mistale to ur enviroment.
    Last edited by sidhanath.chavan; August 2nd, 2012 at 10:17 PM.

Similar Threads

  1. Java command prompt
    By Tanmaysinha in forum Java Theory & Questions
    Replies: 2
    Last Post: August 30th, 2011, 09:30 AM
  2. Java command prompt
    By Tanmaysinha in forum Java Theory & Questions
    Replies: 1
    Last Post: August 29th, 2011, 03:05 PM
  3. Replies: 4
    Last Post: August 17th, 2011, 02:53 PM
  4. Java on command prompt
    By Tanmaysinha in forum What's Wrong With My Code?
    Replies: 12
    Last Post: August 17th, 2011, 06:45 AM
  5. [SOLVED] Redirect command prompt output to log file.
    By goldest in forum File I/O & Other I/O Streams
    Replies: 9
    Last Post: November 24th, 2010, 05:26 AM