How to package .java files in VS code? [duplicate] - java

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.

Related

Visual studio code cannot find java libraries

I've setup a Java project in Visual Studio Code. I installed previously a few java extensions :
and I can create a program that does Hello World.
In a folder called lib inside the project folder, I copied some jars (lwjgl3) and, although are shown by intellisense when I try to import them, I have an error that says it cannot find the package :
Anything I missed ?
You need to add the jar to your classpath. The classpath is something used by java to find the libraries your program uses. Java looks in each folder/jar archive to find the class files, because it would be slow if it searched every directory on your computer. See this question: What is a classpath and how do I set it?
Add a reference to the package in the following way.
Click the plus sign to the right of Referenced Libraries under the JAVA PROJECTS panel
Select the .jar file in the pop-up window.

Run configuration IntelliJ for 2D game

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.

Package presentation in Java EE perspective in eclipse

I have a problem when switching to my Java EE perspective in eclipse indigo.
In my Java perspective I have this layout:
But when I switch to Java EE perspective I get this:
I have tried this and this unsuccessfully. I want to have the first layout in the Java EE perspective. Is there a way to do it?
Package presentation can be found here :
These are my settings in eclipse 4.2 which result in the layout you want:
- folder presentation: grouped
- package presentation: flat
You can switch this using the "Package Presentation -> Flat" from the drop down menu in the project explorer.
You can also import your project by using "import existing maven projects" . It will then configure the folders to be flat.
The above answers do not really solve the real problem that most noobs experience in Eclipse. So I thought I would add an answer to this question since the questioner resolved to reinstalling eclipse which is not necessary nor a viable solution. The real issue is that when you created or rather imported this project, you picked the option to create a Project in eclipse, not a Java Project. If you pick Project, it creates it as a filesystem looking directory structure similar to what you see in windows explorer. To fix this, just re-import your project from the workspace as a "Java Project" and you can change the package structure as flat or hierarchical as per your viewing pleasure.. Hope this helps.

How can I install httpclient and other libraries in Java so I can use them in Eclipse?

I'm a new Java developer I've only ever done PHP in the past and all the functions I needed to use were built into PHP. This doesn't seem to be the case with Java though!
I downloaded Eclipse and wrote a hello world and some other things and everything is working great for me! I downloaded httpclient (from http://hc.apache.org/downloads.cgi) and I can see two files, httpclient-4.0.2.jar and httpmime-4.0.2.jar but I'm not sure how I can add them into my program and start using them to build something with!
Any help would be appreciated so much and I'm sorry if this comes off as a trolling question of sorts, I'm really new to this!
Hi in any environment weather it be eclipse, jdeveloper, intellj, netbeans or commandline you need to add the downloaded jar files to the classpath/libraries. This is so the JVM know where to look for external libraries.
In Eclipse in the project explorer right click on the root project and go to properties. Then in the left hand list go to Java build path, once selected go to libraries then go to add external libraries. Once selected they should appear in the list next to the button.
hope this helps.
In Eclipse, right click on your project in the Package Explorer and choose Build Path > Add External Archives... Navigate to your jar files, select them, and click Open.

In java, how do you import one project into another without having them in the same (Eclipse) workspace?

I've been a c++ programmer for 10 years, i'm used to just creating libraries and then linking to them from my existing project. However in java, i have 2 projects, one is my game engine, the other is the test environment that i would like to use, here is how it is structured:
com.logic.engine
com.logic.testapp
yet in my test app, i cannot do
import com.logic.engine.*;
it simply cannot find the reference.
How can I do this without having to copy and paste my engine into every program i write with it?
Package your engine as a jar, and add the jar to the classpath of the projects that use it.
In eclipse you can also declare that your test prgram depends on your game engine. The classes will then be available. Check in the build paths dialog available from the context menu in the project view.
In eclipse, right click on your test project in the Project Explorer and select Properties. In the "Java Build Path" section, go to the Projects tab and add your engine project.
You need to change the classpath to include the folder containing your engine.
java -cp /path/to/engine testapp
I just fired up Eclipse to check this out.
You should see a red wavy underline on import com.logic.engine.*; - mouse over this and it'll tell you that the import could not be resolved and offer you a number of so-called quick fixes. One of those should be Fix Project Setup - that will offer to fix things for you, in this case to add one project to the other's build path.

Categories