Changing Java Version From Within Eclipse - java

I wrote a project in eclipse with an older version of java.
Now on a different computer but running the same codes I'm getting a whole lot of red.
This is due to Version incompatibility.
I've had my professor change the Java version from within the console window before. The code compiled just fine after he did so. I'm sure its the same case with this code.
After looking at other tutorials and google links I could only find command line approaches and #override methods. To be honest I still don't understand these.
What is the best way to change the version of a code originally written in an older code inside the eclipse console?
thanks!

In eclipse go to Window -> Preferences -> Java -> Compiler, there you can change the java versions. Hope it helps.

Ideally if we want to have two different versions of Java [say 1.6 and 1.7], then we should have two workspace defined accordingly to avoid any mixup. We can also change the Project Facets if our projects has facets that is.
In Eclipse Follow steps given below:
1) Windows -> Preferences -> Java -> Compiler and as per the image set the compliance compiler level as per your requirements.
2) Windows -> Preferences -> Java -> Installed JRE's and check if your required jre/jdk is available or not as given in image below:
3) Right Click on Your Project and go to Java Build Path and check if your required Library is available or not as per image given below:
4) Now you can edit the JRE System Library or add one by clicking on button's available on last image's right side, then a pop up as given below will open up. Here, you can change the execution environment [default values also can be set]

In myeclipse select the project -> properties -> java compiler -> there you can specify jdk version.

