I was importing the source code of a Java game to my local IntelliJ (community edition 2.3.2019) and try to make it run. I'm new to Java btw.
I got the code from here: https://github.com/janbodnar/Java-Space-Invaders
What I did is download the code from the repo, export the zip file in a folder and import it as a project in IntelliJ. After importing a lot of my project files appear red as you can see here:
IntelliJ red icons
My first question is: Why are these icons red? And how can I find out why IntelliJ is marking them red? I tried by hovering over these icons, but nothing happens / indicates what's wrong.
Also: Afaik in order to make this project run I need to take care of the run configuration. I tried this, but the main class is not choosable if I choose the path com.zetcode.SpaceInvaders.java (where the main class is part of the code).
What am I doing wrong? How can I run this project/code in IntelliJ?
Many thanks in advance.
You need to follow these steps:
1. IntelliJ -> New Project-> Project from existing resources(the unzipped code directory).
2. Follow the next steps as given by project explorer.
3. Remember this project uses jdk above 10 as it uses "var" keyword.
4. So, go to the project settings if the jdk in step 2 was selected below 10.
I couldn't see any red marked files. I just ran the SpaceInvaders.java file to run the project & the game started.
Related
I am a beginner in java, and hence do not know maven or boot Spring. I have tried Eclipse and IntelliJ, and wanted to try Visual Studio Code. I can make an Eclipse Project in VS Code but I cannot find any option there. I have installed the Java Extension for Visual Studio Code. Any Help would be appreciated.
I am using the latest Visual Studio Code March 2020. Thanks in advance!
Image showing where to Right Click: create a Java Package in VS Code
Inside the Java Projects tab, right click on the project.
You can then choose to create a new Java Class or Package.
I had the same situation as you, I figured creating packages on my own in VS code, Just follow these steps:
Open up your VS code.
Step 1: Click on "Explorer" , should be at top-left corner.
Step 2: Click on "New Folder"
Step 3: Enter the name of your package here.
Step 4: Drag and drop the your classes in that package.
You are all set now! Good luck!
I would recommend checking out Java Env Setup for VS Code.
and secondly:
A package is a path of subdirectories. Say your java sources are in (subdirectory of) a > directory src. All sources immediately under src have the "default" package = no package > declaration.
In src/com/java/learn (4 nested directories) the package com.java.learn; is expected for > java sources.
In your case create a path of 3 directories: com, java, and learn the latter containing > your java source.
recycled from here: How to Start a Java Project with package declaration on Visual Studio Code?
Just go to explorer and click on new folder. And then name the folder your package name. Drag and drop the classes you want in that package
Even I was in the same situation so I researched and got some results. Make sure you have JDK and JVM in your machine
You need to install Extension pack for java (from Microsoft) which is available by searching in extensions in Visual Studio
Make sure you install all the packages related to that extension such Maven for Java, Debugger for java, Project Manager for Java and Test runner for java all these are from Microsoft. After installing these you can run your java files.
I am running a build task in a Java project in Visual Studio Code.
The warning in the "PROBLEMS" tab:
[myfile].java is a non-project file, only syntax errors are reported
It refers to the first line where I load in the class file containing the main():
package [the project folder];
import [the project folder].[the file with other classes].*;
I can only avoid the warning by copying the files' text (the code text itself) into new Java files of a new project in a new unrelated folder. The code itself is correct and compiles without errors. Actually, this is the answer, but it is much manual work.
When I just copy the Java files of the project with the warning message into a new folder, the warning still appears!!!! (!)
When I just copy the whole project folder to a new place, the error remains as well, of course.
I guess that copying text into new Java files with the same names and the same folder structure is different from copying the files themselves because the files probably get tagged by Visual Studio Code, so that they have a project stamp even when the folder structure is destroyed. Perhaps this supports recovering the project structure from recovered raw files? Could this be the problem of this Visual Studio Code warning?
I checked other threads before, and this is just the last step.
How can I fix "build failed, do you want to continue"? In Visual Studio Code
Visual Studio Code - Java - Import Errors and More
--> Thus, I cleaned Visual Studio Code's workspaceStorage (on Windows: C:\Users\USER\AppData\Roaming\Code\User\workspaceStorage) and restarted without success.
Try cleaning the Java language server workspace:
Cmd + Shift + P (Ctrl + Shift + P on Windows) to show the command palette
Choose "Java: Clean the Java language server workspace"
Restart and Delete
It just worked for me.
I got the same warning simply because I had two Java (Maven) projects in the same Visual Studio Code workspace. Once I moved projectA out of the workspace, the warning for projectB was gone.
WorkspaceRoot
│ projectA
└───projectB
My current solution is to have one Java (Maven) project for one workspace, i.e, one Maven project per Visual Studio Code workspace.
My guess is that Visual Studio Code treats all Java projects inside the same workspace as as one project and hence, the projects interfering with each other.
This is an answer for those who do not use Maven.
The whole problem came up from loading not the direct project folder, but the parent folder, though the projects had been developed in their direct project folders from the start.
FOLDER1 (parent) contained
FolderA (direct project folder of Java files)
FolderB (direct project folder of Java files)
I have written the projects separately. But one time I opened the FOLDER1 in Visual Studio Code instead. That seems to have merged the two projects to just one project. After this, I changed back to opening only the FolderA/B and got the warnings that are reported in the question.
Now that I have opened FOLDER1 again and made both FolderA/B projects run without warnings (perhaps you might just comment out everything without fixing anything, but that is untested), opening the isolated FolderA/B projects threw no warnings either. It seems as if Visual Studio Code makes opening the parent folder the start of a new project which interferes with the child projects.
And the reason why I had a warning was a code error inside the other project's folder in the end (not important, but I had forgotten to load the local package needed for "FolderA" project at the start of some Java file).
Whatever error I had, the warning was confusing, as I was only working on project "FolderB" which had nothing to do with "FolderA" and which had no code issue. This led to the strange effect that I got the warning of the "FolderA" project also in my "FolderB" project, because Visual Studio Code considered both as one project.
I struggled with this for a long time, but I did not find a proper solution on the Internet. I somehow managed to do it by following these steps:
Here are the actions that I've performed:
There are folders you see in your left pane. (or press Ctrl + Shift + E to open the left pane).
Right-click on them one by one and press "Add folder to Java Source Path."
The one suggested in the solutions didn't solve my problem 100%. The problem with this extension occurred "Language Support for Java(TM) by Red Hat," which let our folder away from the source path.
Today I ran into this problem while going off-script during an Intro to Java tutorial video and somehow fixed it. This solution may not work for everyone.
Problems (1): [myfile].java is a non-project file, only syntax errors are reported.
How I fixed it...
I went to Java in Visual Studio Code and read sections "Working with Java source files" and "Working with Java projects". Basically, when working with "Java projects" in Visual Studio Code, you must have the necessary extensions installed to work with those project files.
In my case, I needed to build a Maven project supported through the extension "Language Support for Java by Red Hat" and "Server Connector by Red Hat", since one of the extensions did not come with Visual Studio Code's "Coding Pack for Java - Windows" or the "Java Development Kit: Amazon Corretto".
Once I had my extensions downloaded I opened the Command Palette (Ctrl + Shift + P) and typed "Java:Create Java Project" → "Maven" → "Maven-Archetype-Quickstart" → "1.4" → name] input group id → [name] input artifact id → then selected the folder I created that contained my Java file with the original error code (shown above). Once it was done processing, I reopened Visual Studio Code and opened my folder. Ran the program and the error went away.
You may have to copy/move your files manually or press Ctrl + Shift + E to open the left pane and add whatever you need to the Java Source Path.
I renamed the package which contains "[myfile].java is a non-project file, only syntax errors are reported" and it worked for me.
For me, removing the code folder in the C:\Users\yourHome\AppData\Roaming path solves the problem.
I have used the Visual Studio Code editor first-time for Java and I faced this issue with my simple Hello, World! program.
I tried to clean the editor's workspace, but the issue wasn't resolved.
Then, I created a folder named mypractice in the C:// drive. Following that, added the same in the Visual Studio Code workspace, and then created a Java file in that newly created workspace.
When I ran that file, it worked without any warnings or errors.
Instead of directly opening the Main.java file with Visual Studio Code Don't do this.
Open the folder in Visual Studio Code by selecting the 'Open with code' option after right clicking it in the folder and then run the program so it wont show that problem again Do this.
This is what helped me getting rid of that problem (Main.java is a non-project file, only syntax errors are reported), so I shared.
You should add your folder (in which your particular file is) to the workspace.
I have been running it a Dev Container/Docker image and multiple rebuilds have not been helping with this error. It has turned out I have accidentally created the directory in the root of the tree the current user had no access to. It has been the source of the issue.
Me and a few other students are building a game with Java at university. Someone else setup the basic structure for the project and i pulled the repo and tried to run it.
When i run it i get
this window
When i open the config, i am unable to add the main class, it is located in the file shown in the image. The OK button is disabled. You can see that here.
I'm running the latest version of IntelliJ, JDK 9 and Windows 10. The project in question is built with LibGDX. The full repo and folder structure can be seen here.
I am able to setup my own project and run my own apps, i just can't run this specific project.
Please help.
I think, since you have no "Java" directory, Intellij does not recognize any folder as Java Sources. You'll have to do that manually by rightclicking and mark the desired directory as Source root.
I figured out what the problem was, and of course - it was obvious. The project was setup with an old version of Gradle (2.4) and i was running Java 9. I installed Java 8 and made the project run.
The OK button was disabled because the project hadn't built properly (i though it had) so it couldn't find the class i needed to run it.
I recently got myself a mac and I'm really confused at why my IntelliJ does not seem to have an option to "Run" the main method that is sitting in my Application.java. I'm just trying to run a basic example.
On Windows, I normally I just right click on the Application.java file and select "Run Application.main()" and then it runs but on my mac version of IntelliJ there is no such option:
Then I try to set the config by clicking "Edit Configuration":
And then I tried to set my Main class as "Application.java" but it does not let me click "OK".
I'm using java 1.8 to compile:
It seems your project is not correctly imported,
and IntelliJ doesn't treat your source code as source code.
In your first screenshot, in the view of the project's directory,
I see that all these directories have the same brown color:
java
com
futureprocessing
...
When a project is correctly imported,
the java directory should look green,
and the sub-directories under should be shown in dotted notation,
so for example com.futureprocessing....
It looks like your project is a Gradle project.
Try to right-click on build.gradle and select reimport.
Or you might reimport from scratch.
This assumes that build.gradle is correctly written,
and that you are able to build your project on the command line.
Only in that case, IntelliJ should be able to import or re-import it correctly.
If importing/reimporting doesn't work reasonably (though it really should),
then you can try the tips in the answer and mark the source and resources directories manually to be treated as such.
Your java directory is not marked as a Sources root. Right click the java directory in project view and select Mark As/Sources Root.
Similarly the resources directory is not marked as Resources root. You can mark it as such the same way.
These problems are probably caused by incorrectly importing the project and so on. So other solution might be to try to import the project again if it doesn't work.
I reinstall my Eclipse environment every year when the new version is released. The most recent release - Juno - however, does not work as expected.
Currently, when I try running demos from Oracle, for example, it asks me to select an Ant file to run before compiling. After creating a blank Ant file to bypass the requirement (which I thought was rather dumb, since I don't think TDD is something that should be forced on people), it still refused to run, saying the "Selection could not be launched."
I understand that Juno is an update from the 3.x track to the 4.x track, but this seems a little ridiculous to me. What can or should I do so I can get Eclipse back up and running again?
Just to clarify:
All files in question are on my Eclipse workspace path
I don't do much in the way of Java development, though I know I'll need to do so soon. Hence, my unfamiliarity with the most recent version of Eclipse.
I would like to run my files & projects without Ant files. I know it has something to do with Run configs, but I don't know how to change them.
Here's what I did to run the demo from Eclipse Juno:
Downloaded the project from http://docs.oracle.com/javase/tutorial/uiswing/examples/zipfiles/components-FrameDemoProject.zip (if this is not the correct demo, then please provide the link you're using)
Unzipped it to my projects directory (not my workspace), so that you have:
PROJECTS_DIR/components-FrameDemoProject/src
PROJECTS_DIR/components-FrameDemoProject/nbproject
PROJECTS_DIR/components-FrameDemoProject/build.xml
From Eclipse, File > New > Java Project
List item Uncheck Use default location and navigate to <PROJECTS_DIR>/components-FrameDemoProject
Hit Finish
Drill down to src/components/FrameDemo.java
Right-click on FrameDemo.java and select Run As > Java Application
Let me know if any of these steps give you trouble.
Take a look at you project properties and select the Builders entry. Do you see an Ant Builder entry? Try to select or create a Java Builder entry instead.
I just created a new Java Project in Juno and added the FrameDemo.java source. In my case, the Java Builder was selected as the default for this new project.