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?
Related
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.
I have problem with compiling NanoVM project from:
http://www.harbaum.org/till/nanovm/index.shtml
NanoVM is Java implementation for AVR microcontrollers. I know this is not efficient. I have problem with "makefile" file. This makefile contains instructions to compile Java .class from source .java files. But i don't know, how can i run makefile? I was using previously javac.exe compiler to write java standard applications. In install instructions writes that I must have Java SDK (J2SE). Is there any difference between SDK and JDK? I have JDK installed, i have read earlier that there isn't any difference between SDK and JDK, but i'm not sure. I know makefile files are used to manage compilation, i previously used makefile to compile programs wrote in C, but is there some Java compiler like gcc? Sorry for my stupid question, i'm quite newbie in makefiles, i think solution is very simple.
My makefile:
#
# Makefile for NanoVMTool
#
APP = NanoVMTool
VERSION = 1.5
all: ../$(APP).jar
CLASSPATH = ../../java/examples
NATIVEPATH = ../../java/native
JAVAFILES = AccessFlags.java CodeInfo.java ConstPoolEntry.java FieldInfo.java \
MethodInfo.java AttributeInfo.java CodeTranslator.java \
ConstPoolEntryError.java InnerClassInfo.java NanoVMTool.java \
ClassFileReader.java CommonInfo.java ConvertException.java \
LineNumberInfo.java NativeMapper.java ClassInfo.java \
Config.java Debug.java LocalVariableInfo.java UVMWriter.java \
ClassLoader.java ConstPool.java ExceptionInfo.java \
MethodIdTable.java Uploader.java NVMComm2.java
# compile target code
$(CLASSPATH)/%.class: $(CLASSPATH)/%.java
javac -classpath $(CLASSPATH):$(NATIVEPATH) $<
%.class: %.java
echo "public class Version {" > Version.java
echo " public static String version = \"V$(VERSION)\";" >> Version.java
echo "}" >> Version.java
javac $<
../$(APP).jar: $(APP).class
jar cmf $(APP).mf ../$(APP).jar *.class
# convert and upload a class file (should be moved to vm/target Makefile)
asuro-%: $(CLASSPATH)/%.class $(APP).class
java $(APP) ../config/Asuro.config $(CLASSPATH) $*
mega8-%: $(CLASSPATH)/%.class $(APP).class
java $(APP) ../config/Mega8.config $(CLASSPATH) $*
clean:
rm -f *.class *~
In order to run a Makefile (on Linux / UNIX), you first need to install "make" and any of the other tools that it uses. In this case, the tools are just the java, javac and jar commands. For the last two, you need a JDK installation.
But i don't know, how can i run makefile?
Change directory to the directory containing the Makefile, then run 'make' with the appropriate target. In this case make all.
Is there any difference between SDK and JDK?
There is no such thing as the Java SDK.
... but is there some Java compiler like gcc?
The Java compiler is javac. That is what your makefile is using.
Sorry for my stupid question, i'm quite newbie in makefiles, i think solution is very simple.
The REAL solution is to find and read a tutorial on Makefiles and how to read, write and use them. (In general, the solution to asking newbie questions is to educate yourself so that you aren't a newbie!)
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.
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
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.