Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Apparent Java Location Restriction

Reply
Thread Tools

Apparent Java Location Restriction

 
 
kvnsmnsn@hotmail.com
Guest
Posts: n/a
 
      06-17-2005
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_

 
Reply With Quote
 
 
 
 
Thomas Weidenfeller
Guest
Posts: n/a
 
      06-17-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) 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/...classpath.html

/Thomas

--
The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/...g/java/gui/faq
 
Reply With Quote
 
 
 
 
kvnsmnsn@hotmail.com
Guest
Posts: n/a
 
      06-17-2005
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_

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
adf java and <location></location> hehehe Javascript 1 07-06-2009 07:20 AM
Is there any restriction in C++ calling Java methods? Cal Java 4 02-27-2009 03:35 AM
Location, location, location =?Utf-8?B?VHJhY2V5?= Wireless Networking 2 02-17-2007 08:37 PM
Java superclass restriction design Arvind Java 4 11-10-2004 08:43 PM
s/// has apparent side effect on grep() John E. Jardine Perl 2 04-13-2004 08:45 PM



Advertisments