在这篇文章中,我们将为您详细介绍java-helloworld的内容,并且讨论关于javahelloworld程序编写并运行的相关问题。此外,我们还会涉及一些关于1.6Java第一个程序HelloWo
在这篇文章中,我们将为您详细介绍java-helloworld的内容,并且讨论关于javahelloworld程序编写并运行的相关问题。此外,我们还会涉及一些关于1.6 Java第一个程序 HelloWorld、AG 百家家乐线上的官网网址《 787977.tv 蝙蝠 14735340 》java 输出 hello world_java 输出 Hello World、gRPC-Java HelloWorld使用笔记、HelloWorld Java的知识,以帮助您更全面地了解这个主题。
本文目录一览:- java-helloworld(javahelloworld程序编写并运行)
- 1.6 Java第一个程序 HelloWorld
- AG 百家家乐线上的官网网址《 787977.tv 蝙蝠 14735340 》java 输出 hello world_java 输出 Hello World
- gRPC-Java HelloWorld使用笔记
- HelloWorld Java
java-helloworld(javahelloworld程序编写并运行)
java核心机制:
1.java虚拟机:一次编译,随处运行,对不同的平台有不同的虚拟机
2.垃圾收集机制
开发需要jdk java software development kit
用户需要jre java runtime environment
java application初步
public class HelloWorld //{
public static void main(String[] args) {
System.out.println("HelloWorld");
}
}
java源文件以.java为扩展名,源文件的基本组成部分是类
一个源文件最多只能有一个public类,可以有多个其它类,如果源文件包含public类,它必须按照该类名来命名
java应用程序的执行入口是main方法 它有固定的书写格式 public static void main(String[] args) {}
java严格区分大小写
java方法由一条条语句构成,每条语句以分号结束
java语言有三种注释方式
// 用于单行注释
/* */ 用于多行注释
/** */
1.6 Java第一个程序 HelloWorld
程序开发步骤说明:
开发环境搭建完毕后,可以开发我们的第一个Java环境了。
Java程序开发三步骤:编写 | 编译 | 运行 。
Java程序开发的三个步骤:
编写第一个Java程序(HelloWorld):
1.在C:\ 目录下新建文本文件,完整的文件名修改为 HelloWorld.java ,其中文件名为HelloWorld ,后缀名必须为.java
2.编写程序:
public class HelloWorld{
public static void main(String[] args) {
System.out.println("Hello,World");
}
}
3.编译运行程序:
4.代码注释:
//第一行的第三个单词必须和所在的文件名称完全一样,大小写也要一样,大小写也要一样
//public class后面代表定义一个类的名称,类是Java当中所有源代码的基本组织单位:
public class HelloWorld{
//第二行是万年不变的固定写法,代表main方法
//这一行代表程序执行的起点
public static void main(String[] args) {
/*
第三行代表打印输出语句(其实就是屏幕显示)
希望显示什么东西,就在小括号当中填写什么内容
*/
System.out.println("Hello,World");
}
}
AG 百家家乐线上的官网网址《 787977.tv 蝙蝠 14735340 》java 输出 hello world_java 输出 Hello World
参考链接: Java Hello World 程序
一、输出 “Hello World!”
1、新建一个 java 项目,点击 File->New->Java Project,创建 java 项目的界面之后,输入项目名称 wly,点击 finish。
2、创建好 java 项目之后,鼠标右键项目,选择 New->Class,创建一个类,mypackage 为包名,Name 类名 Hello,首字母大写,点击 finish。
3、编写程序输出 “Hello World!”
二、测试主方法的输入参数
1、System.in 和 System.out 方法
缺点一:该方法能获取从键盘输入的字符,但只能针对一个字符的获取
缺点二:获取的只是 char 类型的。如果想获得 int,float 等类型的输入,比较麻烦。
gRPC-Java HelloWorld使用笔记
环境:Windows,JDK8,Maven3,Eclipse
在Eclipse创建Maven Project,项目名称“grpcdemo”
编写pom.xml,直接将gRPC-Java里examples里的pom文件内容复制过来,主要是以下部分:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<grpc.version>1.15.0</grpc.version><!-- CURRENT_GRPC_VERSION -->
<protobuf.version>3.5.1</protobuf.version>
<protoc.version>3.5.1-1</protoc.version>
<netty.tcnative.version>2.0.7.Final</netty.tcnative.version>
<!-- required for jdk9 -->
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty-shaded</artifactId>
<version>${grpc.version}</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-protobuf</artifactId>
<version>${grpc.version}</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-stub</artifactId>
<version>${grpc.version}</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-alts</artifactId>
<version>${grpc.version}</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-testing</artifactId>
<version>${grpc.version}</version>
<scope>test</scope>
</dependency>
<!-- Used in HelloWorldServerTls -->
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty</artifactId>
<version>${grpc.version}</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-tcnative-boringssl-static</artifactId>
<version>${netty.tcnative.version}</version>
</dependency>
<dependency>
<groupId>com.google.api.grpc</groupId>
<artifactId>proto-google-common-protos</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java-util</artifactId>
<version>${protobuf.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.9.5</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.5.0.Final</version>
</extension>
</extensions>
<plugins>
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.5.1</version>
<configuration>
<protocArtifact>com.google.protobuf:protoc:${protoc.version}:exe:${os.detected.classifier}</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}</pluginArtifact>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>1.4.1</version>
<executions>
<execution>
<id>enforce</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<requireUpperBoundDeps/>
</rules>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
新建src/main/proto文件夹,编写hello.proto文件(相关protobuf语法可自行搜索)
// Copyright 2015 The gRPC Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
syntax = "proto3";
option java_multiple_files = true;
option java_package = "io.grpc.examples.helloworld";
option java_outer_classname = "HelloWorldProto";
option objc_class_prefix = "HLW";
package helloworld;
// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
rpc SayHelloAgain (HelloRequest) returns (HelloReply) {}
}
// The request message containing the user''s name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloReply {
string message = 1;
}
在命令窗口切换到项目根目录,运行
mvn compile
运行完成后,项目结构如下
编写服务端代码
/**
* Server that manages startup/shutdown of a {@code Greeter} server.
*/
public class HelloWorldServer {
private static final Logger logger = Logger.getLogger(HelloWorldServer.class.getName());
private Server server;
private void start() throws IOException {
/* The port on which the server should run */
int port = 50051;
server = ServerBuilder.forPort(port)
.addService(new GreeterImpl())
.build()
.start();
logger.info("Server started, listening on " + port);
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
// Use stderr here since the logger may have been reset by its JVM shutdown hook.
System.err.println("*** shutting down gRPC server since JVM is shutting down");
HelloWorldServer.this.stop();
System.err.println("*** server shut down");
}
});
}
private void stop() {
if (server != null) {
server.shutdown();
}
}
/**
* Await termination on the main thread since the grpc library uses daemon threads.
*/
private void blockUntilShutdown() throws InterruptedException {
if (server != null) {
server.awaitTermination();
}
}
/**
* Main launches the server from the command line.
*/
public static void main(String[] args) throws IOException, InterruptedException {
final HelloWorldServer server = new HelloWorldServer();
server.start();
server.blockUntilShutdown();
}
static class GreeterImpl extends GreeterGrpc.GreeterImplBase {
@Override
public void sayHello(HelloRequest req, StreamObserver<HelloReply> responseObserver) {
HelloReply reply = HelloReply.newBuilder().setMessage("Hello " + req.getName()).build();
responseObserver.onNext(reply);
responseObserver.onCompleted();
}
@Override
public void sayHelloAgain(HelloRequest req, StreamObserver<HelloReply> responseObserver) {
HelloReply reply = HelloReply.newBuilder().setMessage("Hello again " + req.getName()).build();
responseObserver.onNext(reply);
responseObserver.onCompleted();
}
}
}
编写客户端代码
public class HelloWorldClient {
private static final Logger logger = Logger.getLogger(HelloWorldClient.class.getName());
private final ManagedChannel channel;
private final GreeterGrpc.GreeterBlockingStub blockingStub;
/** Construct client connecting to HelloWorld server at {@code host:port}. */
public HelloWorldClient(String host, int port) {
this(ManagedChannelBuilder.forAddress(host, port)
// Channels are secure by default (via SSL/TLS). For the example we disable TLS to avoid
// needing certificates.
.usePlaintext()
.build());
}
/** Construct client for accessing HelloWorld server using the existing channel. */
HelloWorldClient(ManagedChannel channel) {
this.channel = channel;
blockingStub = GreeterGrpc.newBlockingStub(channel);
}
public void shutdown() throws InterruptedException {
channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
}
/** Say hello to server. */
public void greet(String name) {
logger.info("Will try to greet " + name + " ...");
HelloRequest request = HelloRequest.newBuilder().setName(name).build();
HelloReply response;
try {
response = blockingStub.sayHello(request);
} catch (StatusRuntimeException e) {
logger.log(Level.WARNING, "RPC failed: {0}", e.getStatus());
return;
}
logger.info("Greeting: " + response.getMessage());
try {
response = blockingStub.sayHelloAgain(request);
} catch (StatusRuntimeException e) {
logger.log(Level.WARNING, "RPC failed: {0}", e.getStatus());
return;
}
logger.info("Greeting: " + response.getMessage());
}
/**
* Greet server. If provided, the first element of {@code args} is the name to use in the
* greeting.
*/
public static void main(String[] args) throws Exception {
HelloWorldClient client = new HelloWorldClient("localhost", 50051);
try {
/* Access a service running on the local machine on port 50051 */
String user = "world";
if (args.length > 0) {
user = args[0]; /* Use the arg as the name to greet if provided */
}
client.greet(user);
} finally {
client.shutdown();
}
}
}
HelloWorld Java
新建文本文档命名为HelloWorld.java
里面填入下列代码
public class HelloWorld{
public static void main(String[] args){
System.out.println("HelloWorld!!!");
}
}
然后在HelloWorld.java文件目录下运行CMD/PowerShell(按住Shift,点击鼠标右键)
输入以下命令进行编译。
javac HelloWorld.java
生成HelloWorld.class字节码文件,跨平台性。
然后java运行,不需要任何后缀
java HelloWorld
输出
HelloWorld!!!
注意:
HelloWorld.java文件名必须与
public class HelloWorld{
这里的类名一致。
编译带包名的java
java pakage/HelloWorld
今天关于java-helloworld和javahelloworld程序编写并运行的介绍到此结束,谢谢您的阅读,有关1.6 Java第一个程序 HelloWorld、AG 百家家乐线上的官网网址《 787977.tv 蝙蝠 14735340 》java 输出 hello world_java 输出 Hello World、gRPC-Java HelloWorld使用笔记、HelloWorld Java等更多相关知识的信息可以在本站进行查询。
本文标签: