• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java; Can not find .java file from any directory  RSS feed

 
Anastassia Koustova
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so my problem is that javac can not find the java file unless I am directly in the file already

What I have tried:

-Making a PATH and CLASSPATH to the file folder.
- trying to -d path\to file tells me that it can not find the source file. (I would like to make it clear that I do not need it to compile to this directory. I need javac to be able to find this file, no matter what directory I start in on the cmd line

-I got java to run the class folder from any directory but I can not get javac to,I always have to be at the folder in the command line e.g. c:\User\Sh\Desktop\JavaProg>javac Example.java (This works but only if im already in JavaPRog)

How do I make it work from any directory?

 

Thank you
 
Ron McLeod
Bartender
Posts: 1602
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The -d option specifies where to put the complied class files, not the location of the source java files.  To specifiy the the location of your source, simply include the path to the file.
D:\>javac -d \JavaRanch\bin  \JavaRanch\src\jr92\HelloW.java

D:\>cd \JavaRanch\bin
D:\JavaRanch\bin>java jr92.HelloW

Hello W!
 
Ron McLeod
Bartender
Posts: 1602
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
.. and Welcome to the Ranch! 
 
Anastassia Koustova
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you!
Is there a way I don't have to state the path every time and can javac the .class file from any directory?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The purpose of PATH is to help find / run the javac compiler from any directory.

The purpose of CLASSPATH is to help find / use the class files, either by the JVM or Java compiler, from any directory.

I don't think there is an equivalent for Java source files...


Anyway, I guess one option would be to wrap javac with a batch file (or shellscript for UNIX). This batch file can call the javac command, using some sort of environment variable, such as SRCPATH. It is still doing the same command, which states the path, but you don't have to type it over. The source path is specified via the environment variable, and the batch file is configured to use it.

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!