using cygwin to make a make file out of a single java file called memsim.java.
The makefile is called makefile.txt and it's contents is
JFLAGS = -g
JC = javac
.SUFFIXES: .java .class
.java.class:
$(JC) $(JFLAGS) $*.java
CLASSES = \
Memsim.java
default: classes
classes: $(CLASSES:.java=.class)
clean:
$(RM) *.class
My command is
make -f makefile.txt
however I am not getting a single "make" file. Instead it's making a file for each class called (file).class (file being the class name).
Using javac from jdk1.7.0_51.
EDIT: All I'm asking for is how to write a file that will make a file called "make" that will compile and run the file Memsim.java. Any information or link as to where I could find an answer would be great.
A full explanaition of what the commands in your makefile.txt mean you can find here http://www.cs.swarthmore.edu/~newhall/unixhelp/javamakefiles.html.
If you rename makefile.txt to Makefile you can compile the Memsim.java with make (without the need to specify -f makefile.txt.
If you add following to your makefile. (keep care about the single TAB instead of spaces in front of java)
exec:
java Memsim
you can execute the Memsim class with make exec.
With make clean you would remove the Memsim.class file.
Related
I have in my makefile currently this implicit rule:
.java.class:
$(JAVAC) -classpath . $<
What I need to achieve is not only to use this rule on .java files but also on .sqlj files.
I need something like this:
(.java.sqlj).class:
$(SQLJC) -classpath . $<
The reason for that is I have a circular dependency between one of my java files and one of my sqlj files that only gets resolved when compiling both the .java files and the .sqlj files in one step.
As far as I tried it the sqlj translator can also compile .java files so this should be no problem.
You can't use Old-Fashioned Suffix Rules for this.
You need to use Implicit Rules for this.
So you would write
%.class: %.java %.sqlj
$(SQLJC) -classpath . $<
Assuming the %.java file was the main input. If $(SQLJC) needs the .sqlj file instead then using
%.class: %.sqlj %.java
$(SQLJC) -classpath . $<
would do that.
If you need to pass both the .sqlj and .java files to $(SQLJC) at the same time then replace $< with $^ in either of those examples.
To compile all .java and .sqlj files together at once you want something more like this:
# Assumes files are all in the current directory.
FILES=$(wildcard *.java) $(wildcard *.sqlj)
tgt.class: $(FILES)
$(SQLJC) -classpath . $^
But assuming you can actually compile any of your .java or .sqlj files to some intermediate format (.class?) individually doing things this way will lose you the benefit of only needing to do that for each input file when it changes. This will recompile every file every time any of them changes.
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.
If I have all my .java and .class files in one place (i.e. in the default package) then everything is OK and I do all the JNI stuff, etc.
But in this case I have package-ception (lots of directories), my class and Java files are separated in /bin and /src and so on. And I need to generate the header file, but I am getting errors all the time. I tried so many commands, I saw different tutorials. I am already out of options.
So my project is in c://gvk/SEP3 and then the class and Java files with the native methods that I am gonna use are in /bin/CalculatorServer and /src/CalculatorServer
I have all the time run the javah command from the directory where the class file with the native methods is. The commands I tried so far are:
javah -d ./CalculatorServer NativeMethodsCalculator
Error: Could not find class file for 'NativeMethodsCalculator'.
javah -d ./CalculatorServer CalculatorServer.NativeMethodsCalculator
Error: Could not find class file for 'CalculatorServer.NativeMethodsCalculator'.
javah -d c://gvk/SEP3/bin/CalculatorServer -classpath c://gvk/SEP3/bin/CalculatorServer NativeMethodsCalculator
Error: Could not find class file for 'NativeMethodsCalculator'.
javah -classpath c://gvk/SEP3/bin/CalculatorServer -o NativeMethodsCalc.h src.CalculatorServer.NativeMethodsCalculator
Error: Could not find class file for 'src.CalculatorServer.NativeMethodsCalculator'.
javah -jni bin.CalculatorServer.NativeMethodsCalculator
Error: Could not find class file for 'bin.CalculatorServer.NativeMethodsCalculator'.
What you didn't try: go just to /bin/ (not into CalculatorServer) and run
javah -jni CalculatorServer.NativeMethodsCalculator
This is the only way how to run it. Just look at the javah doc. It says "fully-qualified-classname" in the synopsis. "Fully qualified" means full classpath. You were giving it only the classname. It worked for you so far only because you were using a default package, which means that your fully qualified classname was equal to a bare classname.
Option -d and -o doesn't influence the class lookup, only the storage of native result. All the variants you tried do not make any difference to your mistake.
I have all the time run the javah command from the directory where the class file with the native methods is
That's your mistake. You should run it from the directory that contains the outermost package, with the inner packages and their .class files below it. Then you don't need a -d argument or a -classpath argument. Assuming your outermost package is CalculatorServer, you should be in the directory containing CalculatorServer, and the command line required is javah CalculatorServer.NativeMethodsCalculator.
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?
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