I'm trying to use Java in current MATLAB Version (R2016b).
I just have this simple Class with no dependencies:
public class Main {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
I changed my JVM for Matlab to my local one as described here
and with version -java I get following output:
Java 1.8.0_111-b14 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode
My Java class was compiled by IntelliJ with Project SDK 1.8.0_111 and the containing folder of my .class files were added with javaaddpath to the MATLAB dynamic class paths.
I have no more ideas what could be wrong.
Using older SDKs is not possible as you can not download Java 6 or 7 anymore. (It seems you need a long term support account from oracle to do this)
Anyone can help me getting this to work?
I finally tried packaging my IntelliJ project into a jar file as described here.
Now adding the path with javaaddpath /my/path/to/project.jar to the dynamic classpath seems to work as I can use my Java classes with their full package identifier like com.example.Main in MATLAB now.
Kinda frustrating that .class files won't work with same project settings in IntelliJ.
Related
Hy,
I've ported an old 1.6 java desktop application to java 11, a lot of problems concerning jaxb no more supported on jre but at the end the application is running. Actually I've a problem with a jar named gfe.jar especially on class
de.vcs.spc.ett.sdl.SDLParser extends org.xml.sax.helpers.DefaultHandler
in the jar aren't available source files. Do somebody know if it is available a more recent version of this jar that actually is compiled with java 1.6 instead i need it for java 11.
the exception is:
"de.vcs.spc.ett.sdl.UnknownTypeException: Unknown type 'IdentifiedBlock'."
coming from:
final FieldDef fieldDef =
SDL_PARSER.createFieldDef("IdentifiedBlock");
The same code with the same libraries is working well on Linux.
On internet I can't find any information about this.
Thanks
I am having a doubt.
My understanding is that jdk has [ jre + development tools (Java, javac, debugger etc.) + source code (src.zip) ].
Now working of java compiler is nothing to do with the running of class file.
If I am compiling a .java file then from where the java compiler is importing the package?
I could find the packages under jre.
If I do not opt to install jre while installing jdk, does that mean I will not be able to compile the java file having import statement?
Please help.
First, as a minor remark, a statement like
import java.util.List;
just introduces an abbreviation, allowing you to use the simple word List later in your code instead of the full class name java.util.List. So it's not so much the import statement itself, but the usage of a class like java.util.List that needs some explanation.
You understand correctly that, to compile your java file, the compiler needs some information about every class you use, and it typically finds this information in some jar file containing that class.
Now, where is this jar file containing the java.util.List class that the compiler reads? You're correct, it comes from the JRE, from the rt.jar that's part of the system classpath (the Java compiler itself is a java program that needs the basic classes itself, so wherever you successfully run javac, you always have an rt.jar available).
If your source code used a class from some other library, you'd have to specify that library on the javac command line, using an option like -cp.
Jdk = JRE + other tools like you mentioned.
When you are compiling your java file and you are using java inbuild library then it uses rt.jar to resolve dependency i.e import statements.
You can refer below link for the difference
What is the difference between JVM, JDK, JRE & OpenJDK?
OK so I am making a Java Programme and I have the following directory setup:
- MyProgramme
|__ Engine
|______ utils
|__________ Important.java
|
|__ src
|______ main
|__________ MainApp.java
Inside MainApp.java I have an import statement:
import Engine.utils.Important;
I have also tried
import utils.Important;
To no avail.
Is there something stupid I am missing? Why cant I import Important from MainApp?
I am running Windows 10, cmd java -version returns the following:
java version "1.8.0_111"
Java(TM) SE Runtime Environment (build 1.8.0_111-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.111-b14, mixed mode)
Thanks
Quick Edit: Have also tried MyProgramme.Engine.utils.Important;
Second Edit: The error I am getting is java: package Engine does not exist
As you are only a starter, move utils folder to src.
Then use it as import utils.Smth; and you should be fine.
Also consider using some development editors. Such as IntelliJ, Eclipse or NetBeans.
Import the project and you will be fine :)
Manually playing around with the classpath is not a good idea for you.
You may want modularity in your project right now, but you don't know how to use packages well. Read up on that.
If you already know all that stuff, you should read about gradle or maven. In a nutshell these tools combine several projects into one. But in an early stage of a project this would be a major overengineering and cause a large overhead of development.
Provided your classpath is set up correctly, your first one (import Engine.utils.Important;) is fine. You need to ensure the classpath includes the parent of your Engine directory, and the parent of your main directory (assuming your Main class is in the package main, not src.main, which would be odd).
*nix example:
If the top level of your hierarchy is ~/temp, then make sure your classpath includes ~/temp and ~/temp/src. For instance, this works fine:
javac -cp ~/temp:~/temp/src src/main/Main.java
Windows example:
Or on Windows, if you assume your base directory is C:\Temp, then:
javac -cp C:\temp;C:\temp\src src\main\Main.java
Assumptions
The above assumes Important.java has
package Engine.utils;
and that Main.java has
package main;
Side note: The overwhelming convention in Java is that package names are in all lowercase. So engine rather than Engine.
I know that java intent is for gui and multi platform, but the problem I'm facing it how to release a java application into a linux servers, which I don't have control on, i.e. I dont know what java vm is installed if at all.
So, how do i compile this into a true standalone linux exe, do not assume any pre installed package on the target linux.
public class MyTest {
public static void main(String[] args) {
System.out.println("You passed in: " + args[0]);
}
}
You need to specify as a requirement an installed JRE.
Otherwise you would need to deliver a JRE yourself as part of the deliverable application
The GNU COmpiler for Java does exactly this. Keep in mind that it will work properly only for small programs, either way you'll need a JVM.
There's also Avian, which has another approach and allows to deploy a lightweight JVM with jour application, but it still hasn't all the features of a full JRE.
Creating one binary from java may not be that good. You may consider tools like http://launch4j.sourceforge.net/ for creating a full installation along with appropriate jre.
Let me start by saying I've been doing professional development work in C++ for about 15 years, and I've done a very small amount of Java on Linux systems. However, I seem to be doing something incorrectly with my Java installation on eclipse.
I have eclipse installed on a Windows 7 machine in C:\tools\eclipse\helios.
I have the Java jdk installed in C:\tools\Java\jdk1.6.0.
The Java jre is installed in C:\tools\Java\jre1.6.0.
In eclipse, I create a java project, and point the project at the jdk folder listed above. In other words, I have the jdk listed as one of the installed JREs and it is the selected JRE.
BTW: I also have Google's android tools and the MotoDev envioronment installed in eclipse, and I can write and build android java code in those projects. Of course, they don't use the jdk.
So, I'm relatively new to Java and I want to start simple, so I created the basic HelloWorld project. I have the following simple code:
package app;
import system; // System not recognized here!!!
public class Application
{
public static void main(String[] args)
{
system.out.println("Hello World!"); // System not recognized here, obviously!
}
}
Anyone have any idea what I'm doing wrong here? There doesn't seem to be anywhere to point to the jdk/lib directory. The jdk looks in jdk1.6.0/jre/lib, which I'm pretty sure isn't going to help me.
Thanks.
System class is present in java.lang package. It's imported by default. So no need to import it like you did. Please bear in mind that it's Java's convention to have class names begin with uppercase characters. Hence System class is with upper case S
Documentation for packages and classes in JDK is available at http://download.oracle.com/javase/6/docs/api/. It's a good idea to refer to this often especially when beginning with Java programming
Maybe you need a capital 'S' for System? And you shouldn't need to import 'System'. It is imported automatically.
It is System and not system. And No need to import it explicitly.