javac no source files found - java

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'

Related

Linux equivalent of including the classpath during compilation

I'm following a guide that only includes compilation instructions on windows. How would one run this build.bat file on Linux?
The batch file looks like this:
#echo off
#echo Compiling...
javac -classpath ..\..\lib\OneWireAPI.jar;%classpath% -d . .\src\*.java
And when I run the javac command on Linux, it fails:
javac -classpath ../../lib/OneWireAPI.jar;%classpath% -d . ./src/ReadTemp.java
The output is:
javac: no source files
What is the correct way to do this?
On Linux, you have to use : (colon) in place of ; (semicolon) as the path separator in Java options.
Also, if you have a classpath variable, in most common Linux shells it is referenced by $classpath rather than by %classpath%
javac -classpath ../../lib/OneWireAPI.jar:$classpath -d . ./src/ReadTemp.java
You have two items that did not get translated correctly from Windows CMD to Unix:
Path separator ; should be :.
Environment variables should be changed from %classpath% to $CLASSPATH format. Note that pretty much everything is case-sensitive in Linux, including environment variable names, and the Java path is traditionally all-caps.
Try
javac -classpath ../../lib/OneWireAPI.jar:$CLASSPATH -d . ./src/ReadTemp.java

Import multiple libraries in makefile within Javac command

The problem
This is a problem I just faced using makefile in java, on Windows.
I wanted to set up my classpath with multiple path (libraries, etc.). The new command work by hands, but not from the makefile which throws me this error :
javac : no source files
Example
Let's say I have this makefile :
JFLAGS = -g
JARFLAGS = -cvfm
CLASSPATH = ./bin
LIBS = C:/java/lib/mylib.jar
SOURCEPATH = ./src/client
compileAll:
javac $(JFLAGS) -d $(CLASSPATH) -cp $(CLASSPATH)\;$(LIBS) $(SOURCEPATH )/*.java
jar $(JARFLAGS) app.jar bin/client/MANIFEST.MF bin/client/*.class
So the command line to compile the project is :
javac -g -d ./bin -cp ./bin;C:/java/libs/lib.jar ./src/client/*.java
It works well.
The class files goes to ./bin directory. It imports classes from ./bin and the lib.jar library. And it compliles all the source files from the ./src/client directory.
This command works perfectly by hands, but no from the makefile which doesn't compile anything.
Thanks to my text editor which colored the ';' character, I understood that I just needed to escape (disable) the ';' character by using a '\' :
javac -g -d ./bin -cp ./bin\;C:/java/libs/lib.jar ./src/client/*.java
Now, it works well from makefile !

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.

How can I compile and run a Java class in a different directory?

I'm writing a makefile that compiles a .java file in a different directory, and then I want to run it, without changing directories. I want to do something along the lines of:
$(SQM_JAVA_TOOL_DONE) : $(SQM_JAVA_TOOL)
$(shell cd /home_dir)
javac myjavafile.java
java myjavafile
where the Java file is /home/myjavafile.java, and the makefile isn't running from /home.
How can I do this?
I might be misunderstanding the question, but you can compile with
javac /home/MyJavaFile.java
This will create MyJavaFile.class in /home
You can then run it by including /home on the classpath. e.g.
java -cp /home MyJavaFile
If you want to generate the class file in a different directory then you can use the -d option to javac.
Use the -d command line parameter with javac to tell it what directory you'd like to store the compiled class files in. Then, to run the program, simply include this directory in the classpath:
javac -d some/directory myjavafile.java
java -cp some/directory myjavafile
Just to add to the existing answers, you may want the --source-path flag:
--source-path <path>, -sourcepath <path>
Specify where to find input source files
I believe this effectively sets the package root javac will compile from (i.e. <path> will be stripped from the expected package name of the files). It's still necessary to enumerate the files to compile, and this should still be relative to the current working directory, not the path passed to --source-path.
For example, to compile and run from a project's root where source is stored in src/ and you want it build in bin/:
$ javac --source-path src -d bin src/mypackage/*.java
$ java -cp bin mypackage.Main
This works even from directories elsewhere in the filesystem, e.g.:
$ javac --source-path /some/absolute/path/src -d /some/absolute/path/bin /some/absolute/path/
$ java -cp /some/absolute/path/bin mypackage.Main
I am using VS Code and installed java and code runner extensions. When I created new java project using the extension, it was creating the .class file in src instead of bin. To solve the issue I opened settings.json file from File > Preferences > Settings and searched for "settings" (or "code-runner"). Then I added following lines in that file.
"code-runner.executorMap": {
"java": "cd \"$workspaceRoot\\\" && javac --source-path src -d bin src\\$fileName && java -cp bin $fileNameWithoutExt",
}
If you don`t want to see the command that runs before code file then add these lines instead:
"code-runner.clearPreviousOutput": true,
"code-runner.showExecutionMessage": false,
"code-runner.executorMap": {
"java": "there is && clear added in the execution paramater"
"java": "cd \"$workspaceRoot\\\" && javac --source-path src -d bin src\\$fileName && clear && java -cp bin $fileNameWithoutExt",
}
I hope this finds someone with similar issue.

Categories

Resources