IndiGo
IndiGo

Reputation: 101

How to Start a Java Project with package declaration on Visual Studio Code?

I am learning Java on Visual Studio Code. I have installed the "Microsoft extension for Java" in it. My basic Java programs runs fine without package declaration. But I would like to package my program. How ?

Earlier I used "IntelliJ IDEA". I used to start a New Project and declare "package com.java.learn". In Visual Studio Code there is no option to create New java Project. There is an option to create Workspace but I still have the same issue.

I have two java class. "Index.java" & "InputHelper.java". Index.java is the main java file. InputHelper is a seperate class which I use in Index.java. I want to make a project and package both ( or more ) files.

Error Message:

The declared package "com.java.learn" does not match the expected package

enter image description here

Upvotes: 6

Views: 19199

Answers (4)

nik195
nik195

Reputation: 1

first step : you have to select last sub-directory of src\com\java\learn.

second step: write click on learn and create class.

third step: now whenever you class is open it will automatically fetch your package like com.java.learn.

note: if you have installed run coder extension then please uninstall it and try to run program with normal run java option in VS Code.

Upvotes: 0

Winand
Winand

Reputation: 2433

You can use Java Projects panel to create a new project, package, class.

enter image description here

Also I think there's an issue in VSC 1.63.2, because a new item is created but it's not displayed in project structure until I reload VSC window.

Another option is to put right package declaration on the first line of a class file and use inline 💡light bulb button to move that class to the package it belongs.

P. S. I'm learning Java now so I could be missing something

enter image description here

Upvotes: 0

Deep
Deep

Reputation: 959

I faced a similar issue, coming from Eclipse/IDEA background you find it difficult to not have a feature in your java IDE to create a new package.

Although, Joop Eggen's answer is correct that package is a path of subdirectories but you might find it tedious to create subdirectories when the number of sub packages is greater and name of sub packages is long.

You can use the below VSCode extension : https://github.com/jiangdequan/vscode-java-saber

It is a very handy extension.It provides support for:

  • New: Java files(annotation/class/interface/enum/package/JSP/HTML)
  • Generate Getters and Getters
  • Copy Qualified Name
  • Sort Project By Name
  • Run Maven Goals
  • Generate Docs

You can try this extension.

Upvotes: 2

Joop Eggen
Joop Eggen

Reputation: 109567

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.


For the rest, try to follow the coding conventions of java: class names starting with a capital like Index, variable and method names with a small letter.

In fact though Microsoft is often underestimated, I would chose a more mainstream IDE for learning java. IntelliJ IDEA (Community edition) is fine; NetBeans IDE is a clean an nice IDE too; eclipse is used very often - though a bit overdone IMHO.

Upvotes: 9

Related Questions