简体   繁体   中英

Java: How do I run this eclipse-compiled program on the command line?

I have a simple Hello World program that works properly when run from within Eclipse. What do I need to do to run this program from the command line?

~/g/private/eclipse/Hello/bin --> java Hello.class
Error: Could not find or load main class Hello.class

Here are the eclipse-generated files:

~/g/private/eclipse/Hello --> find . -type f
./.classpath
./.gitignore
./.project
./.settings/org.eclipse.jdt.core.prefs
./bin/Hello.class
./src/Hello.java

and the eclipse-generated .classpath :

~/g/private/eclipse/Hello --> cat .classpath
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" path="src"/>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
    <classpathentry kind="output" path="bin"/>
</classpath>

(note) I don't think this is a dupe of this question, as I'm explicitly asking about running a program compiled in eclipse. How to run Java program in command prompt

java.exe or java expects a class name as its argument, not a file name

java -classpath C:\user\ Hello

So running java Hello.class will tell it to go looking for hello.class.class file.

or define classpath and use

java -cp C:\user\ Hello

PS : From Stackoverflow source

no need to give .class

~/g/private/eclipse/Hello/bin --> java Hello.class

just run like

~/g/private/eclipse/Hello/bin --> java Hello

or, specifying the class path:

~/g/private/eclipse/Hello java -cp bin Hello

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