Why Eclipse doesn't have path to java compiler? - java

As I know, java source code is compiled into class files by the java compiler (javac.exe); then these class files are put into JVM to interpret using java.exe
But Eclipse is only using javaw.exe. So, I think javaw.exe is equivalent to javac + java
But some references say javaw.exe nearly equivalent java.exe. thus Eclipse only has the interpreter progress. Compiler process takes place where, how?
How should I understand this?

Eclipse does not compile files using javac. It has its own, independent implementation of a Java compiler, complete with its own set of error messages—and bugs.
Eclipse still needs access to all the standard library classes against which it compiles Java code.
The Java runtime which runs the Eclipse IDE process is not related to the JDK used to compile the code against. For all that matters, Eclipse could be a native application written in C++ and that wouldn't stop it from being able to compile Java.

Related

Bootstrapping java and python compilers

The wikipedia page on compiler bootstrapping lists python and java among the languages whose compilers are bootstrapped. Aren't javac and cpython implemented in c?
Why are these languages listed on that page?
javac is written in Java, and compiles Java source to bytecode. The source is available online. So javac was bootstrapped. You may be confusing javac with the JVM as the latter is written in C and is used to compile bytecode to machine code and run it.
Same thing for Python. The compiler is not the same thing as the execution environment.
With Java, there is the distinction between the Java compiler (javac) and the Java Virtual Machine (java). Unless an operating system and/or CPU would have support to run Java applications natively, you will need a JVM to run any Java application.
On the other hand, tools such as Apache Maven are written in Java, and invoke the Java compiler as well. This is not done by invoking a Java package. The page on the Maven Compiler Plugin describes how this is done, and how you can configure the plugin to use the javac executable instead.
There will always be the need for some native code. But nothing is stopping you from writing all of the code that produces Java bytecode in Java itself. After all, what it does is convert text into bytes. You can do that in any programming language, including Java.

what does it means by "recompile source code?" in java

I have read following:
"Active mq requires JRE or JDk
(a JDK is required if you plan to recompile source code)"
My question is, what does it means by "recompile source code?"
It means that if you want to download the source code yourself and compile it yourself, you need the JDK. The JRE (Java Runtime Environment) can run Java code, but to compile Java code you need the JDK (Java Development Kit).
Compiling Java source code is the act of turning Java code as written by programmers into Java Byte Code by the JVM which can then be run by the JRE on any machine.
To "re-compile" means to restart this process. For us as programmers working in an IDE it means that we just save the newly changed code and press the compile icon.

Stand alone java program

