Invalid flag error when using bash to compile java - java

I am trying to write a simple script to compile my java code.
When I put
javac Main.java
in my compile.sh and do
bash compile.sh
I get error: invalid flag: Main.java
However, if I just simply use the command javac Main.java , everything works fine.
I am using a Ubuntu VirtualBox on Win10.
How can I get my script working?

Here's the problem you're seeing:
$ cat compile.sh
javac Main.java
$ bash compile.sh
javac: invalid flag: Main.java
Usage: javac <options> <source files>
use -help for a list of possible options
This happens because the script uses DOS style line endings:
$ cat -v compile.sh
javac Main.java^M
You can fix it by setting your editor to save it with Unix line terminators, or with e.g. tr:
$ tr -d '\r' < compile.sh > fixed.sh
(no output)
$ cat -v fixed.sh
javac Main.java
It now works as expected:
$ bash fixed.sh
(no output)

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

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 !

j2objc: command not found

I have to convert java code to objective-c code.
Here I have to write the following code and run on my terminal:
$ cat Hello.java
public class Hello {
public static void main(String[] args) {
System.out.println("hello, world");
}
}
$ j2objc Hello.java
translating Hello.java
Translated 1 file: 0 errors, 0 warnings
To compile the translated file:
$ j2objcc -c Hello.m
$ j2objcc -o hello Hello.o
Here when I run $ j2objcc -o hello Hello.o on my terminal, I get the following error.
$ j2objcc -o hello Hello.o
Undefined symbols:
"_objc_autoreleasePoolPop", referenced from:
-[JavaLangThread run] in libjre_emul.a(Thread.o)
"_objc_autoreleasePoolPush", referenced from:
-[JavaLangThread run] in libjre_emul.a(Thread.o)
ld: symbol(s) not found
clang: error: linker command failed with exit code 1 (use -v to see invocation)
How can I resolve this error?
You need to add the path to where you unzipped the j2objc distribution bundle. For example:
$ cd $HOME
$ unzip ~/Downloads/j2objc-*.zip
$ export PATH=${PATH}:${HOME}/j2objc
Note: the export command above needs to be added to your ${HOME}/.bashrc file so it's set when you log out and back in.
You comment you are using XCode 3.2.6. Currently, J2ObjC seems to require XCode 4+.
Reference: https://code.google.com/p/j2objc/

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