Results 1 to 11 of 11

Thread: Simple question

  1. #1
    Games2Design is offline Member
    Join Date
    Sep 2012
    Posts
    44
    Rep Power
    0

    Default Compiling without being in java file directory

    Everytime i have to compile a java file i am asked to "go into" directory where the file is at (as many of you know).
    So my question is - what must i/or can i do so that the compiler locates the file without the need of going into the directory manually.

    I got windows 7 32-bit
    Last edited by Games2Design; 09-29-2012 at 05:03 PM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Simple question

    Please go through the Forum Rules - particularly the third paragraph.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Simple question

    You need to specify the absolute or relative path of the source file. You also need to ensure that any and all dependencies are available on the classpath, usually fia the -cp flag to javac..

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    Games2Design is offline Member
    Join Date
    Sep 2012
    Posts
    44
    Rep Power
    0

    Default Re: Simple question

    Quote Originally Posted by DarrylBurke View Post
    You need to specify the absolute or relative path of the source file. You also need to ensure that any and all dependencies are available on the classpath, usually fia the -cp flag to javac..

    db
    Could you elaborate on how to do that or explain what you mean? Because i don't understand what you mean by source file, relative path, absolute path, -cp flag, etc.

    Pretty much everything :S

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default Re: Simple question

    Suppose you have a directory 'a' under the root; also suppose that directory /a has two subdirectories 'b' and 'c'; so they are /a/b and /a/c. Now suppose your current working directory is /a/b (i.e. you're 'in' that directory) and your source file YourFile.java is stored in directory /a/c. There are a couple of ways to compile that file:

    using a relative path: javac ../c/YourFile.java
    using an absolute path: javac /a/c/YourFile.java

    (use backslashes '\' if you're on a MS Windows machine).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    Games2Design is offline Member
    Join Date
    Sep 2012
    Posts
    44
    Rep Power
    0

    Default Re: Simple question

    I checked Youtube videos for help to set CLASSPATH. And i have java programs in a folder at C drive called "Java programs".

    So i made CLASSPATH as a user and system variable, with directory adress to the folder.

    But still, i can't just compile without having to write path or go into directories through CMD.

    I don't get why it's not working?


    CLASSPATH : Is a path that helps javac find the classes right? So why is it not working?

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Simple question

    We may need to see your attempts, your command window dumps and get a knowledge of the package structure of your class to help more.

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Simple question

    Don't set the system variables, use the -cp or -classpath flag on the javac / java /javaw command line. Also and probably more importantly, the classpath, whether read for a system variable, a command line, or a jar manifest, is where the java executables look for dependencies. When compiling a .java source file, you have to pass the path to the source file to javac exactly as JosAH explained.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    Games2Design is offline Member
    Join Date
    Sep 2012
    Posts
    44
    Rep Power
    0

    Default Re: Simple question

    I hope this is what you wanted to see? If there is anything else you need screenshot of let me know.
    Simple question-classpath.png

    Simple question-cmd.png

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Simple question

    I agree with Darryl -- you don't want to set the system classpath but rather want to set it on the command line when you make your javac and java calls using the -cp command.

    Sometimes all you have to do is pass in a period for *this* directory: -cp .

  11. #11
    Games2Design is offline Member
    Join Date
    Sep 2012
    Posts
    44
    Rep Power
    0

Similar Threads

  1. simple question
    By jwl in forum New To Java
    Replies: 4
    Last Post: 08-05-2012, 03:31 AM
  2. A simple question
    By chris.bos in forum New To Java
    Replies: 11
    Last Post: 12-02-2011, 06:37 PM
  3. Please help. Simple question
    By owencain in forum New To Java
    Replies: 14
    Last Post: 06-16-2011, 02:07 AM
  4. Simple question
    By Qsc in forum New To Java
    Replies: 6
    Last Post: 03-07-2011, 12:24 AM
  5. Probably a really simple question...
    By ibanez270dx in forum New To Java
    Replies: 0
    Last Post: 11-16-2007, 02:27 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •