Linux equivalent of including the classpath during compilation - java

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

Related

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

specifying log4j in classpath

I believe this is how I can compile and run a file that uses external library. I'm using Windows.
top level directory
|
|-log4-1.2.17.jar
|-MyApp.java
|-com
|-foo
|-Bar.java
Compiling
javac -cp log4j-1.2.17.jar;. com\foo\Bar.java
javac -cp log4j-1.2.17.jar;"com\foo";. MyApp.java
Executing
java -cp log4j-1.2.17.jar;"com\foo";. MyApp
Compiling itself failed.
simple batch script, for compiling all your project
set COMPILED_CLASSES=.\
set TEMP_FILE=temp
dir .\*.java /s /B > %TEMP_FILE%
javac -classpath log4j-1.2.17.jar;%COMPILED_CLASSES% -d %COMPILED_CLASSES% #%TEMP_FILE%
rm %TEMP_FILE%
add it to top level dir and run
EDIT
step by step
javac ./com/foo/Bar.java -classpath log4j-1.2.17.jar
next
javac ./MyApp.java -classpath log4j-1.2.17.jar;./
run
java -classpath log4j-1.2.17.jar;./ MyApp
Include current directory in java classpath
java -cp log4j-1.2.17.jar;. MyApp
Why do you have to include current directory:
The default class path is the current directory. Setting the CLASSPATH variable or using the -classpath command-line option overrides that default, so if you want to include the current directory in the search path, you must include "." in the new settings.
Y need to include the local directory. If you want to do it in the current directory it would be something like:
javac -cp .;log4j-1.2.17.jar Bar

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'

powershell run java process problem

I'm trying to run a java process via Powershell in Windows XP. Here's the command:
java.exe -cp .;./common.jar -Dcontext=atest1 -Dresourcepath=. DW_Install
So, the classpath is . and .\common.jar (I think java takes the wrong slashes, right?) There are two environment variables, one "atest1" the other "." and the class to execute main on is DW_Install (in the default package).
This command works in cmd.exe, but doesn't is PS. What's going on? What is PS doing while parsing this command that CMD doesn't do (or vice versa)?
Aaron
The problem is that PS for some reason parses -Dresourcepath=. differently than cmd. What works is
java -cp '.;.\common.jar' -Dcontext=atest1 "-Dresourcepath=." DW_Install
It doesn't matter which way the slash goes, and it doesn't matter which quotes one uses (' or "). The classpath must be escaped, however, with some kind of quotes. A good test to see what's getting by the PS interpreter is to echo it. The following:
echo java -cp '.;.\common.jar' -Dcontext=atest1 -Dresourcepath=. DW_Install
yields the following output:
java
-cp
.;.\common.jar
-Dcontext=atest1
-Dresourcepath=
.
DW_Install
(Notice the resourcepath and the value of resourcepath are not on the same line.) Whereas the output to
echo java -cp '.;.\common.jar' -Dcontext=atest1 '-Dresourcepath=.' DW_Install
yields the following output:
java
-cp
.;.\common.jar
-Dcontext=etaste1
-Dresourcepath=.
DW_Install
Which is much more to our liking.
Although I wish this upon none of you, I hope that this post helps those of you that must deploy java projects on Windows machines (even though they will not run on any other platform ever).
Running external command-line programs from PowerShell is sometimes a bit problematic because there PowerShell exposes two different parsing modes that get trumped by the different syntaxes of said external programs.
In any case, running a command in Powershell requires using either the . prefix (dot-"sourcing") or the & operator.
You can workaround this by passing each parameter to the external program as separate variables, like so:
PS> $classpath = ".;./common.jar"
PS> $env = "-Dcontext=atest1 -Dresourcepath=."
PS> $class = "DW_Install"
PS> . java.exe -cp $classpath $env $class
Another example based on https://gaming.stackexchange.com/questions/24543/how-do-i-change-player-name-in-minecraft-multiplayer-in-offline-mode-in-linux
function mineCraftAs {
Param (
[parameter(mandatory=$true, HelpMessage="Minecraft character name." ,ValueFromPipeline=$true)]
[string] $name
)
if(!(test-path $env:appdata)) { $(throw "Appdata not found at $env:appdata")}
$private:minecraftPath=Join-Path $env:appdata .minecraft
if(!(test-path $minecraftPath)) { $(throw "Minecraft not found at $minecraftpath")}
$private:minebinPath=join-path $minecraftPath "bin"
if(!(test-path $minebinPath)) { $(throw "Minecraft bin not found at $minebinPath")}
$minebinPath | write-debug
gci $minebinpath | write-debug
#java -Xms512m -Xmx1024m -cp "%APPDATA%/.minecraft\bin\*" -Djava.library.path="%APPDATA%\.minecraft\bin\natives" net.minecraft.client.Minecraft '"'%1'"'
echo java -Xms512m -Xmx1024m -cp ('"'+$minebinPath+'\*"') ('-Djava.library.path="'+$minebinPath+'\natives"') net.minecraft.client.Minecraft ($name)
$minecraftJob=& 'C:\Program Files (x86)\Java\jre6\bin\java.exe' -Xms512m -Xmx1024m -cp ('"'+$minebinPath+'\*"') ('-Djava.library.path="'+$minebinPath+'\natives"') net.minecraft.client.Minecraft ($name)
}
minecraftas newbie
The following should work:
java.exe -cp '.;./common.jar' -Dcontext=atest1 -Dresourcepath=. DW_Install
I guess that PowerShell interprets the ; in the classpath as command delimiter, thereby trying to run java -cp . and ./common.jar -D....
start-process -nnw java "-cp .;./common.jar -Dcontext=atest1 -Dresourcepath=. DW_Install"

Categories

Resources