Apparent Java Location Restriction

K

kvnsmnsn

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_
 
K

kvnsmnsn

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_
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top