convert windows javac command to linux - java

I'm having troubles with converting my javac command as used under windows (my libraries are placed within the lib folder, the source code within the src folder, and the compiled code should be placed within the bin folder):
javac -cp lib/\* -d bin src/\*.java
into the correct linux javac command. I have tried many variations including
javac -cp lib/* -d bin src/*.java
but that doesn't seem to work :
"invalid flag: lib/org.eclipse.paho.client.mqttv3-1.0.2.jar".
I am looking for a solution to this already quite some time, so any advice on this would be highly appreciated!

This is one of those odd things that happens when you don't put quotes around an argument.
Solution worked out in comments section: "lib/" instead of lib/
javac -cp "lib/*" -d bin src/*.java
The other issue was that ; is needed instead of : in Linux for classpaths.

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.

Java make batch file for multiple package structure?

I am trying to make a batch file to create my client side application window, I am using multiple packages.
src/nu/connect/client/* contains all the logic and chat window itself.
src/nu/connect/message/* contains the MessageStructure class file.
javac -d bin src\nu\connect\client\*.java
javac -d bin -cp bin src\nu\connect\client\ChatWindow.java
java -cp bin nu.connect.client.ChatWindow
pause
Here is the error I am getting,when i run the batch file:
src\nu\connect\client\ChatWindow.java:7: error: package com.message does not exist.
Here is the solution to my problem, had to compile both packages in one line.
javac -d bin src\nu\connect\client\*.java src\nu\connect\message\*.java
javac -d bin -cp bin nu\connect\client\ChatWindow.java
java -cp bin nu.connect.client.ChatWindow
pause

javac: invalid flag: .getting error when compiling a java servlet

i run command
javac -classpath /home/coolhunk/JBoss/jboss-6.0.0.Final/common/lib/jboss-servlet-api_3.0_spec.jar -d helloapp.war/WEB-INF/classes -sourcepath src/com/manning/jbia/intro/*
for generating java class files.but i am getting error
javac: invalid flag: src/com/manning/jbia/intro/HelloWorldServlet.java~
Usage: javac <options> <source files>
use -help for a list of possible options
can anyone please point out what is the mistake in this command ??
Try these commands using tomcat, place your servlet source in src folder and run these,
C:\Documents and Settings\ssit>cd C:\src
C:\src>javac -classpath "C:\Program Files\Apache Software Foundation\Tomcat 6.0\
lib\servlet-api.jar" MyServlet.java
you can get the class file for the servlet. After getting the class file make the war file.
javac -classpath /home/coolhunk/JBoss/jboss-6.0.0.Final/common/lib/jboss-servlet-api_3.0_spec.jar -d helloapp.war/WEB-INF/classes -sourcepath src/com/manning/jbia/intro/*
The problem is the last item. The wildcard causes it to be expanded into everything in the directory, which causes everything after the first expansion to be treated as a source file name. The expansions also appear to include src/com/manning/jbia/intro/HelloWorldServlet.java~, which the compiler doesn't want to know about.
Try this:
javac -classpath /home/coolhunk/JBoss/jboss-6.0.0.Final/common/lib/jboss-servlet-api_3.0_spec.jar -d helloapp.war/WEB-INF/classes -sourcepath src src/com/manning/jbia/intro/*.java

Java compiler linux

I was on windows and my compiler code was
#echo off
"C:\Program Files\Java\jdk1.6.0_29\bin\javac.exe" -classpath deps/log4j-1.2.15.jar;deps/jython.jar;deps/xstream.jar;deps/mina.jar;deps/mysql.jar;deps/poi.jar;deps/slf4j.jar;deps/slf4j-nop.jar -d bin src\server\event\*.java src\server\model\items\*.java src\server\model\minigames\*.java src\server\model\npcs\*.java src\server\model\objects\*.java src\server\model\players\*.java src\server\model\players\skills\*.java src\server\model\players\packets\*.java src\server\model\shops\*.java src\server\net\*.java src\server\task\*.java src\server\util\*.java src\server\world\*.java src\server\util\log\*.java src\server\*.java src\server\world\map\*.java
pause
Now when I moved to linux, it doesn't work.
I've tried to change it to .sh file and edited like this
javac -classpath deps/log4j-1.2.15.jar:deps/jython.jar:deps/xstream.jar:deps/mina.jar:deps/mysql.jar:deps/poi.jar:deps/slf4j.jar:deps/slf4j-nop.jar -d bin src/server/event/*.java src/server/model/items/*.java src/server/model/minigames/*.java src/server/model/npcs/*.java src/server/model/objects/*.java src/server/model/players/*.java src/server/model/players/skills/*.java src/server/model/players/packets/*.java src/server/model/shops/*.java src/server/net/*.java src/server/task/*.java src/server/util/*.java src/server/world/*.java src/server/util/log/*.java src/server/*.java src/server/world/map/*.java
Basically I changed ; to :, \ to /
After running sh compile.sh in shell, I get error.
javac: invalid flag: src/server/world/map/*.java
Usage: javac <options> <source files>
use -help for a list of possible options
: not found 2: compile.sh:
Anyone know what I am doing wrong?
Doing a little research it seems this problem might be caused by incorrect line endings in your .sh file. Have you tried completely retyping it in vi, emacs, vim, or something like that?

compile files from different directories with javac, referring a depending jar file?

I have the following set up:
I have 4 packages:
root/src/terminal - has some java files
root/src/mail - has some java files
root/src/data - has some java files
root/src/main - has a single java file, Main.java
I also have the following files
root/bin - a folder to store .class files
root/mail.jar - a jar file which has important classes used in my code
Within the root, I would like to enter a terminal command which compiles root/src/main/Main.java and puts the class files in the root/bin location.
Can someone show me the command to do this? I'm on a Mac (running Leopard).
Here's the one liner:
cd /xyz/root
rm -rf bin/*
javac -d bin -classpath mail.jar -sourcepath src main/Main.java
Alternatively, you could use absolute directory names:
rm -rf /xyz/root/bin/*
javac -d /xyz/root/bin -classpath /xyz/root/mail.jar \
-sourcepath /xyz/root/src /xyz/root/ main/Main.java
In reference to Ant you said "I would rather keep it simple.".
In fact in the long term it is simpler to create a simple Ant build.xml file. The alternative is a bunch of non-portable scripts or batch file ... or lots of typing.
To run the application, assuming that you are still in the /xyz/root directory:
java -classpath bin:mail.jar main.Main
Or on Windows:
java -classpath bin;mail.jar main.Main
Or modify the above to use absolute pathnames in the classpath argument; e.g.
java -classpath /xyz/root/bin:/xyz/root/mail.jar main.Main
Without knowing your operating system?
What you should look into is using Apache Ant. It is a build tool that once installed and configured can utilize a build.xml file in your root to compile class files to a folder as well as package a jar file.
http://ant.apache.org/
try this:
javac -cp "/root/mail.jar;/root/src;" -d "/root/bin" Main.java
This is written hoping that you have package declarations in your classes from src folder like package terminal; and package main;.
See this: Options in javac command
Or use Apache Ant as suggested by maple_shaft.
From comment give by #maple_shaft:
In Unix, Linux operating systems the classpath separator is a colon instead of a semicolon.

Categories