New to java programs so apologies for the noob question. I have written a java program that I run using "java myprog" command in a terminal window. I want to give this program to friend but not sure what that person needs as a minimum to run the program. Can the code be compiled to run completely independently of any java installation my friend may or may not have? Will the other person need to recompile the code?
Option 1
You can give them the compiled output (the .class) file.
They need a version of the JRE (but not the JDK that developers need to compile) that is compatible with the version you used to compile your code. The simplest way is just to match versions. For example, if you're using J2SE 7, then get JRE 7 for your friend.
With this your friend should be able to type java myprog and get the same results as you. If you want your friend to run the program from the command line the same way you do, then this is probably the simplest approach.
So, if on both your machine and your friend's machine you type:
java -version
...and you get the same major version, then you're probably in the right ballpark.
Option 2
You can also create an executable .jar file if you want something your friend can just double-click, but they'll still need a version of the JRE installed, and creating executable JARs can be a bit difficult if you're new to the language. They take some learning and trial-and-error time. This is definitely a more complex approach than option #1.
Option 3
There are compilers that will convert your java program into native code (an .exe file in Windows), but you'll need to know what OS your friend is running, and make sure you're compiling for your friend's OS.
One example of these tools that I heard about years ago is Excelsior JET (though I'm not endorsing it as a tool - just saying that they exist).
This is definitely the most advanced/most difficult of options, and includes even more trial-and-error than option #2, but if you're talking about a really simple program with a single file, then it might be very straightforward.
Of course, Excelsior JET wasn't free last time I checked.
I've written an entire article on this very subject:
Convert Java to EXE - Why, When, When Not, and How
You want to create an executable .jar file. Provided that your friend has Java installed, he can run the program by simply double clicking on the file.
Java programs are usually compiled into Java bytecode - not to a native executable. Once compiled, the resulting class files will be executable on any JVM that is recent enough, regardless of the underlying hardware platform or operating system. In order to have a JVM, however, your friend would typically need to have an appropriate Java Runtime Environment (JRE) installed.
Alternatively, there are ways to distribute a program along with a JRE, but I believe that this approach is not trivial. There are programs, such as launch4j, that can automate the process somewhat, if you want to go that way.
The options given above suggesting giving the .class file and using java myprog may not work if the friend is not located in the directory where myprog.class has been placed, as the class path may need to be specified. So the friend may have to use java -cp path_to_myprog myprog

What is the difference between using javac and javax.tools.JavaCompiler?

Maven Compiler Plugin documentation states:
The Compiler Plugin is used to compile the sources of your project. Since 3.0, the default compiler is javax.tools.JavaCompiler (if you are using java 1.6) and is used to compile Java sources. If you want to force the plugin using javac, you must configure the plugin option forceJavacCompilerUse
And indeed when forceJavacCompilerUse is not specified in our build there are some build errors, for example when the code references the com.sun. packages (legacy, we know that its a bad idea...)
What are other differences between these two compile modes in general and with maven? Are there any output differences that one should know?
javac (as "java compiler") is an executable, which could be theoretically even a platform-dependent executable or a script. This is called to compile a .java to a .class.
On windows is its name javac.exe, and it is normally somewhere below C:\Program Files*\jdk*\bin.
This compiler was developed in java as well. That means, if we start this .exe, a new java virtual machine need to be started to run it. This is slow.
But, because it was written in Java, there is a much faster alternative to it: from our already running jvm, we simply import its main class (f.e. javax.tools.JavaCompiler or such) and call this. This doesn't need to start an unneeded jvm. That is what maven does. Simply 10 yrs was them enough to make this correctly. :-)
Of course it has some fallbacks as well. To most probable cause is that in the case of the internal compiler it needs to run from the same jvm and in the same namespace as the maven core. Also specifying an alternate jvm is impossible, and there could be some side effects as well resulting from the namespace collisions. But they are very improbable, because both of them is well-designed software.

Calling Java from MATLAB

I have been using Swig to create a Java wrapper for a a library written in C++. The wrappers get generated into a package and then jar'ed. The files are compiled correctly and work perfectly with java but I can't call it from MATLAB.
I tried adding the path to the jar in the static Java path file in MATLAB and then calling the classes in the jar file but I get the error "Undefined variable or class.." Or if I try using javaObject(...) "No class * can be located on Java class path".
I'm not sure what I am doing wrong.
EDIT:
To test calling a c++ library from MATLAB, I created a simple "data reader" class which contains a function that writes a randomly generated vector< vector<double> > to a text file and and a function that reads it.
The swig files generated are: SimpleReader.java, DoubleVector.java, exampleJNI.java, example.java, DoubleVector2.java in the package com.example.reader. These are compiled and packed into example.jar (the library dll generated is also packed into the jar).
It all works fine calling it from java so the problem must be specific to MATLAB. There is not much code for MATLAB as nothing seems to work. I get as far as
javaclasspath('c:/reader/reader.jar');
obj = com.example.reader.SimpleReader;
at which point I get 'Undefined variable "com" or class "com.example.reader.SimpleReader"'
In general you're supposed to be able to do this:
javaclasspath('/path/to/myjar.jar')
myobj = com.example.mypackage.MyObject;
myobj.someMethod(123);
I've been using this with MATLAB for quite a while now and have had no trouble. Perhaps you could post the exact MATLAB code you are using?
I get as far as
javaclasspath('c:/reader/reader.jar');
obj = com.example.reader.SimpleReader;
at which point I get 'Undefined variable "com" or class "com.example.reader.SimpleReader"'
Well, for starters, you mentioned your jarfile is called example.jar, but your MATLAB code references reader.jar -- are you sure the jar you're referencing in javaclasspath() exists? Have you tried looking at the contents of it? (e.g. with 7zip or any program that can read .zip-format files, since .jar files are just .zip-format files with additional specifications)
hmmm...
which version of MATLAB are you using?
are your classes public?
What do you get when you try typing the following:
javap -classpath c:/reader/example.jar com.example.reader.SimpleReader
You say you're using version 7.0.4 -- this is likely the problem. Earlier versions of MATLAB use an older version of the Java JRE:
MATLAB is only fully supported on the JVM that we ship with MATLAB. For example:
JVM 1.3.1 for MATLAB 6.5.1 (R13SP1)
JVM 1.4.2 for MATLAB 7.0.1 (R14SP1)
MATLAB 7.0.4 (R14SP2) and later versions till MATLAB 7.4 (R2007a) use JVM 1.5 and MATLAB 7.5 (R2007b) and later use JVM 1.6. There are components that may not work properly under a different version of the JVM.
You basically have three choices at this point.
(if possible) -- use only JAR files that are compatible with Java 5. In this case, since you're making your own library, you need to use the -target 1.5 option. (target="1.5" if you're using the ant <javac> task) This generally isn't a huge deal, since 1.6 is kind of an incremental improvement from 1.5 -- although if you're using some of the few Java 6 classes like ArrayDeque, or external libraries that depend on 1.6, you're out of luck.
use JRE 1.6 with Matlab 7.4 by changing the JVM. Not sure this is a good idea.
upgrade MATLAB to a version that runs on Java 6 (R2007b or later).
Remember this issue when you go to upgrade your Java development environment to Java 7 or Java 8.

Categories

Resources