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.
Related
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?
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.
I understand that I need to import the packages in
import com.ericsson.otp.erlang.*;
To run Jinterface in Java, this is not included in Java's default libraries but in Erlang's. How do I access this library? Which path should I use? I've google it but found nothing. I am using Ubuntu 13.10. The above code is not enough for this to work.
If you have done any Java development before, then you know that you should add OtpErlang.jar to your application's class path.
You can do this in the command line, Ant, Maven, Gradle or even in your IDE.
Command line example:
javac -classpath OtpErlang.jar YourGame.java
I use OS X and OtpErlang.jar is under:
/usr/local/Cellar/erlang/R16B03-1/lib/erlang/lib/jinterface-1.5.8/priv/OtpErlang.jar
Keep in mind that you need to include OtpErlang.jar also when you run your game.
Try looking in the Jinterface users Guide, they give an example of compiling the Java code.
I am having a problem using the custom jar libraries (algs4.jar/stdlib.jar from http://algs4.cs.princeton.edu/home/) with the command prompt. I added the libraries to the IntelliJ classpath setting (project sturcture -> SDKs -> classpath) and am able to use their classes with no problems using IntelliJ.
However, I also want to be able to use these libraries in the command prompt. Even though I had no CLASSPATH variable set in my Windows settings I've been able to use javac/java in cmd with no problems when using the standard java libraries. (Probably because IntelliJ sets the CLASSPATH for all the standard libraries on every startup/compile to work systemwide). However, even after adding the 2 jars to the IntelliJ classpath setting, I wasn't able to use "javac" in cmd. I then created the CLASSPATH variable in windows settings, and added the jars to them. After this I was able to compile with javac with no problems. However, when I try to run the program in cmd, I get this:
"Error: Could not find or load main class ".
I get this error whenever I uses ANY of the jar libraries, including the standard ones, with which I didn't have problems prior to setting the Windows CLASSPATH. I guess what happens is once I set my own CLASSPATH this overrides the classpath set by intelliJ. When I removed the CLASSPATH, I was once again able to compile and run the standard libraries in cmd, but not the 2 custom libraries.
Please help!
You should make these libraries a part of the project by adding them to the module dependencies as a library instead of the JDK, then you can produce an artifact jar file that will have all the dependencies inside or near the jar and referenced via the jar manifest file classpath. See also the artifacts help section.
As this post points out, in this particular case the problem likely is due to the fact that that the classes in the library are "...placed in the default package (no package statement), and modern versions of java (at least 7 and 8) forbid to refer classes in default package, unless your own class is in default package too."
To solve the problem, I extracted the .java files from the jars (change the filetype from .jar to .zip and extract to a directory). I made up a package name, "com.sedge.stdlib", and modified the .java files as needed. For example, I modified the StdLib.java class from algs4 by placing a package directive at the top:
package com.sedge.stdlib;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Locale;
/**
* <i>Standard output</i>. This class provides methods for writing strings
* and numbers to standard output.
* <p>
* For additional documentation, see Section 1.5 of
* <i>Introduction to Programming in Java: An Interdisciplinary Approach</i> by Robert Sedgewick and Kevin Wayne.
*
* #author Robert Sedgewick
* #author Kevin Wayne
*/
public final class StdOut {
. . .
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.