Easy
Download the jdk from Oracle's official website for the version you want to compile your project with.
JDK Oracle's official
create a new Java project
[
Once the project has been generated, If you select the part that says Configure JREs
By default eclipse will use the version you have installed on your system so if you don't change this configuration you will never be able to run the program with the build you need, in this case I will use jdk 1.8.
After pressing Add
5.1 And choose the option select the 3rd option in this case, called Standard VM and press Next
Now eclipse asks us to indicate the path where the libraries and other content is located in order to compile and run the program, we only have to indicate the directory where it is located.
As my goal is to run a program with the JRE 8 , I will look for the directory path where the download described in option 1 of this message is located.
As a quick example, since I just want my program to compile without worrying about anything else, I will add all the contents of my address.
Now we change by clicking on the JRE we want to compile our program, to be changed to the original default so that the new selection is executed ; and finally we press Apply and Close and Next
Press Finish to create the project
Now we have the whole project with all the necessary content to compile and run it.
Personally I think this is the quickest and cleanest way to do it ; the problem I encountered when I changed JDK is that when compiling I couldn't find the directory with the necessary components to run it, you had to download it and configure it together with the default parameters, which can cause a lot of headaches for less experienced users.
Finally, this is the version of eclipse that I am using
I hope you find this system useful, I use it to be able to run old examples that use applets , which are obsolete classes and jvm does not compile.

Related

How I run individual java file in IDEA?

I am a java beginner, the first java IDE I downloaded was Visual Studio Code, it was very easy to use and everything is auto configured. But it kind overheats my laptop all the time, so I want to try IDEA, so far it's a very good experience, except when I open a java file and tried to run it in IDEA, it always pops out this run configuration window and I don't understand how to configure it. In visual Studio Code I can open any java file any time and run without any issues, but now I have to go through creating projects every time. Is there any solution for this?
From how the file icon looks:
your file is not recognized as the part of the sources of your project. Check the project settings to ensure that source directories are correctly set.
I'd also recommend you to look up and follow the conventions for the directory structure of java projects.
Once you've fixed the problem with sources, you'll see "run" icon next to your class, main method, or when you're right clicking the file.
Command-line
To run a single file, there is no need for an IDE.
In Java 11 and later, the java tool at the command-line can both compile and execute a single-file Java class. See JEP 330: Launch Single-File Source-Code Programs.
If your class named HelloWorld were in a file named HelloWorld.java, on a console type:
java HelloWorld.java
To be clear: The java command-line tool really only executes Java apps, while the javac command-line tool compiles Java source code. As a convenience, the java tool was enhanced to effectively call javac on your behalf for a single-file.
JShell
If you just want to run a few lines of Java, try JShell, the REPL tool bundled with Java 9 and later.
See:
Java Shell User’s Guide by Oracle
JEP 222: jshell: The Java Shell (Read-Eval-Print Loop)
Search to learn more and find tutorials.
BlueJ
Using an IDE such as IntelliJ, NetBeans, or Eclipse can be a daunting task for the new student of Java. Those IDEs are heavy-duty tools designed for professional programmers.
I recommend using an IDE designed for beginners. BlueJ comes to mind, designed specifically for educational purposes. BlueJ makes getting started with Java easier.
If you insist on using IntelliJ, read on.
If using IntelliJ, define a project
IntelliJ is not designed to work with single files. IntelliJ expects you to work within a project.
I strongly recommend learning the basics of Maven to create and drive your new project. By defining your project in Maven, the configuration is independent of any one IDE. You can move your project between major IDEs such as IntelliJ, NetBeans, and Eclipse.
Maven is also very useful for downloading needed libraries ("dependencies") that you may want to leverage in your work. And Maven is good at packaging your Java app as a JAR (or WAR or EAR).
In IntelliJ, choose "New Project". In the New Project window, click the Maven item on left. Check the Create from archetype box. Scroll the list to find item for org.apache.maven.archetypes:maven-archetype-quickstart. Under that, choose the "RELEASE" item. Click Next button.
In Name field, enter something like MyFirstProject. Click Next button.
On the Maven settings page, just click Finish.
Wait a moment for IntelliJ to download some stuff and configure your project. Eventually you should see a BUILD SUCCESS message in the Run pane.
You will also see a pom.xml file displayed. The POM contains your settings for Maven to run your project, in XML format.
Change the <maven.compiler.source> and <maven.compiler.target> elements to the version of Java you are using. The current version is Java 17.
After editing the pom.xml, look for a little floating windoid with a tiny Maven icon. Click the icon to have Maven process your changed POM. Wait a moment.
In the Project pane, navigate to the App file. There you see code to print “Hello World!”. Let's run that code now. Click the green triangle button on the left, in the gutter, next to the main method line. A pop-up menu appears offering a Run item. Choose that item to run the app immediately.
Down in the Run pane, you should see the results, the Hello World! text.
At this point you can add your single file to the org.example package seen in the Project pane.
By the way, you can change that package name by context-clicking and choosing Refactor > Rename….
Later, learn to use the Run/debug configurations feature of IntelliJ.
Know that you need not create a new project for each time you want to do a little experiment. Create one project for such experiments. Keep adding new .java class files for each experiment. Delete old class files you no longer need.
Eventually, I suggest updating the versions of various items in your POM. The QuickStart archetype is not configured for the latest versions (for reasons I cannot fathom).
And when you learn about unit testing, replace JUnit 4 in the POM with JUnit Jupiter (Aggregator) to use JUnit 5. One of the benefits of using Maven is that you can easily switch out dependencies such as going from JUnit 4 to JUnit 5.
The IDE needs to know what's called the entry point of the program, i.e. where to start running your code. That's what the "Edit Configuration" window is wanting you to do.
If your file "Lab3.java" is in a package, make sure to fully specify that in the field you have in red. Otherwise without knowing how your project is structured (as the other answer alludes to), it's difficult to pinpoint what we're missing here.
When you create your IntelliJ project, add a directory /src right at the root of your project. Right click on that folder and tell IntelliJ that you wish to mark it as a source root. The directory should turn blue in color.
Put your packages under /src. IntelliJ will know that those are Java files.
When you want to run a class with a main method, choose Run->Edit Configurations. Tell IntelliJ that you want to add an Application. It should prompt you with the classes that have main methods in them. You'll have no trouble running them.
Use Maven or Graddle. Make sure the project is configured with the build tool enabled and integrated, it will do basic things automatically. If you are not sure, please create a new project and add your files in. Steps:
Open the IDE
New Project
Choose from the left side bar "Maven" or "Graddle"
Give it a name and the location in your machine.
Click Finish
Now you have the project ready. You need the appropriate method to run in java. A main class. In IntelliJ you can just type "main" and the auto-complete will add it for you, make sure you inside the curly brackets of the class {}. More info about the main class. You seem to have this nailed down.
Lastly make sure you have a JDK installed in the IDE. I am pretty sure this is your issue here, make sure to use one of the option IntelliJ provides. A full guide from the developers is here and should satisfy your needs. I would suggest OpenJDK for a beginner, because that served me well at the beginning, at the end of the day its your choice.

Java database path not found

I'm attempting to change the file path of eclipse java neon to access a database I need for my programming class and I cant figure out how to do it for windows 10 (the book only instructs us on on how to do it in window 8 and 7).
I'll give you the short version and just say that the book tells me to either edit the current classpath to look something like .;C:\Program Files\Java\jdk1.7.0_51\db\lib\derby.jar or to make a new one that looks like that.
The problem is when I changed it to look like that it kept giving me a path error and I just need to fixed that error. My version of Java is jre1.8.0_111 and the database I am supposed to be making is called CoffeeDB (though I don't think you need that info).
The end game of this is to be able to connect to databases so I can do my homework of creating and changing them.
If you need more information to help I am more than willing to give it, I'm just not sure as to what more you would need.
Specifying the classpath for your pc
See these IBM article and Oracle tutorial about classpath management.
Specifying the classpath for eclipse
Eclipse usually sync the class path with the system. But just to be sure -
Step 1:
Right click on your current project -> Build Path -> configure built path
Step 2:
On left side tab; select Java Built Path
Step 3:
On right side tab; select Add Library
Step 4:
Select JRE System Library.
Select Environment, Installed JRE(jdk 1.8) -> Finish -> Apply -> OK.
Related Question (problem related to installation of JDK, JRE)
Maybe you find something beneficial here
Eclipse - no Java (JRE) / (JDK) ... no virtual machine

How to setup libraries for IntelliJ 13 on Mac with JDK 1.8

I recently installed the Java 1.8 Update 20 JRE on my Mac running OSX 10.9.4. After that, I also added the JDK of 1.8.0_20. When I go into Terminal and execute "java -version" or "javac -version", each time I get "1.8.0_20" as a result. For me, this means that both JRE and JDK 1.8 are the System standard right now (I might be wrong on this).
So, I also add IntelliJ IDEA 13.1 and open it. I logged into GitHub using my credentials and it worked. So from the launcher window, I proceed Configure → Project Defaults → Project Structure. From here I firstly click the "New" button next to "No SDK" and select JDK. It automatically brings me to /Library/Java/JavaVirtualMachines/jdk1.8.0_20.jdk/Contents/Home. Just to make sure I select the JDK itself, I back off into ~/JavaVirtualMachines/ and select the jdk1.8.0_20.jdk, hitting return. This is now recognized as "1.8" in IntelliJ.
On the left hand side, I now go to Libraries and hit the "+" sign. This also automatically brings me to /Library/Java/JavaVirtualMachines/jdk1.8.0_20.jdk/Contents/Home/jre/lib. I know that it has to add all the .jar files in this folder, so I highlight using SHIFT + CLICK the entire contents of the folder and click "Ok". This is now library "ext" (don't know where that name comes from). I click Apply and Ok and go back to the Quick Start menu.
Selecting "Create New Project", I leave the template for Java and see that the Project SDK has been set as 1.8 (Java version "1.8.0_20"). Underneath it gives me the option to tick for "Groovy" (again, no idea what that is) and lastly, the library... Only that the line "Use library:" returns "[No library specified]". Even when I tick Groovy and the library field becomes clickable, it still does not find the before-created Java library. Nevermind that, I untick Groovy and go to write some simple Java code: Under the project name, in its src folder, I create a class and write:
public class main {
public static void main (String[] args){
System.out.println("Hello World!");
}
}
When I try to run the code, the button for it is greyed out. I have been searching the SO forum for a while, but it doesn't seem that anyone on JDK8 and IntelliJ 13 had this exact problem. Any ideas?
Okay, I figured this out: Removed JDK 1.8 with its JRE for purpose of a clean install down the line.
Went into Eclipse and tried to run the same code with no trouble. So I opened IDEA and removed the JDK and lib that I added manually. Then, I inserted back the 1.7 JDK and left it as such. Opened a black project and did not tick Groovy or the library option. For some reason, IDEA now reads the JDK internal Lib and includes it in the project. Created a new "main" class and wrote the same code as above. Then I checked the run options and although 1 was greyed out, the other worked.
From then on I installed JDK 1.8 again and tried the same, everything works just fine now.

Source code navigation and JavaDoc bug in Eclipse Indigo

I have a problem with Eclipse Indigo regarding library source code. When I open a library class (e.g. from the JDK, but also from some other included libraries, such as Guava), either using the Open Type dialogue or simply through Ctrl+ click on the class name or some of its methods, once in a while (not always) something breaks.
Don't get me wrong, the source is always displayed without any problem (either JDK source shipped with the JDK itself or Guava source downloaded by Maven). What actually stops to work:
When I open the Quick Outline popup in that given class using Ctrl + O, the list of methods and fields displays normally, but when I click on any of them, Eclipse doesn't move the view on it as it should. Also Ctrl + clicking any of the methods or fields of that class in my code doesn't bring me directly to it, but simply at the top of the source file.
When I hover over that said class or its methods, no JavaDoc appears in the popup as you can see in the attached image. I am in the HashSet class, the source is clearly there, but it isn't displayed in the popup.
I found out that this can be fixed by opening the Outline View and briefly clicking through the class's methods and fields. After a few clicks, Eclipse catches on and starts to work correctly, but only for this one class, not for all which are broken in this way at the moment.
Has anybody met this kind of bug? Is it tracked at Eclipse Bugzilla? Thanks in advance for any advice.
For completeness, this is my configuration:
Windows 7 Professional x64
Oracle JDK 1.7 Update 2 32b
Eclipse Indigo for Java EE Developers 32b
your issue #2 may be resolved by following these steps :
Go to http://java.sun.com/j2se/1.5.0/download.jsp and choose to
download the JDK 5.0 Source Code.
For JDK 5.0, select Download(SCSL source).
Download JDK (SCSL) 5.0 (1.5.0). This will give you a file jdk-1_5_0-src.scsl.zip. You do not need to unzip this file; Eclipse likes it the way it is.
In Eclipse, go to Projects -> Properties -> Java Build Path -> Libraries and expand JRE System Library [jre 1.5.0], then rt.jar. Select Source attachment and click Edit...
Select the above zip file. and Finish by exiting the dialog boxes.
source
Hope this helps..
Sounds like wrong sources are attached to the libs inside Eclipse.
I had similar symptoms in a project where a class exist in both: inside a lib and in the project's src folder. Same canonical class name, but different implementations.
I would recommend to verify (or reinstall) your JDK-Installation and re-configurating it in Eclipse.
Hitting Ctrl+Shift+T and typing HashSet and taking a screenshot of that window may help here, too.
To answer my own question, I didn't find out how to solve the problem in Indigo, but updating to Juno fixed it.

Eclipse Can't find default Java Classes on Mac OS X

I have done Android and C development in my copy of Eclipse, but have never actually done Java strictly. When I add code that compiles using "javac codename.java" to an eclipse project, eclipse gives me errors and can't recognize statements like import java.io.* . I believe this is due to a problem with my class path and I have tried adding some paths like library/system/java but have not had any success. Any ideas for a solution?
If Eclipse is having troubles with classes delivered by JDK, I would suggest checking out if it (thus - JDK) is properly installed. You may do that by going to menu Window > Preferences and started typing "jdk" in search input. It should point You to Java > Installed JREs. Check if there is proper JDK installed (if not - You may add it there).
After that try to clean Your project - menu Project > Clean....
Hope this helps, regards.
If you go into the preferences dialog and go to Java/Installed JREs, make sure those paths are correct.
In order for Eclipse to recognize source files as Java and compile them, your project has to be a Java project. Putting a .java file in a project that wasn't created via New / Java Project won't work.
Please try creating a Java project and put your .java file in it.
On os x the default location (right now anyway) is /System/Library/Java/JavaVirtualMachines/[VERSION].jdk/Contents/Home
The default location is available from /usr/libexec/java_home.
That said, I've never had any problems with eclipse not finding the jdk, usually it's all
automatic.

Categories

Resources