I downloaded a sample code written in java that has multiple jar files and java files. I am not a Java programmer so I am having a hard time compiling the code. Here's my attempt:
javac -classpath lib/*.jar src/*.java
However this is what I get:
javac: invalid flag: lib/dom4j-1.6.1.jar
Usage: javac <options> <source files>
use -help for a list of possible options
What's wrong with my approach and how can I compile the code? ALl the jar files are located in the lib folder, and the java files in the src folder.
You need to stop the shell from globbing the wild-card in lib/*.jar by escaping it.
Also, you need to remove the .jar suffix ... because that's how classpath wildcards work; see Oracle's "Setting the classpath" document.
So ...
javac -classpath lib/\* src/*.java
Using an IDE is another option. However, if all you want to do is compile and run, then downloading and installing and learning to use an IDE is overkill (IMO). And the flipside is that it is good for an IDE-using Java programmer to also understand how to compile and run from the shell prompt ...
old post, but thought below details help,
you can specify jar files by separating by ; in windows and : in unix
Eg: (windows)
javac -cp first.jar;second.jar;third.jar YourClass.java
(unix)
javac -cp first.jar:second.jar:third.jar YourClass.java
Source: https://gullele.com/pass-all-the-jars-in-classpath-when-compiling-java/
Related
I programmed two java classes (Coordinate.java and Orienteering.java) using Eclipse. These classes are stored in C:\Users\Jack\Desktop\Orienteering\Orienteering\src\jp\co\worksap\global
The package is jp.co.worksap.global.Orienteering. I want to know how can I compile the two files in Windows command line and run it? Orienteering.java is the main class.
First open the command line, then change to the source folder
cd C:\Users\Jack\Desktop\Orienteering\Orienteering\src\jp\co\worksap\global
javac -g Orienteering.java Coordinate.java
That should generate two corresponding .class files (with debug symbols). Then move up a few directories and execute java like,
cd C:\Users\Jack\Desktop\Orienteering\Orienteering\src\
java -cp . jp.co.worksap.global.Orienteering
note alternatively, the javac line could have been javac -O *.java (which would not include debug symbols and would optimize the build, and would compile any other java source files). Also, it is more typical to use a tool like ant, maven, gradle or sbt for java builds.
In Eclipse there is an option to compile it for you. I believe it's:
File -> Export -> Runnable Jar File -> [Select the classes] -> Select Location -> Run the JAR File that gets created in that location.
As far as I know, I think Windows CMD can only compile one Java Class.
Try this:
Goto your source directory using cd command:
cd C:\Users\Jack\Desktop\Orienteering\Orienteering\src
Compile your classes using javac
javac jp\co\worksap\global\Coordinate.java jp\co\worksap\global\Orienteering.java
To run your program:
java -cp . jp.co.worksap.global.Orienteering
I just got a makefile like this
CLASSPATH=.:/usr/share/java/antlr.jar
Mipsim.class: Mipsim.java MipsimLexer.class MipsimLexerTokenTypes.class MipsimParser.class Memory.class Processor.class
javac -classpath .:/usr/share/java/antlr.jar Mipsim.java
Memory.class: Memory.java MemoryAccessible.class
javac Memory.java
Processor.class: Processor.java
javac Processor.java
MemoryAccessible.class: MemoryAccessible.java
javac MemoryAccessible.java
MipsimLexer.java MipsimLexerTokenTypes.java MipsimParser.java: Mipsim.g
antlr Mipsim.g
MipsimLexerTokenTypes.class: MipsimLexerTokenTypes.java
javac MipsimLexerTokenTypes.java
MipsimLexer.class: MipsimLexer.java
javac MipsimLexer.java
MipsimParser.class: MipsimParser.java
javac MipsimParser.java
clean:
rm -f *.class MipsimLexer.* MipsimLexerTokenTypes.* MipsimParser.*
I have to run this make file, and build the object code.
However, the terminal told this
antlr Mipsim.g
make: antlr: No such file or directory
make: *** [MipsimLexer.java] Error 1
I guess probably anrlr.jar couldn't be found in this case. So I just changed the CLASSPATH into
CLASSPATH=.:/antlr.jar
and put antlr.jar in the same folder, then tried it again. The same error just happened again.....
Could somebody please help me out?
Thanks
CLASSPATH=.:/antlr.jar
That's not going to work. It says to use the current directory and the antlr.jar file in the root file system (which probably won't exist).
You may want to try:
CLASSPATH=.:./antlr.jar
And make sure that it exists for the duration of the antlr executable. Some make programs will execute each command in a separate shell so changing the CLASSPATH may not carry forward. I tend to use the bash "set variable for one process" method:
CLASSPATH=.:./antlr.jar antlr Mipsim.g
On top of that, the way you generally run antlr is by running java, giving the class you want to run, with something like:
java -cp ./antlr.jar org.antlr.Tool Mipsim.g
If you have an executable file (or script) antlr which will do that for you, it appears not to be in your path.
I am trying to compile a Java servlet which uses multiple external jars. Javac recognises the first jar, but then spits out errors that it can't find the following jars. When I swap the order, it still recognises the first, but none after. The command line I am using;
javac -classpath ~/servlet/servlet-api-2.3.jar:~/servlet/gson-2.2.2.jar:~/servlet/mysql-connector-java-5.1.22-bin.jar ~/servlet/dataExchange.java ~/servlet/dbUserConnect.java ~/servlet/dbTTConnect.java -d $TOMCAT_HOME
As you can see I am trying to use the servlet jar, Googles GSON (JSON), MySQL & Oracle Jars.
If you need any other information just ask.
Thanks in advance!
At least one mistake:
Usage: javac <options> <source files>
Your command line is:
Usage: javac <options> <source files> <options>
Try this:
javac -classpath ~/servlet/servlet-api-2.3.jar:~/servlet/gson-2.2.2.jar:~/servlet/mysql-connector-java-5.1.22-bin.jar -d $TOMCAT_HOME ~/servlet/dataExchange.java ~/servlet/dbUserConnect.java ~/servlet/dbTTConnect.java
The problem is the use of the ~ character throughout the path. Only the instance at the beginning of the option will be expanded.
I am trying to compile my source code from the Windows 7 command prompt. I have added the directory in which my *.java file is located to the classpath. Yet, I still get this error:
C:\Users\Alex>javac HelloThere.java
javac: file not found: HelloThere.java
Usage: javac <options> <source files>
use -help for a list of possible options
I'm very confused as to why this happens because if I navigate to the folder where this file is located, it will compile. However, this is not a satisfactory solution since I intend on compiling JUnit tests directly from the command line as well.
Other solutions I have attempted:
C:\Users\Alex>javac -classpath "C:\Users\Alex\AndroidProject\UnitTest\src" HelloThere.java
javac: file not found: HelloThere.java
Usage: javac <options> <source files>
use -help for a list of possible options
I do not think this has ANYTHING to do with typos.
Why can't I compile my project like this?
The CLASSPATH variable is not used by javac to find where your source code lives. Use the -sourcepath arg to javac instead.
Save yourself a lot of time and manual typing and use a build tool like Apache Ant or Maven.
You don't want to use classpath, that tells the compiler where to find externally referenced .class files used by your file. You want to use -sourcepath which tells javac where your .java files may be hiding.
How to compile all files in directory to *.class files?
Well, this seems pretty obvious, so I may be missing something
javac *.java
(With appropriate library references etc.)
Or perhaps:
javac -d bin *.java
to javac create the right directory structure for the output.
Were you looking for something more sophisticated? If so, could you give more details (and also which platform you're on)?
Yet another way using "find" on UNIX is described here:
http://stas-blogspot.blogspot.com/2010/01/compile-recursively-with-javac.html
The following two commands will compile all .java files contained within the directory ./src and its subdirectories:
find ./src -name *.java > sources_list.txt
javac -classpath "${CLASSPATH}" #sources_list.txt
First, find generates sources_list.txt, a file that contains the paths to the Java source files. Next, javac compiles all these sources using the syntax #sources_list.txt.
Here's a code fragment that I use to build an entire project where, as usual, source files are in a deeply nested hierarchy and there are many .jar files that must go into the classpath (requires UNIX utilities):
CLASSPATH=
for x in $(find | grep jar$); do CLASSPATH="$CLASSPATH:$x"; done
SRC=$(find | grep java$)
javac -cp "$CLASSPATH" $SRC