Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 394
IndiGo Avatar asked Aug 06 '18 12:08

IndiGo


People also ask

How do I create a Java project using Visual Studio Code?

You can also create a Java project using the Java: Create Java Project command. Bring up the Command Palette (Ctrl+Shift+P) and then type java to search for this command. After selecting the command, you will be prompted for the location and name of the project. You can also choose your build tool from this command.

How do I run a Java code in Visual Studio?

Open your Java code file in Text Editor, then use shortcut Ctrl+Alt+N , or press F1 and then select/type Run Code , or right click the Text Editor and then click Run Code in context menu, the code will be compiled and run, and the output will be shown in the Output Window.

How do you enter a method declaration in Visual Studio Code?

Go to Definition# If a language supports it, you can go to the definition of a symbol by pressing F12. Tip: You can jump to the definition with Ctrl+Click or open the definition to the side with Ctrl+Alt+Click.


2 Answers

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.

like image 182
Joop Eggen Avatar answered Oct 09 '22 11:10

Joop Eggen


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.

like image 26
Deep Avatar answered Oct 09 '22 11:10

Deep