Skip to main content

hello world java

· 8 min read

hello

java hello world

public class HelloWorld {

public static void main(String[] args) {
// Prints "Hello, World" to the terminal window.
System.out.println("Hello, World");
}

}


编译

编译 需要添加g 选项

javac -g HelloWorld.java 

调试

方法一:

使用jdb 调试hello wrold

jdb -classpath . HelloWorld
> stop  in HelloWorld.main                                
Deferring breakpoint HelloWorld.main.
It will be set after the class is loaded.
> run
run HelloWorld
Set uncaught java.lang.Throwable
Set deferred uncaught java.lang.Throwable
>
VM Started: Set deferred breakpoint HelloWorld.main

Breakpoint hit: "thread=main", HelloWorld.main(), line=5 bci=0
5 System.out.println("Hello, World");

main[1]

使用maven 编写helloworld

当遇到maven package后,java -java some.jar 说找不到main的时候可以参考以下答案
https://stackoverflow.com/a/9689877/6229548

加载类

(gdb) bt
#0 open64 () at ../sysdeps/unix/syscall-template.S:84
#1 0x00007ffff695b544 in os::open (path=0x7ffff7fcefd0 "/home/dinosaur/jdk8/build/linux-x86_64-normal-server-slowdebug/jdk/classes/java/lang/Class.class", oflag=0, mode=0)
at /home/dinosaur/jdk8/hotspot/src/os/linux/vm/os_linux.cpp:5188
#2 0x00007ffff63ffdfc in ClassPathDirEntry::open_stream (this=0x7ffff006f178, name=0x7ffff000cce8 "java/lang/Class.class", __the_thread__=0x7ffff000c000)
at /home/dinosaur/jdk8/hotspot/src/share/vm/classfile/classLoader.cpp:210
#3 0x00007ffff640055b in LazyClassPathEntry::open_stream (this=0x7ffff001ad48, name=0x7ffff000cce8 "java/lang/Class.class", __the_thread__=0x7ffff000c000)
at /home/dinosaur/jdk8/hotspot/src/share/vm/classfile/classLoader.cpp:330
#4 0x00007ffff640209b in ClassLoader::load_classfile (h_name=0x7ffff4062108, __the_thread__=0x7ffff000c000) at /home/dinosaur/jdk8/hotspot/src/share/vm/classfile/classLoader.cpp:909
#5 0x00007ffff6a8570a in SystemDictionary::load_instance_class (class_name=0x7ffff4062108, class_loader=..., __the_thread__=0x7ffff000c000)
at /home/dinosaur/jdk8/hotspot/src/share/vm/classfile/systemDictionary.cpp:1304
#6 0x00007ffff6a838b8 in SystemDictionary::resolve_instance_class_or_null (name=0x7ffff4062108, class_loader=..., protection_domain=..., __the_thread__=0x7ffff000c000)
at /home/dinosaur/jdk8/hotspot/src/share/vm/classfile/systemDictionary.cpp:779
#7 0x00007ffff6a81ff7 in SystemDictionary::resolve_or_null (class_name=0x7ffff4062108, class_loader=..., protection_domain=..., __the_thread__=0x7ffff000c000)
at /home/dinosaur/jdk8/hotspot/src/share/vm/classfile/systemDictionary.cpp:232
#8 0x00007ffff6a819f2 in SystemDictionary::resolve_or_fail (class_name=0x7ffff4062108, class_loader=..., protection_domain=..., throw_error=true, __the_thread__=0x7ffff000c000)
at /home/dinosaur/jdk8/hotspot/src/share/vm/classfile/systemDictionary.cpp:171
#9 0x00007ffff6a81d64 in SystemDictionary::resolve_or_fail (class_name=0x7ffff4062108, throw_error=true, __the_thread__=0x7ffff000c000)
at /home/dinosaur/jdk8/hotspot/src/share/vm/classfile/systemDictionary.cpp:212
#10 0x00007ffff6a87277 in SystemDictionary::initialize_wk_klass (id=SystemDictionary::Class_klass_knum, init_opt=0, __the_thread__=0x7ffff000c000)
at /home/dinosaur/jdk8/hotspot/src/share/vm/classfile/systemDictionary.cpp:1866
#11 0x00007ffff6a873a7 in SystemDictionary::initialize_wk_klasses_until (limit_id=SystemDictionary::Cloneable_klass_knum, start_id=@0x7ffff7fd0a84: SystemDictionary::Object_klass_knum,
__the_thread__=0x7ffff000c000) at /home/dinosaur/jdk8/hotspot/src/share/vm/classfile/systemDictionary.cpp:1882
#12 0x00007ffff6a8b13c in SystemDictionary::initialize_wk_klasses_through (end_id=SystemDictionary::Class_klass_knum, start_id=@0x7ffff7fd0a84: SystemDictionary::Object_klass_knum,
__the_thread__=0x7ffff000c000) at /home/dinosaur/jdk8/hotspot/src/share/vm/classfile/systemDictionary.hpp:408
#13 0x00007ffff6a874e0 in SystemDictionary::initialize_preloaded_classes (__the_thread__=0x7ffff000c000) at /home/dinosaur/jdk8/hotspot/src/share/vm/classfile/systemDictionary.cpp:1901
#14 0x00007ffff6a87199 in SystemDictionary::initialize (__the_thread__=0x7ffff000c000) at /home/dinosaur/jdk8/hotspot/src/share/vm/classfile/systemDictionary.cpp:1843
#15 0x00007ffff6ad68c9 in Universe::genesis (__the_thread__=0x7ffff000c000) at /home/dinosaur/jdk8/hotspot/src/share/vm/memory/universe.cpp:288
#16 0x00007ffff6ad8db6 in universe2_init () at /home/dinosaur/jdk8/hotspot/src/share/vm/memory/universe.cpp:991
#17 0x00007ffff66463b3 in init_globals () at /home/dinosaur/jdk8/hotspot/src/share/vm/runtime/init.cpp:114
#18 0x00007ffff6ab93ef in Threads::create_vm (args=0x7ffff7fd0e80, canTryAgain=0x7ffff7fd0e03) at /home/dinosaur/jdk8/hotspot/src/share/vm/runtime/thread.cpp:3424
#19 0x00007ffff6702ed0 in JNI_CreateJavaVM (vm=0x7ffff7fd0ed8, penv=0x7ffff7fd0ee0, args=0x7ffff7fd0e80) at /home/dinosaur/jdk8/hotspot/src/share/vm/prims/jni.cpp:5166
#20 0x00007ffff7bc3bda in InitializeJVM (pvm=0x7ffff7fd0ed8, penv=0x7ffff7fd0ee0, ifn=0x7ffff7fd0f30) at /home/dinosaur/jdk8/jdk/src/share/bin/java.c:1145
#21 0x00007ffff7bc1a36 in JavaMain (_args=0x7fffffffa910) at /home/dinosaur/jdk8/jdk/src/share/bin/java.c:371
#22 0x00007ffff73d66ba in start_thread (arg=0x7ffff7fd1700) at pthread_create.c:333
#23 0x00007ffff78f741d in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:109

加载classloader

(gdb) bt
#0 open64 () at ../sysdeps/unix/syscall-template.S:84
#1 0x00007ffff695b544 in os::open (path=0x7ffff7fcefd0 "/home/dinosaur/jdk8/build/linux-x86_64-normal-server-slowdebug/jdk/classes/java/lang/ClassLoader.class", oflag=0, mode=0)
at /home/dinosaur/jdk8/hotspot/src/os/linux/vm/os_linux.cpp:5188
#2 0x00007ffff63ffdfc in ClassPathDirEntry::open_stream (this=0x7ffff006f178, name=0x7ffff000cd08 "java/lang/ClassLoader.class", __the_thread__=0x7ffff000c000)
at /home/dinosaur/jdk8/hotspot/src/share/vm/classfile/classLoader.cpp:210
#3 0x00007ffff640055b in LazyClassPathEntry::open_stream (this=0x7ffff001ad48, name=0x7ffff000cd08 "java/lang/ClassLoader.class", __the_thread__=0x7ffff000c000)
at /home/dinosaur/jdk8/hotspot/src/share/vm/classfile/classLoader.cpp:330
#4 0x00007ffff640209b in ClassLoader::load_classfile (h_name=0x7ffff40621c8, __the_thread__=0x7ffff000c000) at /home/dinosaur/jdk8/hotspot/src/share/vm/classfile/classLoader.cpp:909
#5 0x00007ffff6a8570a in SystemDictionary::load_instance_class (class_name=0x7ffff40621c8, class_loader=..., __the_thread__=0x7ffff000c000)
at /home/dinosaur/jdk8/hotspot/src/share/vm/classfile/systemDictionary.cpp:1304
#6 0x00007ffff6a838b8 in SystemDictionary::resolve_instance_class_or_null (name=0x7ffff40621c8, class_loader=..., protection_domain=..., __the_thread__=0x7ffff000c000)
at /home/dinosaur/jdk8/hotspot/src/share/vm/classfile/systemDictionary.cpp:779
#7 0x00007ffff6a81ff7 in SystemDictionary::resolve_or_null (class_name=0x7ffff40621c8, class_loader=..., protection_domain=..., __the_thread__=0x7ffff000c000)
at /home/dinosaur/jdk8/hotspot/src/share/vm/classfile/systemDictionary.cpp:232
#8 0x00007ffff6a819f2 in SystemDictionary::resolve_or_fail (class_name=0x7ffff40621c8, class_loader=..., protection_domain=..., throw_error=true, __the_thread__=0x7ffff000c000)
at /home/dinosaur/jdk8/hotspot/src/share/vm/classfile/systemDictionary.cpp:171
#9 0x00007ffff6a81d64 in SystemDictionary::resolve_or_fail (class_name=0x7ffff40621c8, throw_error=true, __the_thread__=0x7ffff000c000)
at /home/dinosaur/jdk8/hotspot/src/share/vm/classfile/systemDictionary.cpp:212
#10 0x00007ffff6a87277 in SystemDictionary::initialize_wk_klass (id=SystemDictionary::ClassLoader_klass_knum, init_opt=0, __the_thread__=0x7ffff000c000)
at /home/dinosaur/jdk8/hotspot/src/share/vm/classfile/systemDictionary.cpp:1866
#11 0x00007ffff6a873a7 in SystemDictionary::initialize_wk_klasses_until (limit_id=SystemDictionary::SoftReference_klass_knum, start_id=@0x7ffff7fd0a84: SystemDictionary::Cloneable_klass_knum,
__the_thread__=0x7ffff000c000) at /home/dinosaur/jdk8/hotspot/src/share/vm/classfile/systemDictionary.cpp:1882
#12 0x00007ffff6a8b13c in SystemDictionary::initialize_wk_klasses_through (end_id=SystemDictionary::Reference_klass_knum, start_id=@0x7ffff7fd0a84: SystemDictionary::Cloneable_klass_knum,
__the_thread__=0x7ffff000c000) at /home/dinosaur/jdk8/hotspot/src/share/vm/classfile/systemDictionary.hpp:408
#13 0x00007ffff6a87553 in SystemDictionary::initialize_preloaded_classes (__the_thread__=0x7ffff000c000) at /home/dinosaur/jdk8/hotspot/src/share/vm/classfile/systemDictionary.cpp:1918
#14 0x00007ffff6a87199 in SystemDictionary::initialize (__the_thread__=0x7ffff000c000) at /home/dinosaur/jdk8/hotspot/src/share/vm/classfile/systemDictionary.cpp:1843
#15 0x00007ffff6ad68c9 in Universe::genesis (__the_thread__=0x7ffff000c000) at /home/dinosaur/jdk8/hotspot/src/share/vm/memory/universe.cpp:288
#16 0x00007ffff6ad8db6 in universe2_init () at /home/dinosaur/jdk8/hotspot/src/share/vm/memory/universe.cpp:991
#17 0x00007ffff66463b3 in init_globals () at /home/dinosaur/jdk8/hotspot/src/share/vm/runtime/init.cpp:114
#18 0x00007ffff6ab93ef in Threads::create_vm (args=0x7ffff7fd0e80, canTryAgain=0x7ffff7fd0e03) at /home/dinosaur/jdk8/hotspot/src/share/vm/runtime/thread.cpp:3424
#19 0x00007ffff6702ed0 in JNI_CreateJavaVM (vm=0x7ffff7fd0ed8, penv=0x7ffff7fd0ee0, args=0x7ffff7fd0e80) at /home/dinosaur/jdk8/hotspot/src/share/vm/prims/jni.cpp:5166
#20 0x00007ffff7bc3bda in InitializeJVM (pvm=0x7ffff7fd0ed8, penv=0x7ffff7fd0ee0, ifn=0x7ffff7fd0f30) at /home/dinosaur/jdk8/jdk/src/share/bin/java.c:1145
#21 0x00007ffff7bc1a36 in JavaMain (_args=0x7fffffffa910) at /home/dinosaur/jdk8/jdk/src/share/bin/java.c:371
#22 0x00007ffff73d66ba in start_thread (arg=0x7ffff7fd1700) at pthread_create.c:333
#23 0x00007ffff78f741d in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:109

打印hello world

这是打印hello world 的堆栈,估计是被优化了打印不了完整堆栈

(gdb) bt
#0 write () at ../sysdeps/unix/syscall-template.S:84
#1 0x00007ffff556779a in handleWrite (fd=1, buf=0x7ffff7fce270, len=12)
at /home/dinosaur/jdk8/jdk/src/solaris/native/java/io/io_util_md.c:164
#2 0x00007ffff556710a in writeBytes (env=0x7ffff000c210, this=0x7ffff7fd0398, bytes=0x7ffff7fd0390, off=0, len=12, append=0 '\000',
fid=0x47e1043) at /home/dinosaur/jdk8/jdk/src/share/native/java/io/io_util.c:189
#3 0x00007ffff555a79c in Java_java_io_FileOutputStream_writeBytes (env=0x7ffff000c210, this=0x7ffff7fd0398, bytes=0x7ffff7fd0390,
off=0, len=12, append=0 '\000') at /home/dinosaur/jdk8/jdk/src/solaris/native/java/io/FileOutputStream_md.c:70
#4 0x00007fffe10298dc in ?? ()
#5 0x0000000000000008 in ?? ()
#6 0x0000000000000008 in ?? ()
#7 0x00007ffff000c000 in ?? ()
#8 0x00007fffe02c74d8 in ?? ()
#9 0x00007fffe1028ee3 in ?? ()
#10 0x00007ffff7fd0318 in ?? ()
#11 0x00007fffe0173f60 in ?? ()
#12 0x00007ffff7fd0398 in ?? ()
#13 0x00007fffe0175120 in ?? ()
#14 0x0000000000000000 in ?? ()
(gdb) c
Continuing.
Hello, World

(gdb) bt
#0 write () at ../sysdeps/unix/syscall-template.S:84
#1 0x00007ffff556779a in handleWrite (fd=1, buf=0x7ffff7fce2d0, len=1)
at /home/dinosaur/jdk8/jdk/src/solaris/native/java/io/io_util_md.c:164
#2 0x00007ffff556710a in writeBytes (env=0x7ffff000c210, this=0x7ffff7fd0400, bytes=0x7ffff7fd03f8, off=0, len=1, append=0 '\000',
fid=0x47e1043) at /home/dinosaur/jdk8/jdk/src/share/native/java/io/io_util.c:189
#3 0x00007ffff555a79c in Java_java_io_FileOutputStream_writeBytes (env=0x7ffff000c210, this=0x7ffff7fd0400, bytes=0x7ffff7fd03f8,
off=0, len=1, append=0 '\000') at /home/dinosaur/jdk8/jdk/src/solaris/native/java/io/FileOutputStream_md.c:70
#4 0x00007fffe10298dc in ?? ()
#5 0x00007ffff7fd0410 in ?? ()
#6 0x00007ffff672dd43 in JVM_ArrayCopy (env=0x7ffff000c210, ignored=0x7ffff7fd0400, src=0x7ffff7fd03f8, src_pos=0,
dst=0x7f00f6265bea, dst_pos=1, length=0) at /home/dinosaur/jdk8/hotspot/src/share/vm/prims/jvm.cpp:298
#7 0x00007fffe1007500 in ?? ()
#8 0x0000000000000000 in ?? ()

java class file

4.1 The ClassFile Structure

A class file consists of a stream of 8-bit bytes. All 16-bit, 32-bit, and 64-bit quantities are constructed by reading in two, four, and eight consecutive 8-bit bytes, respectively. Multibyte data items are always stored in big-endian order, where the high bytes come first. In the Java SE platform, this format is supported by interfaces java.io.DataInput and java.io.DataOutput and classes such as java.io.DataInputStream and java.io.DataOutputStream.

通过jvm文档,可以知道class文件存的magic number0xCAFEBABE,存储方式是大端的

4.1 The ClassFile Structure A class file consists of a single ClassFile structure:

ClassFile {
u4 magic;
u2 minor_version;
u2 major_version;
u2 constant_pool_count;
cp_info constant_pool[constant_pool_count-1];
u2 access_flags;
u2 this_class;
u2 super_class;
u2 interfaces_count;
u2 interfaces[interfaces_count];
u2 fields_count;
field_info fields[fields_count];
u2 methods_count;
method_info methods[methods_count];
u2 attributes_count;
attribute_info attributes[attributes_count];
}

The magic item supplies the magic number identifying the class file format; it has the value 0xCAFEBABE.

dinosaur@dinosaur-X550VXK:~/jdk8/build$ hexdump  HelloWorld.class -C

使用hexdump查看class文件


00000000 ca fe ba be 00 00 00 34 00 1d 0a 00 06 00 0f 09 |.......4........|
00000010 00 10 00 11 08 00 12 0a 00 13 00 14 07 00 15 07 |................|
00000020 00 16 01 00 06 3c 69 6e 69 74 3e 01 00 03 28 29 |.....<init>...()|
00000030 56 01 00 04 43 6f 64 65 01 00 0f 4c 69 6e 65 4e |V...Code...LineN|
00000040 75 6d 62 65 72 54 61 62 6c 65 01 00 04 6d 61 69 |umberTable...mai|
00000050 6e 01 00 16 28 5b 4c 6a 61 76 61 2f 6c 61 6e 67 |n...([Ljava/lang|
00000060 2f 53 74 72 69 6e 67 3b 29 56 01 00 0a 53 6f 75 |/String;)V...Sou|
00000070 72 63 65 46 69 6c 65 01 00 0f 48 65 6c 6c 6f 57 |rceFile...HelloW|
00000080 6f 72 6c 64 2e 6a 61 76 61 0c 00 07 00 08 07 00 |orld.java.......|
00000090 17 0c 00 18 00 19 01 00 0c 48 65 6c 6c 6f 2c 20 |.........Hello, |
000000a0 57 6f 72 6c 64 07 00 1a 0c 00 1b 00 1c 01 00 0a |World...........|
000000b0 48 65 6c 6c 6f 57 6f 72 6c 64 01 00 10 6a 61 76 |HelloWorld...jav|
000000c0 61 2f 6c 61 6e 67 2f 4f 62 6a 65 63 74 01 00 10 |a/lang/Object...|
000000d0 6a 61 76 61 2f 6c 61 6e 67 2f 53 79 73 74 65 6d |java/lang/System|
000000e0 01 00 03 6f 75 74 01 00 15 4c 6a 61 76 61 2f 69 |...out...Ljava/i|
000000f0 6f 2f 50 72 69 6e 74 53 74 72 65 61 6d 3b 01 00 |o/PrintStream;..|
00000100 13 6a 61 76 61 2f 69 6f 2f 50 72 69 6e 74 53 74 |.java/io/PrintSt|
00000110 72 65 61 6d 01 00 07 70 72 69 6e 74 6c 6e 01 00 |ream...println..|
00000120 15 28 4c 6a 61 76 61 2f 6c 61 6e 67 2f 53 74 72 |.(Ljava/lang/Str|
00000130 69 6e 67 3b 29 56 00 21 00 05 00 06 00 00 00 00 |ing;)V.!........|
00000140 00 02 00 01 00 07 00 08 00 01 00 09 00 00 00 1d |................|
00000150 00 01 00 01 00 00 00 05 2a b7 00 01 b1 00 00 00 |........*.......|
00000160 01 00 0a 00 00 00 06 00 01 00 00 00 01 00 09 00 |................|
00000170 0b 00 0c 00 01 00 09 00 00 00 25 00 02 00 01 00 |..........%.....|
00000180 00 00 09 b2 00 02 12 03 b6 00 04 b1 00 00 00 01 |................|
00000190 00 0a 00 00 00 0a 00 02 00 00 00 05 00 08 00 06 |................|
000001a0 00 01 00 0d 00 00 00 02 00 0e |..........|

我们来看看hello world这个class文件的各种内容

第一个是magic number: ca fe ba be 四个字节
然后是minor_version:00 00
major_version:00 34

相关阅读