Apparent Java Location Restriction

Discussion in 'Java' started by kvnsmnsn@hotmail.com, Jun 17, 2005.

  1. Guest

    The task before me is to write a compiler for a fairly simple
    programming language. I want to write a program that can be executed
    wherever the source files are for this programming language. I would
    like to write this program in Java because I'm used to Java and like
    it a lot.

    But I've noticed that in order to run a Java program you have to
    be in the directory where the ".class" file resides. Well, actually I
    think it's not quite that restrictive, but depending on the way you
    name your Java program there appears to be only one directory you can
    execute it from.

    For example if I take Java class <ArgCheck> defined like so:

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

    public class ArgCheck
    {
    public static void main ( String[] arguments)
    {
    int argmnt;

    for (argmnt = 0; argmnt < arguments.length; argmnt++)
    { System.out.println( argmnt + "/" + arguments[ argmnt] + "/");
    }
    }
    }

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

    it works fine when I run it with "java ArgCheck *" from the directory
    it's written in. But when I change directory to the parent directory
    and type in "java Alan/ArgCheck *" I get error message:

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

    Exception in thread "main" java.lang.NoClassDefFoundError:
    Alan/ArgCheck (wrong name: ArgCheck)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
    at java.security.SecureClassLoader.defineClass
    (SecureClassLoader.java:124)
    at java.net.URLClassLoader.defineClass
    (URLClassLoader.java:260)
    ...

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

    On the other hand, if I write this program in C, it runs just
    fine _regardless_ of where I execute it from. "./ArgCheck *" works in
    the directory where it resides, and "Alan/ArgCheck *" works in the pa-
    rent directory.

    So is this a problem inherent to Java, or is there a way to exe-
    cute a Java program from any directory you want? If I want to have my
    compiler in one directory, I guess I could just copy the ".class"
    files to the directory where I want to do the compiling, or, alter-
    nately, I could write my compiler in C and run it wherever I want. Am
    I forced to choose between just these two options?

    ---Kevin Simonson

    "You'll never get to heaven, or even to LA,
    if you don't believe there's a way."
    from _Why Not_
    , Jun 17, 2005
    #1
    1. Advertising

  2. wrote:
    > But I've noticed that in order to run a Java program you have to
    > be in the directory where the ".class" file resides. Well, actually I
    > think it's not quite that restrictive, but depending on the way you
    > name your Java program there appears to be only one directory you can
    > execute it from.


    Please do not spread such utter nonsense. Learn your your tools:

    http://java.sun.com/j2se/1.5.0/docs/tooldocs/windows/classpath.html

    /Thomas

    --
    The comp.lang.java.gui FAQ:
    ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
    Thomas Weidenfeller, Jun 17, 2005
    #2
    1. Advertising

  3. Guest

    Thomas Weidenfeller posted:

    => But I've noticed that in order to run a Java program you have
    to
    => be in the directory where the ".class" file resides. Well, actually
    I
    => think it's not quite that restrictive, but depending on the way you
    => name your Java program there appears to be only one directory you
    can
    => execute it from.
    =
    =Please do not spread such utter nonsense. Learn your your tools:
    =
    =http://java.sun.com/j2se/1.5.0/docs/tooldocs/windows/classpath.html

    I apologize for my misunderstanding of how Java works. I tried
    using the "-classpath" argument and it solves my problem completely.
    Thanks for pointing me in the right direction!

    ---Kevin Simonson

    "You'll never get to heaven, or even to LA,
    if you don't believe there's a way."
    from _Why Not_
    , Jun 17, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John E. Jardine

    s/// has apparent side effect on grep()

    John E. Jardine, Apr 12, 2004, in forum: Perl
    Replies:
    2
    Views:
    434
    John Jardine
    Apr 13, 2004
  2. Shari

    Apparent session crosstalk

    Shari, Sep 24, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    425
    Shari
    Oct 1, 2004
  3. Guest
    Replies:
    4
    Views:
    412
    Guest
    Jan 28, 2005
  4. Isaac Grover
    Replies:
    6
    Views:
    526
    Spartanicus
    Feb 1, 2004
  5. Integer Software
    Replies:
    3
    Views:
    690
    Tejal Joshi \(MSFT\)
    Apr 19, 2004
Loading...

Share This Page