如何在 Visual Studio Code 上使用包声明启动 Java 项目? [英] How to Start a Java Project with package declaration on Visual Studio Code?

查看:38
本文介绍了如何在 Visual Studio Code 上使用包声明启动 Java 项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Visual Studio Code 上学习 Java.我已经安装了Microsoft Java 扩展"在里面.我的基本 Java 程序在没有包声明的情况下运行良好.但我想打包我的程序.怎么样?

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 ?

之前我使用过IntelliJ IDEA".我曾经开始一个新项目并声明package com.java.learn".在 Visual Studio Code 中没有创建新 java 项目的选项.有一个创建工作区的选项,但我仍然遇到同样的问题.

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.

我有两个java类.索引.java"&InputHelper.java".Index.java 是主要的java 文件.InputHelper 是我在 Index.java 中使用的一个单独的类.我想制作一个项目并打包两个(或更多)文件.

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.

错误信息:

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

推荐答案

包是子目录的路径.假设您的 Java 源代码位于目录 src 的(子目录)中.src 下的所有源都具有默认"包 = 无包声明.

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.

src/com/java/learn(4 个嵌套目录)中,package com.java.learn; 预计用于 java 源.

In src/com/java/learn (4 nested directories) the package com.java.learn; is expected for java sources.

在您的情况下,创建 3 个目录的路径:com、javalearn,后者包含您的 java 源代码.

In your case create a path of 3 directories: com, java, and learn the latter containing your java source.

其余的,尽量遵循java的编码约定:类名以大写开头,如Index,变量和方法名以小写字母开头.

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.

其实虽然微软经常被低估,但我还是会选择更主流的IDE来学习java.IntelliJ IDEA(社区版)很好;NetBeans IDE 也是一个干净的不错的 IDE;eclipse 经常使用 - 虽然有点过头恕我直言.

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.

这篇关于如何在 Visual Studio Code 上使用包声明启动 Java 项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