javac: invalid flag: activation-1.1.jar - java

I'm using Tomcat7 , jdk 1.7.0_55 & eclipse, when I trying to compile the entire project(Java Code) using COMMAND PROMPT, its showing Error Like
javac: invalid flag: D:\COMPILE\lib\activation-1.1.jar.
The given below steps are followed to compile the code.
Step.1: dir *.java /s /b > FilesList.txt
Step.2: javac #FilesList.txt -d compiledCode -cp D:\COMPILE\lib\*.jar
After run the Step.2 command its showing Error.so I removed the error jar file from my lib folder & run the command but its showing same error with another jar.
Note: I Already have ANT build.xml but I want to compile the project through COMMAND PROMPT.

The lib*.jar gets expanded by the command shell. You need to avoid that by using quotes.
***** -cp "D:\COMPILE\lib\*" *****
The argument to -cp is a single path list (like $PATH, not multiple arguments with one path each). Multiple files can be separated by : (or ; on Windows)

Related

Why I always received invalid flag error while building .jar

Why I always received invalid flag error while I'm building .jar
When I using Build Jar to make a .jar file in java:
rm -rf build-jar && mkdir build-jar && javac -d build-jar /Users/user/Desktop/projects/Swings/source/library/* && jar cvf build-jar/window.jar build-jar *
I always receive a error:
error: invalid flag: /Users/user/Desktop/projects/Swings/source/library/controller
Usage: javac <options> <source files>
If I use path to .class file:
rm -rf build-jar && mkdir build-jar && javac -d build-jar /Users/user/Library/Application Support/Code/User/workspaceStorage/ca7d24d42ba31de4cfb244fc0f239d07/redhat.java/jdt_ws/swings_12c4bbf0/bin/* && jar cvf build-jar/window.jar build-jar *
I also receive an error:
zsh: no matches found: Support/Code/User/workspaceStorage/ca7d24d42ba31de4cfb244fc0f239d07/redhat.java/jdt_ws/swings_12c4bbf0/bin/*
I'm not very familiar with building jar, It make me headache, And I don't know what to do, the other question's solution or solution on internet are all not work for me(like Classpath invalid flag - Java, etc.)
I'm using IDE vscode, and using vscode extension "JAR Builder"
'star' expansion is a thing your shell does. When you type ls *.txt in your shell, that's not what is run. Your shell itself detects that * and will go out and figure out what you really mean. What actually ends up being executed is ls a.txt b.txt c.txt - everything that star matches, separated out by spaces.
The same is happening here. Hence, why you get this error: Your shell is executing:
javac -d build-jar
/Users/user/Desktop/projects/Swings/source/library/controller
/Users/user/Desktop/projects/Swings/source/library/model
/Users/user/Desktop/projects/Swings/source/library/... and all the other dirs...
and here's the clue: javac does not work like this. You cannot specify directories and expect it to know what to do. You need to list each java file individually, which means you need one heck of a long command line.
There is a reason nobody in the java ecosystem builds apps with the command line. Everybody uses maven or gradle instead. So should you. It'll solve this problem; you just stick your sources in the right location and maven / gradle figure it out from there. Have as many packages as you want.

Javac command works when typed manually but not when run in Makefile

I wish to compile my java project using a makefile as I have done previously however when I run make, it says that the command javac was not found, however if I were to type out the exact same command I am running from my Makefile in the terminal (at the same directory level) the command runs successfully.
The makefile
PATH = src/main/java/cs455/overlay
FILES = $(PATH)/node/*.java $(PATH)/util/*.java
all:
javac $(FILES)
run:
Java ProducerConsumer
clean:
rm *.class
The corresponding command that is formed from make is
javac src/main/java/cs455/overlay/node/*.java src/main/java/cs455/overlay/util/*.java
Again just typing make, it says javac command not found, but pasting this above in the terminal runs no problem.
One, you should probably use another tool for building Java projects. maven or sbt are far more common. Two, do not modify the PATH.
SRCPATH = src/main/java/cs455/overlay
FILES = $(SRCPATH)/node/*.java $(SRCPATH)/util/*.java
all:
javac $(FILES)
run:
Java ProducerConsumer
clean:
rm *.class

jar xf file.jar not working in windows 10

Not working in Windows 10:
> jar xf file.jar
'jar' is not recognized as an internal or external command, operable program or batch file.
In a PowerShell, you should first:
$Env:Path+="C:\Program Files (x86)\Java\jre1.8.0_181\bin"
Change jre1.8.0_181 according to your installed Java Runtime Environment. For sanity check, you can print the updated path:
$Env:Path
Then:
java -jar -h
I still haven't figured how to get the xf subcommand to work though.
Set the JAVA_HOME in Environment variable.
Then set path.
then open a new Command prompt and type
$ javac -version
It should print something like that
javac 1.8.0_152
Now try

Why isn't command line not finding finding my .java files?

I am using Windows CMD, and for some reason, I am getting this error message relating to my .java files. For example, I am typing:
javac FirstProgram.java
However, this error message occurs:
javac: file not found: FirstProgram.java
Usage: javac <options> <source files>
use -help for a list of possible options
I typed in javac -version, and I am currently using javac 1.8.0_144. Someone on in another Stack Overflow question suggested to change the System Variables. I used JAVA_HOME as the variable name, and I copied the path to my JDK folder, but thus far, I haven't had much luck. I still receive the same error message.
There are several problems here ... including why
PROBLEM 1:
"...I am not even allowed to save my FirstProgram.java in the Java
folder in Documents." <= ?!?
PROBLEM 2:
"...I got the error message about a false flag" <= This is probably a space " " in the path name
STRONG SUGGESTION:
Download Eclipse and try compiling and running your program from Eclipse. In other words, using an IDE, instead of the command line.
You can download Eclipse here:
http://eclipse.org
There is a good "starters tutorial" here:
Creating your first Java project
Run the dir or dir/p command to see your directory contents on your command prompt
c:\path\to\your program directory\dir
See if FirstProgram.java is listed or not? If not then you are in a wrong directory.
Now you have two options
You change to the correct directory using cd command or
You use the absolute path for your FirstProgram.java file
Navigate to that specific folder containing that program then [ Shift + L_Click ], click open cmd, then run it again to ensure it is being ran in that folder.
You should try to use absolute path while using the command if it says the file isn't found.
javac /some/directory/path/to/the/file/FirstProgram.java
Note: On command line, most of the shells would anyway not let you complete the path if the file doesn't exist there. And the other way if you're copying the path from an explorer/finder, it shall be guaranteed to be existing.
Edit: The absolute path as pointed out in comments would be using forward slashes in Windows, e.g :
javac \some\directory\path\to\the\file\FirstProgram.java
This is my a simplified DOSJavaIDE environment for simple test applications if Eclipse is too much of a hassle. I can point compiling and running an application to a specific JVM version. Study this script to see how folder structure and paths are given in each of the commands.
Folders and files
c:\projects\test1\classes\
c:\projects\test1\lib\
c:\projects\test1\lib\somelib1.jar
c:\projects\test1\lib\somelib2.jar
c:\projects\test1\src\
c:\projects\test1\src\test\GameLoop2.java
c:\projects\test1\src\META-INF\MANIFEST.MF
c:\projects\test1\javaenv.bat
javaenv.bat
#REM Standalone JavaDosEnvironment
#set JAVA_HOME=C:\Program Files\Java\jdk1.8.0_112
#"%JAVA_HOME%\bin\java" -version
#set cmd=%1
#if "%cmd%"=="" (
#echo Please specify command to run ^(1..n or empty to exit^)
#echo 1=Compile, 2=Jar, 12=CompileJar
#echo 3=Run-test1 GameLoop2 with vsync
#SET /p cmd="1..n: "
)
#IF /I "%cmd%"=="1" set cmd=compile
#IF /I "%cmd%"=="compile" call :COMPILE
#IF /I "%cmd%"=="2" set cmd=jar
#IF /I "%cmd%"=="jar" call :JAR
#IF /I "%cmd%"=="12" set cmd=compilejar
#IF /I "%cmd%"=="compilejar" (
call :COMPILE
call :JAR
)
#IF /I "%cmd%"=="3" set cmd=run-test1
#IF /I "%cmd%"=="run-test1" call :RUN-test1
#goto :END
:COMPILE
xcopy /Y .\src\META-INF\*.* .\classes\META-INF\
set cp=./lib/somelib1.jar;./lib/somelib2.jar
"%JAVA_HOME%\bin\javac" -classpath "%cp%" -sourcepath ./src -d ./classes ./src/test/*.java
#goto :eof
:JAR
xcopy /Y .\src\META-INF\*.* .\classes\META-INF\
SET MF=./classes/META-INF/MANIFEST.MF
"%JAVA_HOME%\bin\jar" cvfm ./lib/test.jar %MF% -C ./classes .
#goto :eof
:RUN-test1
"%JAVA_HOME%\bin\java" -cp "./lib/*" test.GameLoop2 "fullscreen=false" fps=60 vsync=true
#goto :eof
:END
#pause
MANIFEST.MF
Implementation-Title: testapp
Implementation-Version: 1.0.0 (2017-07-21)
Implementation-Vendor: myname
Implementation-URL: http://my.homepage.com/
Run this script in a command-line such as javaenv.bat compile, javaenv.bat jar, javaenv.bat run-test1 or run without arguments to prompt for selection list.
See a customized manifest where you may write anything you want and is included in a ./lib/test.jar file. Compile target has few 3rd party dependency libraries in a classpath.

Java runs OK from command line, but gives "Could not find or load main class" from shell script

The command below runs fine when I execute it on RedHat 6 from command line:
java -cp /my/dir/Dep1.jar:/my/dir/Dep2.jar:/my/dir/MainJar.jar one.of.my.classes.Class1 <args> > log.txt
However, when I execute the following script test1.sh (which has 755 permissions):
#!/bin/sh
export JEXE="java"
export JBDIR="/my/dir"
export ARGS="<args>"
export JARS="${JBDIR}/Dep1.jar:${JBDIR}/Dep2.jar:${JBDIR}/MainJar.jar"
export CLASSPATH=""
for f in `ls ${JBDIR}/*.jar`
do
export CLASSPATH=$CLASSPATH:$f
done
cd "${JBDIR}"
"${JEXE}" -cp "${JARS}" one.of.my.classes.Class1 "${ARGS}" > log.txt
as ./test1.sh from command line, I get Could not find or load main class. What am I doing wrong?
UPDATE:
Sorry for bad editing of the original question. Corrected.
echo produces java -cp /my/dir/Dep1.jar:/my/dir/Dep2.jar:/my/dir/MainJar.jar one.of.my.classes.Class1 <args>
I added building CLASSPATH trying to get the script to work. Removing it has no effect,i.e. I get the same exact error.
Is it possible that the JAR is build somehow incorrectly? I generate it by running an ant build from Eclipse.
The script worked after I added
export PATH=$PATH:<path to java directory>

Categories