Results 1 to 6 of 6
  1. #1
    alman9898 is offline Member
    Join Date
    Oct 2010
    Posts
    2
    Rep Power
    0

    Default Compiling java code from the command line

    This has been frustrating me intensely.

    I am trying to compile my source code from the Windows 7 command prompt. I have added the directory in which my *.java file is located to the classpath. Yet, I still get this error:

    C:\Users\Alex>javac HelloThere.java
    javac: file not found: HelloThere.java
    Usage: javac <options> <source files>
    use -help for a list of possible options

    I'm very confused as to why this happens because if I navigate to the folder where this file is located, it will compile. However, this is not a satisfactory solution since I intend on compiling JUnit tests directly from the command line as well...

    Other solutions I have attempted:

    C:\Users\Alex>javac -classpath "C:\Users\Alex\AndroidProject\UnitTest\src" Hello
    There.java
    javac: file not found: HelloThere.java
    Usage: javac <options> <source files>
    use -help for a list of possible options

    I do not think this has ANYTHING to do with typos...

    Images showing what I'm doing...





    Please tell me I'm just doing something stupid...because I have no idea why this will not work...

    As a side note, the path to the jdk\bin folder is in my PATH variable and works fine. So please no one suggest that.
    Last edited by alman9898; 10-11-2010 at 01:34 AM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    A few things about the classpath and the CLASSPATH environment variable:

    (1) The classpath specifies the path that Java will search on to find classes. (! But the important thing is it's for classes - the classpath has nothing to do with the location of java source files.)

    (2) These classes (that are on the classpath) are - from your point of view - data files. They are part of the data that you work with to create applications. Therefore jdk\bin should not be on the classpath. jdk\bin contains jdk applications and their resources and is not intended as a user data location.

    (3) Relying an the CLASSPATH environment variable is too limiting since each project may will?) require its own set of libraries and other classes. Specifying the classpath on the command line when you invoke java.exe or javac.exe is preferable and will override CLASSPATH. In fact the CLASSPATH variable might as well be removed unless you know you are using it for some other purpose.

    -----------------

    As far as reasonably complex (or even simple) compiling and building I would recommend Ant as it allows for a lot of possibilities without a lot of overhead.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Sorry - I just reread: you have jdk\bin on the PATH. That's a different matter and quite correct.

    Also I meant to say that javac has -src and -dest switches to indicate the source/output directories if these are different from the directory where you launch the compiler. But - especially for junit integration - using Ant is a lot easier than some of the heinously long command line invocations of javac.exe that can result.
    Last edited by pbrockway2; 10-11-2010 at 02:24 AM.

  4. #4
    alman9898 is offline Member
    Join Date
    Oct 2010
    Posts
    2
    Rep Power
    0

    Default

    I'm sorry, when using the -sourcepath switch it STILL cannot find my file. I don't understand what java's problem is.

    C:\Users\Alex>javac -sourcepath C:\Users\Alex\AndroidProject\UnitTest\src\ Hello
    There.java
    javac: file not found: HelloThere.java
    Usage: javac <options> <source files>
    use -help for a list of possible options

    What am I doing wrong? Note: I have tried doing this with and without quotation marks and with and without a trailing \

  5. #5
    TheGame is offline Member
    Join Date
    Oct 2010
    Posts
    9
    Rep Power
    0

    Default

    hmm..i don't think there's supposed to be a space between src\ and HelloThere.java

    or try using the path without the -sourcepath..
    like:
    javac C:\Users\Alex\AndroidProject\UnitTest\src\HelloThe re.java

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    You're right, it's -sourcepath.

    I have no idea why the file is not being found (if the file is as displayed in the screenshot)

    A few things:

    (0) You could try renaming HelloThere.java to something else and then back again. The idea is to eliminate something weird (invisible) with the file name.

    (1) Could you post the contents of HelloThere.java? Again, just to confirm the spelling of everything, and (lack of a) package declaration which you haven't actually talked about.

    (2) Is there some reason why, in the output you posted, the file name is broken in the middle? It should be HelloThere.java with no spaces.

    (3) What happens with a dir command? Ie what is the output from:

    Java Code:
    C:\Users\Alex> dir AndroidProject\UnitTest\src
    C:\Users\Alex> javac -sourcepath AndroidProject\UnitTest\src HelloThere.java

Similar Threads

  1. Replies: 2
    Last Post: 07-02-2010, 02:20 PM
  2. java command line arguments
    By Vipul03 in forum New To Java
    Replies: 1
    Last Post: 02-22-2010, 02:00 PM
  3. Replies: 1
    Last Post: 06-30-2009, 02:32 PM
  4. Java command line
    By agouzoul in forum New To Java
    Replies: 2
    Last Post: 04-02-2008, 12:12 PM
  5. Unable to execute command line command in java
    By LordSM in forum New To Java
    Replies: 1
    Last Post: 08-08-2007, 12:23 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
  •