aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes javac options Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "javac options" Watch "javac options" New topic
Author

javac options

Valentin Ivanov
Ranch Hand

Joined: Nov 20, 2008
Posts: 38

Hi

classpath variable is not defined for both Windows and Linux /Ubuntu/


WindowsXP

File -> c:\documents and settings\fbi\desktop\base\B1.java
current directory c:\documents and settings\fbi

java -cp Desktop\base B1 - OK
java -cp "c:\documents and settings\fbi\desktop\base" B1 - OK
java -cp c:\documents and settings\fbi\desktop\base B1 - NOT OK

javac -cp desktop\base\ B1.java - NOT OK
javac -classpath desktop\base B1.java - NOT OK
javac -cp "c:\documents and settings\fbi\desktop\base" B1.java - NOT OK
javac -classpath "c:\documents and settings\fbi\desktop\base" B1.java - NOT OK
current dir to c:\documents and settings\fbi\desktop\base\
javac B1.java - OK

Ubuntu

File -> /home/valentino/base/B1.java
current directory "home"

java -cp valentino/base B1 - OK
java -cp /home/valentino/base B1 - OK

javac -cp /home/valentino/base B1.java - NOT OK
javac -cp valentino/base B1.java - NOT OK
javac -cp /valentino/base B1.java - NOT OK
current dir to /home/valentino/base
javac B1.java - OK

So why javac seems to be not working ? What went wrong?
The B1 class is not in any package, so all i need is just to tell the javac where it is, don't I?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

So why javac seems to be not working ? What went wrong?


What's the error message? We highly doubt that the command prompt just returns "NOT OK"...

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6662
    
    5

The CP must either point to the folder that contains the classes or to a JAR file. Some of your CPs are pointing to source files which will not help


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Sonali Sehgal
Ranch Hand

Joined: Jul 09, 2009
Posts: 75
For Windows XP

Your command prompt must be in where the file is located and then you can use the command:-


javac -classpath com:. -g Filename.java
The above is the right syntax to execute the java file

The -classpath which itself has an argument of com:. and -g and then passes it to the compiler and it compiles.
Ken Truitt
Ranch Hand

Joined: Aug 23, 2007
Posts: 124
javac file arguments are always going to be the path relative to the current directory, even if they
are not part of a package:

file: /home/ken/java/test.java //default package
current dir: /
javac invocation: javac home/ken/java/test.java

___

file: /home/ken/java/com/test.java //package com
current dir: /
javac invocation: javac home/ken/java/com/test.java

Even when test.java is in package com, the -cp option cannot be used (as far as I know). You use -cp in conjunction with
javac when you need .class files from another package, basically.

And this is different from -cp in conjunction with java, where the -cp argument should be the directory(ies) that contain(s)
the first directory in the package.


SCJP 88% | SCWCD 84%
Valentin Ivanov
Ranch Hand

Joined: Nov 20, 2008
Posts: 38

Henry the output is:

javac: file not found: B1.java
Usage: javac <options> <source files>
use -help for a list of possible options


I know what that message means, but I don't know why javac can't find the file. I think i use -cp option correct, don't I?

Deepak thanks for helping me, but a think all of my CP's are pointing to "base" directory which contains the B1.java
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14272
    
  21

The classpath is only used to find compiled classes, not to find source files. So setting the classpath to a directory that contains your source code will not work. You can just enter the whole path to your source file, like this:

javac C:\some\directory\MyProgram.java


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Valentin Ivanov
Ranch Hand

Joined: Nov 20, 2008
Posts: 38

Jesper Young wrote:The classpath is only used to find compiled classes, not to find source files. So setting the classpath to a directory that contains your source code will not work.

Thanks, i didn't know -cp is looking only for compiled files and not source,
You can just enter the whole path to your source file, like this:

javac C:\some\directory\MyProgram.java


i did it /I've shown above/ , but still not working
Ken Truitt
Ranch Hand

Joined: Aug 23, 2007
Posts: 124
if using the absolute path isn't working with javac, you may have a problem with your $PATH variable (Windows environmental variable).
And with the Ubuntu path variable.

Either that or you had a typo. Anyway, the concept is that javac doesn't care about packages so you need to give it an argument
that tells it where the actual .java file is, either a relative path or an absolute path. You don't use -cp with javac, I believe, unless you are
pointing to .class files used for compilation of your .java file.

I did read that if the source file for the class file you point to with -cp is found [in the same directory,] the source file may be recompiled, rather than using the .class file. see the javac man page for more info.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: javac options