does javac sourcepath work? - java

I am trying to use javac -sourcepath but I feeling I am doing something really stupid.
[leoks#x ~]$ pwd
/home/leoks
[leoks#x ~]$ /opt/jdk1.7.0_40/bin/javac -sourcepath /home/leoks/EclipseIndigo/workspace2/sand/src/ Hello.java
javac: file not found: Hello.java
Usage: javac <options> <source files>
use -help for a list of possible options
[leoks#x ~]$ /opt/jdk1.7.0_40/bin/javac /home/leoks/EclipseIndigo/workspace2/sand/src/Hello.java
[leoks#x ~]$ /opt/jdk1.7.0_40/bin/javac -help
Usage: javac <options> <source files>
where possible options include:
(...)
-sourcepath <path> Specify where to find input source files
I know nobody uses this option, but is this option broken? Or am I missing something here?

Put your source into a package and try again:
[leoks#x ~]$ /opt/jdk1.7.0_40/bin/javac -sourcepath /home/leoks/EclipseIndigo/workspace2/sand/src/ t/Hello.java
It is not good practice to put classes into the default package.

Related

Running Javac Command with classpath on linux

I have a folder Called TutorialFolder. Inside this, i have tutorial_class folder and WordCount.java file.
When I run wordcount program, it is giving below error.
hduser#ubuntu:~/Desktop/TutorialFolder$ javac -classpath ${HADOOP_CLASSPATH}-d '/home/hduser/Desktop/TutorialFolder/tutorial_class' '/home/hduser/Desktop/TutorialFolder/WordCount.java'
javac: invalid flag: /home/hduser/Desktop/TutorialFolder/tutorial_class
Usage: javac <options> <source files>
use -help for a list of possible options
The problem here is with the variable ${HADOOP_CLASSPATH} which is not set or empty. Thus the command is interpreted as,
javac -classpath -d /home/hduser/Desktop/TutorialFolder/tutorial_class /home/hduser/Desktop/TutorialFolder/WordCount.java
Fix the $HADOOP_CLASSPATH variable, the command should work.
Or, Try with hadoop classpath command
javac -classpath `hadoop classpath` -d /home/hduser/Desktop/TutorialFolder/tutorial_class /home/hduser/Desktop/TutorialFolder/WordCount.java

Javac classpath (.java file)

I want to compile a java file from the command line using this command: javac -cp C:/Java myPgm.java but it keeps saying that
javac: file not found: myPgm.java
Usage: javac <options> <source files>
use -help for a list of possible options
any help please? ;)
run the following code in the terminal to compile the java source code
javac C:/Java/myPgm.java
then to execute the code
java theClassNameOfTheJavaFile
go to your current directory then
type
javac myPgm.java
then
java myPgm
if program inside package then
javac -d . myPgm.java
and then
java package_name.myPgm

Compiling Java from Command Line

I'm following this tutorial on how to build an Android Plugin for Unity
I'm currently at the part where the author tells me to do the following in command line:
1.> javac CompassActivity.java -classpath C:\Program Files (x86)\Unity\Editor\Data\PlaybackEngines\androidplayer\bin\classes.jar
-bootclasspath C:\android-sdk-windows\platforms\android-8\android.jar -d .
2.> javap -s com.yourcompany.yourgamename.CompassActivity
3.> jar cvfM ../Compass.jar com/
However when I type the following line:
javac CompassActivity.java -classpath C:\Program Files (x86)\Unity\Editor\Data\PlaybackEngines\androidplayer\bin\classes.jar
I get the following message:
javac: invalid flags: (x86)\Unity\Editor\Data\PlaybackEngines\androidplayer\bin\classes.jar
usage: javac <options> <source files>
use -help for a list of possible options
So I've tried retyping the line putting my path of the file in angled brackets, placing a dot in between classpath and the start of my file location, but I keep getting the same issue.
Am I using classpath wrong?
If so, what is the correct way I should be doing it?
I should add that the console does point to the correct folder location. That was the first thing I've checked.
There are spaces in the path to classes.jar, you must enclose it using ", or shell will consider it as three distinct parameters (C:\Program, Files and (x86)\Unity\Editor\Data\PlaybackEngines\androidplayer\bin\classes.jar"):
javac CompassActivity.java -classpath "C:\Program Files (x86)\Unity\Editor\Data\PlaybackEngines\androidplayer\bin\classes.jar"
You must try the command like:
usage: javac <options> <source files>
javac -classpath "C:\Program Files (x86)\Unity\Editor\Data\PlaybackEngines\androidplayer\bin\classes.jar" CompassActivity.java
First Check your System is 32-bit or 64-bit
check it out full steps for Config and run:http://introcs.cs.princeton.edu/java/15inout/windows-cmd.html
USe
javac -cp filepath
or you also try to set the classpath first by command
set classpath="filepath"
Then u can try with a command
java filepath

javac: not a directory: src\com\example\web\BeerSelect.java

I am on chapter 3 of Headfirst Servlets and JSP.
This is my input to windows 7 command line:
D:\Workspaces\ServletsJSP\MyProjects\beerV1>javac -classpath
C:\Tomcat\tomcat\lib\servlet-api.jar;classes;. -d
src\com\example\web\BeerSelect.java
This is the error message:
javac: not a directory: src\com\example\web\BeerSelect.java
Usage: javac <options> <source files>
use -help for a list of possible options
I have BeerSelect.java in this directory: D:\Workspaces\ServletsJSP\MyProjects\beerV1\src\com\example\web
The -d flag is used to specify which directory to output to. You seem to have omitted it's argument, so it thinks you want to output into a directory called src\com\example\web\BeerSelect.java, which of course is not a directory.
You haven't specified the directory of the -d flag. This flag is used to specify where the class file should be put. You probably want to change your command to something like this:
D:\Workspaces\ServletsJSP\MyProjects\beerV1>javac -classpath C:\Tomcat\tomcat\lib\servlet-api.jar;classes;. -d bin src\com\example\web\BeerSelect.java
Assuming bin is where you want your class files.

javac no source files found

I have the .java file on the current working directory but javac reports:
javac: no source files
Usage: javac <options> <source files>
use -help for a list of possible options
I'm working on ubuntu.
From your comment above, it looks like you tried:
javac -cp .;lib.jar a.java on your Ubuntu system. The CLASSPATH separator is : on Unix systems and ; on Windows.
Ubuntu considered the command up to the ;, java -cp . and thus gave the message.
javac -cp .:lib.jar a.java should compile fine.
For anyone who is using powersehll on windows use CLASSPATH separator : instead of ;
I tried a similar thing and found that you need to mention the absolute path when you are using the
-cp and -d option with javac like this
javac -cp 'ur location of jars & files'; -d 'location to add your classes to' 'absolute path of file'
eg:
javac -cp C:\home\lib\mywork; -d c:\home\classes c:\home\files*.java
for javac, there are options and arguments
arg: it takes argument as path of source file
options: we require for basic compilation
-sourcepath: the path of dependent source files
-d: directory path of output classes
javac -sourcepath './src' -d './bin' -verbose './src/App.java'

Categories

Resources