About java compiling - java

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

Related

Compiling multiple jar and java files using javac

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/

How can I run my java class from the project root directory

I am compiling and running my Java project from the command line rather than eclipse for the first time and have a slight inconvenience that I just can't seem to find a solution too.
My project has a standard structure:
Project Directory
/src
/self
/redway
/myAPP.java
/bin
/libs
So I compiled using:
javac -sourcepath src src/self/redway/myApp.java -d bin
and so far so good...
I can run the program by navigating to the /bin and then just typing
java self.redway.myApp
BUT and this is really annoying. How do I run it from my project root directory?
I tried just
java bin/self.redway.myApp
and some other obvious ideas to no avail. I know it is a minor thing but it is super irritating and I'm sure there is a simple answer which I should have spotted immediately but I just can't find it!
Thanks.
You can add the java -cp option to specify the class path. E.g.
java -cp bin self.redway.myApp
you can write a shell script like the following:
cd [your bin directory]
java self.redway.myApp
call this script for example runProgram.sh, place it in the directory where you want to be when you run your program, and run it using
sh runProgram.sh
that would also save you time if your program is going to use other jars and you have to add them to the class path, then all you have to do is change your java command in the shellscript to become
java -cp [path to the jar]:[path to another jar]:...:[path to your bin folder] self.redway.myApp

what is CLASSPATH in make file

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.

adding JAR class path in UBUNTU

This is might be a common question but I am not able to add class path for a JAR file in UBUNTU. I have given below all the details I know:
java is located here:
the o/p of which java command is - /usr/bin/java
sudo vim /etc/bash.bashrc
export CLASSPATH=$CLASSPATH:/downloads/aws-java-sdk-1.3.24/lib/aws-java-sdk-1.3.24.jar
ps: downloads folder is directly under the root
sudo vim /etc/environment
CLASSPATH="/usr/lib/jvm/jdk1.7.0/lib: /downloads/aws-java-sdk-1.3.24/lib/aws-java-sdk-1.3.24.jar:"
As you can see, I have added the class path in bashrc and etc/environment... but still I am getting an error while trying to run the S3Sample.java which comes with awssdk for java.
when I compile the java file, I get the following errors:
ubuntu#domU-12-31-39-03-31-91:/downloads/aws-java-sdk-1.3.24/samples/AmazonS3$ javac S3Sample.java
S3Sample.java:25: error: package com.amazonaws does not exist
import com.amazonaws.AmazonClientException;
Now, I clearly understand that the JAR file is not added to the class path and so I am not getting the error. I've also tried javac with the class path option - but it does not work :(
PS: JAVA home is set correctly as other java programs work properly.
To set the classpath, it is in most cases better to use the the -cp or -classpath argument when calling javac and java. It gives you more flexibility to use different classpaths for different java applications.
With the -cp and -classpath arguments your classpath can contain multiple jars and multiple locations separated with a : (colon)
javac -cp ".:/somewhere/A.jar:/elsewhere/B.jar" MyClass.java
java -cp ".:/somewhere/A.jar:/elsewhere/B.jar" MyClass
The classpath entry in the example sets the classpath to contain the current working directory (.), and the two jar files A.jar and B.jar.
If you want to use the CLASSPATH environment variable you can do
export CLASSPATH=".:/somewhere/A.jar:/elsewhere/B.jar"
javac MyClass.java
java MyClass

Building Java package (javac to all files)

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

Categories

Resources