简体   繁体   中英

How to run java files from different packages in command prompt?

I need to run a java program in Windows using the command prompt. JAVAC is running perfectly in my computer. However, when I tried to compile my code (javac sampleprog.java), it displayed an error saying that it can't find the imported packages and classes I used in the program. What command should I use? Or how could I solve this? My java programs are stored in different packages and this sampleprog.java uses an API and calls different methods from different classes.

Thanks!

Navigate to the directory that contains the compiled class files eg c:\\myproj\\classes

Then invoke java com.myproject.MyClass where com.myproject.MyClass is the fully qualified path to MyClass.

If you have dependencies on third party libraries you may also need to add the -classpath flag eg java -classpath c:\\path\\to\\some\\lib.jar com.myproject.MyClass

使用javac编译之后,可以使用java YourFile运行它

Go to root of your class files. For example c:/proj/classes . If your class name is MyClass and it belongs to package com.mycompany you should find file com/mycompany/MyClass.class there.

If so type in command prompt java com.mycompany.MyClass and press ENTER.

Pay attention that when you are using javac you have to type the extension *.java , however when you are running the class you have not type extension .class . The string com.mycompany.MyClass is called fully qualified class name . This name is used by JVM to load class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM