what is CLASSPATH in make file - java

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.

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

Access another java file in a java file

I know this question has been asked and answered a number of times. But I somehow am not able to get this right. I have a package having the following structure
model/
InputDetails.java
RelationDetails.java
Now the file RelationDetails has the following structure:
package model;
public class RelationDetails {
....
}
And the file InputDetails has the following structure
package model;
public class InputDetails {
.....
}
Now I have compiled the RelationDetails.java file that creates a RelationDetails.class file in the same directory.
But when I try to compile the InputDetails.java file, It shows the error
Symbol not found
wherever RelationDetails has been used. Where am I going wrong??
I'd recommend using an IDE like Eclipse or IntelliJ IDEA. They will do the compiling for you. Or use Ant, Gradle or Maven to compile. I am a professional Java developer and I cannot remember the last time I used javac from the command line. There's no need for it.
If you insist on using javac directly, either compile both files together from the appropriate source folder (the directory above "model").:
javac "model/InputDetails.java" "model/RelationDetails.java"
Or, if you want to compile them separately:
javac -classpath . "model/InputDetails.java"
javac -classpath . "model/RelationDetails.java"
The -classpath . bit adds the current folder to the classpath for the javac executable, so it can find the previously compiled class and you won't get the 'Symbol not found' errors.
$ pwd
/tmp/model
$ ls
InputDetails.java RelationDetails.java
$ javac InputDetails.java RelationDetails.java
$ ls *.class
InputDetails.class RelationDetails.class
I am just tried in my eclipse nothing will be showing errors, better to user Eclipse or STS they will help you like this problems easily I think so..
compile with fully qualifier name.
javac model\YourClass.java

Makefile for Java cannot find package

Hello I am new in Java development. I tried to write a makefile which should be runnable in Linux:
JFLAGS = -g
JC = javac
.SUFFIXES: .java .class
.java.class:
$(JC) $(JFLAGS) $*.java
Heap.class: FibonacciHeap.java \
FileOperation.java \
MinLeftistTree.java \
RandomPermutation.java \
Heap.java
default: classes
classes: $(CLASSES:.java=.class)
clean:
$(RM) *.class
In my assumption, Heap.class should be dependent on all the other java file. Also, the main file should be in it as well.
However, I cannot get it run, it shows
Heap.java:3: package heap.FibonacciHeap does not exist
and cannot find the other reference from other java file, such as
Heap.java:61: cannot find symbol symbol : variable RandomPermutation location: class heap.Heap
list = RandomPermutation.GetList(listnum[route]);
This program runs fine in eclipse. Do you have any suggestions?
I am new and I might commit some mistake....and I don't know much about compiler and make file. If you can point it out I will be grateful!
I don't see where you set CLASSPATH. I don't care that it's a make file or Ant - javac.exe and java.exe expect the CLASSPATH to be set when they run. Where's yours?
I believe you have to set CLASSPATH in the makefile, before you run javac.exe.
I'd forget about make (and Eclipse) for a moment. Can you make this project compile and run in a command shell? If you can't, I'd say that you should not be leaning on any tools to help you.
Reading this might be helpful.
Is make really a requirement for fulfilling this assignment? How will the professor know that you used Eclipse or make or Ant or command shell to compile your .java to .class files?

compiling java from the command line

I have a package called studentServer which contains two sub packages student and common.
The common folder has references to the student package and i would like to be able to compile this. How could i do this?
javac student\*.java - compiles the student package
but when i try something similar with the common package errors are thrown - I understand it's something to do with the classpath
javac -verbose -classpath "\student" common\*.java
But I couldn't get this working. Any help would be great.
http://pastebin.com/m2a2f5d5d - here's the output from the compiler
This is a bit vague, but I suspect the classpath for the student code is wrong. Try without the leading backslash.
If you have a directory structure
source/
studentServer/
student/
common/
classes/
And you're in the directory above source, then you want to set the source path to 'source' with the -sourcepath option. You probably also want to use the -d option to tell javac where to put the compiled classes, so they aren't all mixed up with the source:
java -d classes -sourcepath source source/studentServer/student/*.java source/studentServer/common/*.java
go like this
c:\>
use change directory command cd until you get the desired directory
(ex: c:\javaEx\proj1\)
now
cd javaEx go like this
c:\javaEx\proj1\javac *.java
now compilation done in all java files in the proj1 directory.

Categories

Resources