ℹ️ Skipped - page is already crawled
| Filter | Status | Condition | Details |
|---|---|---|---|
| HTTP status | PASS | download_http_code = 200 | HTTP 200 |
| Age cutoff | PASS | download_stamp > now() - 6 MONTH | 0.7 months ago |
| History drop | PASS | isNull(history_drop_reason) | No drop reason |
| Spam/ban | PASS | fh_dont_index != 1 AND ml_spam_score = 0 | ml_spam_score=0 |
| Canonical | PASS | meta_canonical IS NULL OR = '' OR = src_unparsed | Not set |
| Property | Value |
|---|---|
| URL | https://stackoverflow.com/questions/1279542/how-to-execute-a-java-class-from-the-command-line |
| Last Crawled | 2026-03-29 07:26:26 (20 days ago) |
| First Indexed | 2017-05-24 18:40:48 (8 years ago) |
| HTTP Status Code | 200 |
| Meta Title | How to execute a java .class from the command line - Stack Overflow |
| Meta Description | null |
| Meta Canonical | null |
| Boilerpipe Text | This question shows research effort; it is useful and clear
146
Save this question.
Show activity on this post.
I have a compiled java class:
Echo.class
public
class
Echo
{
public
static
void
main
(String arg)
{
System.out.println(arg);
}
}
I
cd
to the directory and enter:
java Echo "hello"
I get this error:
C:\Documents and Settings\joe\My Documents\projects\Misc\bin>java Echo
"hello"
Exception in thread
"main"
java.lang.NoClassDefFoundError: Echo
Caused by: java.lang.ClassNotFoundException: Echo
at java.net.URLClassLoader$
1.
run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
Could not find the main class: Echo. Program will exit.
What is the simplest way to get my java code in a form that I can run from the command line as apposed to having to use Eclipse IDE?
Mel
6,105
10 gold badges
40 silver badges
42 bronze badges
asked
Aug 14, 2009 at 18:52
1
This answer is useful
144
Save this answer.
Show activity on this post.
Try:
java -cp . Echo
"hello"
Assuming that you compiled with:
javac Echo.java
Then there is a chance that the "current" directory is not in your classpath ( where java looks for .class definitions )
If that's the case and listing the contents of your dir displays:
Echo.java
Echo.class
Then any of this may work:
java -cp . Echo
"hello"
or
SET CLASSPATH=%CLASSPATH;.
java Echo
"hello"
And later as
Fredrik
points out you'll get another error message like.
Exception in thread "main" java.lang.NoSuchMethodError: main
When that happens, go and read his answer :)
answered
Aug 14, 2009 at 18:53
1 Comment
The -cp . to tell Java that he should add . (current working directory) to the classpath, meaning the place where he is looking for code.
2009-08-14T19:24:39.143Z+00:00
This answer is useful
53
Save this answer.
Show activity on this post.
With Java 11 you won't have to go through this rigmarole anymore!
Instead, you can do this:
> java MyApp.java
You don't have to compile beforehand, as it's all done in one step.
You can get the Java 11 JDK here:
JDK 11 GA Release
answered
Oct 4, 2018 at 6:12
Ryan Lundy
211k
41 gold badges
187 silver badges
216 bronze badges
1 Comment
> copy C:\Desenv\workspaceServer\TestProject\src\WebServiceStandAlone\MyApp.java . /y && javac MyApp.java && java -cp . MyApp && del MyApp.class && del MyApp.java Some guys do prefer the old fashion way =)
2018-11-13T20:15:04.063Z+00:00
This answer is useful
21
Save this answer.
Show activity on this post.
You need to specify the classpath. This should do it:
java -cp . Echo
"hello"
This tells java to use
.
(the current directory) as its classpath, i.e. the place where it looks for classes. Note than when you use packages, the classpath has to contain the root directory, not the package subdirectories. e.g. if your class is
my.package.Echo
and the .class file is
bin/my/package/Echo.class
, the correct classpath directory is
bin
.
answered
Aug 14, 2009 at 18:54
Comments
This answer is useful
21
Save this answer.
Show activity on this post.
You have no valid main method... The signature should be:
public static void main(
String[]
args);
Hence, in your case the code should look like this:
public
class
Echo
{
public
static
void
main
(String[] arg)
{
System.out.println(arg[
0
]);
}
}
Edit:
Please note that
Oscar
is also right in that you are missing . in your classpath, you would run into the problem I solve after you have dealt with that error.
answered
Aug 14, 2009 at 18:54
Fredrik
5,859
2 gold badges
29 silver badges
33 bronze badges
2 Comments
The only one to give the right result with newer java's.
2023-09-03T12:42:17.977Z+00:00
Keep in mind this thread dates back to 2009 :-)
2023-09-08T07:30:46.43Z+00:00
This answer is useful
8
Save this answer.
Show activity on this post.
If you have in your java source
package
mypackage;
and your class is hello.java
with
public
class
hello
{
and in that hello.java you have
public
static
void
main
(String[] args)
{
Then
(after compilation)
changeDir (cd) to the directory where your hello.class is.
Then
java -cp . mypackage.hello
Mind the current directory and the package name before the class name.
It works for my on linux mint and i hope on the other os's also
Thanks Stack overflow for a wealth of info.
answered
Mar 17, 2018 at 8:22
1 Comment
This answer is useful
3
Save this answer.
Show activity on this post.
My situation was a little complicated. I had to do three steps since I was using a .dll in the resources directory, for JNI code. My files were
S:\Accessibility\tools\src\main\resources\dlls\HelloWorld.dll
S:\Accessibility\tools\src\test\java\com\accessibility\HelloWorld.class
My code contained the following line
System.load(HelloWorld.class.getResource(
"/dlls/HelloWorld.dll"
).getPath());
First, I had to move to the classpath directory
cd /D
"S:\Accessibility\tools\src\test\java"
Next, I had to change the classpath to point to the current directory so that my class would be loaded and I had to change the classpath to point to he resources directory so my dll would be loaded.
set classpath=%classpath%;.;..\..\..\src\main\resources;
Then, I had to run java using the classname.
java com.accessibility.HelloWorld
answered
Aug 10, 2013 at 1:30
Scott Izu
2,319
25 silver badges
14 bronze badges
Comments
This answer is useful
0
Save this answer.
Show activity on this post.
First, have you compiled the class using the command line javac compiler? Second, it seems that your main method has an incorrect signature - it should be taking in an array of String objects, rather than just one:
public
static
void
main
(String[] args)
{
Once you've changed your code to take in an array of String objects, then you need to make sure that you're printing an element of the array, rather than array itself:
System.out.println(args[
0
])
If you want to print the whole list of command line arguments, you'd need to use a loop, e.g.
for
(
int
i
=
0
; i < args.length; i++){
System.out.print(args[i]);
}
System.out.println();
answered
Aug 14, 2009 at 19:03
quanticle
5,070
6 gold badges
35 silver badges
42 bronze badges
Comments
Start asking to get answers
Find the answer to your question by asking.
Ask question
Explore related questions
See similar questions with these tags. |
| Markdown | # 
By clicking “Sign up”, you agree to our [terms of service](https://stackoverflow.com/legal/terms-of-service/public) and acknowledge you have read our [privacy policy](https://stackoverflow.com/legal/privacy-policy).
# OR
Already have an account? [Log in](https://stackoverflow.com/users/login)
[Skip to main content](https://stackoverflow.com/questions/1279542/how-to-execute-a-java-class-from-the-command-line#content)
1. [About](https://stackoverflow.co/)
2. Products
3. [For Teams](https://stackoverflow.co/internal/)
4. Try new site Try BETA
1. [Stack Internal Implement a knowledge platform layer to power your enterprise and AI tools.](https://stackoverflow.co/internal/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=top-nav&utm_content=stack-overflow-for-teams)
2. [Stack Data Licensing Get access to top-class technical expertise with trusted & attributed content.](https://stackoverflow.co/data-licensing/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=top-nav&utm_content=overflow-api)
3. [Stack Ads Connect your brand to the world’s most trusted technologist communities.](https://stackoverflow.co/advertising/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=top-nav&utm_content=stack-overflow-advertising)
4. [Releases Keep up-to-date on features we add to Stack Overflow and Stack Internal.](https://stackoverflow.blog/releases/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=top-nav&utm_content=releases)
5. [About the company](https://stackoverflow.co/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=top-nav&utm_content=about-the-company) [Visit the blog](https://stackoverflow.blog/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=top-nav&utm_content=blog)
1. ### [current community](https://stackoverflow.com/)
- [Stack Overflow](https://stackoverflow.com/)
[help](https://stackoverflow.com/help) [chat](https://chat.stackoverflow.com/?tab=explore)
- [Meta Stack Overflow](https://meta.stackoverflow.com/)
### your communities
[Sign up](https://stackoverflow.com/users/signup?ssrc=site_switcher&returnurl=https%3A%2F%2Fstackoverflow.com%2Fquestions%2F1279542%2Fhow-to-execute-a-java-class-from-the-command-line) or [log in](https://stackoverflow.com/users/login?ssrc=site_switcher&returnurl=https%3A%2F%2Fstackoverflow.com%2Fquestions%2F1279542%2Fhow-to-execute-a-java-class-from-the-command-line) to customize your list.
### [more stack exchange communities](https://stackexchange.com/sites)
[company blog](https://stackoverflow.blog/)
2. [Log in](https://stackoverflow.com/users/login?ssrc=head&returnurl=https%3A%2F%2Fstackoverflow.com%2Fquestions%2F1279542%2Fhow-to-execute-a-java-class-from-the-command-line)
3. [Sign up](https://stackoverflow.com/users/signup?ssrc=head&returnurl=https%3A%2F%2Fstackoverflow.com%2Fquestions%2F1279542%2Fhow-to-execute-a-java-class-from-the-command-line)
# Let's set up your homepage Select a few topics you're interested in:
python
javascript
c\#
reactjs
java
android
html
flutter
c++
node.js
typescript
css
r
php
angular
next.js
spring-boot
machine-learning
sql
excel
ios
azure
docker
Or search from our full list:
- javascript
- python
- java
- c\#
- php
- android
- html
- jquery
- c++
- css
- ios
- sql
- mysql
- r
- reactjs
- node.js
- arrays
- c
- asp.net
- json
- python-3.x
- .net
- ruby-on-rails
- sql-server
- swift
- django
- angular
- objective-c
- excel
- pandas
- angularjs
- regex
- typescript
- ruby
- linux
- ajax
- iphone
- vba
- xml
- laravel
- spring
- asp.net-mvc
- database
- wordpress
- string
- flutter
- postgresql
- mongodb
- wpf
- windows
- xcode
- amazon-web-services
- bash
- git
- oracle-database
- spring-boot
- dataframe
- azure
- firebase
- list
- multithreading
- docker
- vb.net
- react-native
- eclipse
- algorithm
- powershell
- macos
- visual-studio
- numpy
- image
- forms
- scala
- function
- vue.js
- performance
- twitter-bootstrap
- selenium
- winforms
- kotlin
- loops
- dart
- express
- sqlite
- hibernate
- matlab
- python-2.7
- shell
- rest
- apache
- entity-framework
- android-studio
- csv
- maven
- linq
- qt
- dictionary
- unit-testing
- asp.net-core
- facebook
- apache-spark
- tensorflow
- file
- swing
- class
- unity-game-engine
- sorting
- date
- authentication
- go
- symfony
- t-sql
- opencv
- matplotlib
- .htaccess
- google-chrome
- for-loop
- datetime
- codeigniter
- perl
- http
- validation
- sockets
- google-maps
- object
- uitableview
- xaml
- oop
- visual-studio-code
- if-statement
- cordova
- ubuntu
- web-services
- email
- android-layout
- github
- spring-mvc
- elasticsearch
- kubernetes
- selenium-webdriver
- ms-access
- ggplot2
- user-interface
- parsing
- pointers
- google-sheets
- c++11
- security
- machine-learning
- google-apps-script
- ruby-on-rails-3
- templates
- flask
- nginx
- variables
- exception
- sql-server-2008
- gradle
- debugging
- tkinter
- delphi
- listview
- jpa
- asynchronous
- haskell
- web-scraping
- jsp
- pdf
- ssl
- amazon-s3
- google-cloud-platform
- xamarin
- testing
- jenkins
- wcf
- batch-file
- generics
- npm
- ionic-framework
- network-programming
- unix
- recursion
- google-app-engine
- mongoose
- visual-studio-2010
- .net-core
- android-fragments
- assembly
- animation
- math
- rust
- svg
- session
- intellij-idea
- hadoop
- join
- winapi
- curl
- django-models
- laravel-5
- next.js
- url
- heroku
- http-redirect
- tomcat
- inheritance
- google-cloud-firestore
- webpack
- gcc
- swiftui
- image-processing
- keras
- asp.net-mvc-4
- logging
- dom
- matrix
- pyspark
- actionscript-3
- button
- post
- optimization
- firebase-realtime-database
- cocoa
- jquery-ui
- xpath
- iis
- web
- d3.js
- javafx
- firefox
- xslt
- internet-explorer
- caching
- select
- asp.net-mvc-3
- opengl
- events
- asp.net-web-api
- plot
- dplyr
- encryption
- magento
- stored-procedures
- search
- amazon-ec2
- ruby-on-rails-4
- memory
- multidimensional-array
- canvas
- audio
- random
- jsf
- vector
- redux
- cookies
- input
- facebook-graph-api
- flash
- indexing
- xamarin.forms
- arraylist
- ipad
- cocoa-touch
- data-structures
- video
- model-view-controller
- azure-devops
- serialization
- apache-kafka
- jdbc
- razor
- awk
- woocommerce
- routes
- mod-rewrite
- servlets
- excel-formula
- beautifulsoup
- filter
- iframe
- docker-compose
- design-patterns
- aws-lambda
- text
- visual-c++
- django-rest-framework
- cakephp
- mobile
- android-intent
- struct
- react-hooks
- methods
- groovy
- mvvm
- lambda
- ssh
- time
- checkbox
- ecmascript-6
- grails
- installation
- google-chrome-extension
- cmake
- sharepoint
- shiny
- spring-security
- jakarta-ee
- plsql
- android-recyclerview
- core-data
- types
- sed
- meteor
- android-activity
- bootstrap-4
- activerecord
- websocket
- replace
- graph
- group-by
- scikit-learn
- vim
- file-upload
- boost
- junit
- memory-management
- sass
- async-await
- import
- deep-learning
- error-handling
- eloquent
- dynamic
- dependency-injection
- silverlight
- soap
- layout
- apache-spark-sql
- charts
- deployment
- browser
- gridview
- svn
- while-loop
- google-bigquery
- vuejs2
- highcharts
- dll
- ffmpeg
- view
- foreach
- makefile
- plugins
- redis
- c\#-4.0
- reporting-services
- jupyter-notebook
- unicode
- merge
- reflection
- https
- server
- google-maps-api-3
- twitter
- oauth-2.0
- extjs
- terminal
- pip
- axios
- split
- cmd
- encoding
- pytorch
- django-views
- collections
- database-design
- hash
- netbeans
- automation
- data-binding
- ember.js
- build
- tcp
- pdo
- apache-flex
- sqlalchemy
- entity-framework-core
- concurrency
- command-line
- spring-data-jpa
- printing
- react-redux
- java-8
- lua
- html-table
- neo4j
- ansible
- service
- jestjs
- enums
- parameters
- flexbox
- mysqli
- promise
- material-ui
- module
- visual-studio-2012
- outlook
- web-applications
- uwp
- webview
- firebase-authentication
- jquery-mobile
- utf-8
- datatable
- python-requests
- parallel-processing
- colors
- drop-down-menu
- scipy
- tfs
- scroll
- hive
- count
- syntax
- ms-word
- twitter-bootstrap-3
- ssis
- fonts
- rxjs
- constructor
- file-io
- google-analytics
- paypal
- three.js
- powerbi
- cassandra
- graphql
- discord
- graphics
- compiler-errors
- gwt
- react-router
- socket.io
- backbone.js
- solr
- memory-leaks
- url-rewriting
- datatables
- nlp
- terraform
- oauth
- datagridview
- drupal
- zend-framework
- oracle11g
- knockout.js
- triggers
- interface
- neural-network
- django-forms
- casting
- angular-material
- jmeter
- linked-list
- google-api
- path
- timer
- django-templates
- arduino
- orm
- windows-phone-7
- directory
- proxy
- parse-platform
- visual-studio-2015
- cron
- conditional-statements
- push-notification
- functional-programming
- primefaces
- pagination
- model
- jar
- xamarin.android
- hyperlink
- uiview
- visual-studio-2013
- vbscript
- google-cloud-functions
- azure-active-directory
- gitlab
- jwt
- download
- swift3
- sql-server-2005
- rspec
- process
- pygame
- configuration
- properties
- callback
- combobox
- windows-phone-8
- linux-kernel
- safari
- scrapy
- emacs
- permissions
- x86
- clojure
- scripting
- raspberry-pi
- io
- scope
- azure-functions
- compilation
- mongodb-query
- responsive-design
- expo
- nhibernate
- angularjs-directive
- reference
- binding
- bluetooth
- request
- architecture
- dns
- playframework
- 3d
- pyqt
- version-control
- discord.js
- doctrine-orm
- package
- f\#
- rubygems
- get
- sql-server-2012
- autocomplete
- tree
- datepicker
- openssl
- kendo-ui
- jackson
- yii
- controller
- grep
- nested
- xamarin.ios
- static
- null
- transactions
- statistics
- datagrid
- active-directory
- uiviewcontroller
- dockerfile
- webforms
- sas
- discord.py
- computer-vision
- phpmyadmin
- notifications
- duplicates
- pycharm
- mocking
- youtube
- nullpointerexception
- yaml
- menu
- blazor
- sum
- plotly
- bitmap
- visual-studio-2008
- asp.net-mvc-5
- floating-point
- yii2
- css-selectors
- stl
- android-listview
- electron
- jsf-2
- time-series
- cryptography
- ant
- hashmap
- character-encoding
- msbuild
- stream
- asp.net-core-mvc
- sdk
- google-drive-api
- selenium-chromedriver
- jboss
- joomla
- devise
- navigation
- cuda
- cors
- frontend
- anaconda
- background
- multiprocessing
- binary
- pyqt5
- camera
- iterator
- linq-to-sql
- mariadb
- onclick
- ios7
- android-jetpack-compose
- microsoft-graph-api
- android-asynctask
- rabbitmq
- tabs
- amazon-dynamodb
- laravel-4
- environment-variables
- uicollectionview
- insert
- linker
- coldfusion
- xsd
- console
- continuous-integration
- upload
- textview
- ftp
- opengl-es
- macros
- operating-system
- mockito
- localization
- formatting
- xml-parsing
- json.net
- type-conversion
- vuejs3
- data.table
- kivy
- timestamp
- integer
- calendar
- segmentation-fault
- android-ndk
- prolog
- char
- drag-and-drop
- crash
- jasmine
- azure-pipelines
- automated-tests
- dependencies
- geometry
- fortran
- android-gradle-plugin
- itext
- sprite-kit
- mfc
- header
- attributes
- nosql
- format
- firebase-cloud-messaging
- nuxt.js
- odoo
- db2
- jquery-plugins
- event-handling
- julia
- jenkins-pipeline
- leaflet
- annotations
- flutter-layout
- keyboard
- nestjs
- postman
- arm
- textbox
- stripe-payments
- visual-studio-2017
- gulp
- libgdx
- uikit
- timezone
- synchronization
- azure-web-app-service
- dom-events
- wso2
- google-sheets-formula
- xampp
- crystal-reports
- aggregation-framework
- namespaces
- android-emulator
- uiscrollview
- swagger
- jvm
- sequelize.js
- chart.js
- com
- snowflake-cloud-data-platform
- subprocess
- html5-canvas
- geolocation
- garbage-collection
- webdriver
- sql-update
- centos
- dialog
- concatenation
- numbers
- widget
- qml
- tuples
- set
- java-stream
- mapreduce
- ionic2
- smtp
- windows-10
- rotation
- android-edittext
- nuget
- modal-dialog
- spring-data
- radio-button
- doctrine
- http-headers
- grid
- lucene
- sonarqube
- xmlhttprequest
- listbox
- initialization
- switch-statement
- internationalization
- boolean
- components
- apache-camel
- google-play
- gdb
- serial-port
- ios5
- return
- ldap
- youtube-api
- pivot
- eclipse-plugin
- latex
- frameworks
- tags
- containers
- c++17
- github-actions
- subquery
- embedded
- dataset
- foreign-keys
- asp-classic
- label
- uinavigationcontroller
- delegates
- copy
- google-cloud-storage
- struts2
- migration
- protractor
- base64
- uibutton
- find
- queue
- sql-server-2008-r2
- arguments
- composer-php
- append
- jaxb
- stack
- tailwind-css
- zip
- cucumber
- autolayout
- ide
- entity-framework-6
- iteration
- popup
- r-markdown
- windows-7
- vb6
- airflow
- g++
- clang
- hover
- ssl-certificate
- jqgrid
- range
- gmail
Next
You’ll be prompted to create an account to view your personalized homepage.
1. 1. [Home](https://stackoverflow.com/)
2. [Questions](https://stackoverflow.com/questions)
3. [AI Assist](https://stackoverflow.com/ai-assist)
4. [Tags](https://stackoverflow.com/tags)
5. [Challenges](https://stackoverflow.com/beta/challenges)
6. [Chat](https://chat.stackoverflow.com/?tab=explore)
7. [Articles](https://stackoverflow.blog/contributed?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=so-blog&utm_content=experiment-articles)
8. [Users](https://stackoverflow.com/users)
9. [Companies](https://stackoverflow.com/jobs/companies?so_medium=stackoverflow&so_source=SiteNav)
10. [Collectives]()
11. Communities for your favorite technologies. [Explore all Collectives](https://stackoverflow.com/collectives-all)
2. Stack Internal
Stack Overflow for Teams is now called **Stack Internal**. Bring the best of human thought and AI automation together at your work.
[Try for free](https://stackoverflowteams.com/teams/create/free/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=side-bar&utm_content=explore-teams) [Learn more](https://stackoverflow.co/internal/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=side-bar&utm_content=explore-teams)
3. [Stack Internal]()
4. Bring the best of human thought and AI automation together at your work. [Learn more](https://stackoverflow.co/internal/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=side-bar&utm_content=explore-teams-compact)
##### Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
[Learn more about Collectives](https://stackoverflow.com/collectives)
**Stack Internal**
Knowledge at work
Bring the best of human thought and AI automation together at your work.
[Explore Stack Internal](https://stackoverflow.co/internal/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=side-bar&utm_content=explore-teams-compact-popover)
# [How to execute a java .class from the command line](https://stackoverflow.com/questions/1279542/how-to-execute-a-java-class-from-the-command-line)
[Ask Question](https://stackoverflow.com/questions/ask)
Asked
16 years, 7 months ago
Modified [3 years, 8 months ago](https://stackoverflow.com/questions/1279542/how-to-execute-a-java-class-from-the-command-line?lastactivity "2022-06-18 04:27:08Z")
Viewed 529k times
This question shows research effort; it is useful and clear
146
Save this question.
Show activity on this post.
I have a compiled java class:
**Echo.class**
```
Copy
```
I `cd` to the directory and enter: `java Echo "hello"`
I get this error:
```
Copy
```
What is the simplest way to get my java code in a form that I can run from the command line as apposed to having to use Eclipse IDE?
- [java](https://stackoverflow.com/questions/tagged/java "show questions tagged 'java'")
[Share](https://stackoverflow.com/q/1279542 "Short permalink to this question")
Share a link to this question
Copy link
[CC BY-SA 3.0](https://creativecommons.org/licenses/by-sa/3.0/ "The current license for this post: CC BY-SA 3.0")
[Improve this question](https://stackoverflow.com/posts/1279542/edit)
Follow
Follow this question to receive notifications
[edited Apr 27, 2017 at 10:38](https://stackoverflow.com/posts/1279542/revisions "show all edits to this post")
[](https://stackoverflow.com/users/4720935/mel)
[Mel](https://stackoverflow.com/users/4720935/mel)
6,1051010 gold badges4040 silver badges4242 bronze badges
asked Aug 14, 2009 at 18:52
[](https://stackoverflow.com/users/5653/joe)
[joe](https://stackoverflow.com/users/5653/joe)
17\.6k3737 gold badges9999 silver badges134134 bronze badges
1
- 22
Suggestion: The original question would, perhaps, better be left unedited in order not to invalidate the answers, under a quick review. Really weird to see the right method signature and then a bunch of answers that say it was incorrect. Erroneous code is supposed to be left that, for the purpose of comparison with the correct version, at least.
JSmyth
– [JSmyth](https://stackoverflow.com/users/588966/jsmyth "1,504 reputation")
2014-02-03 09:34:18 +00:00
[Commented Feb 3, 2014 at 9:34](https://stackoverflow.com/questions/1279542/how-to-execute-a-java-class-from-the-command-line#comment32497877_1279542)
[Add a comment](https://stackoverflow.com/questions/1279542/how-to-execute-a-java-class-from-the-command-line "Use comments to ask for more information or suggest improvements. Avoid answering questions in comments.") \|
## 7 Answers 7
Sorted by:
[Reset to default](https://stackoverflow.com/questions/1279542/how-to-execute-a-java-class-from-the-command-line?answertab=scoredesc#tab-top)
This answer is useful
144
Save this answer.
Show activity on this post.
Try:
```
Copyjava -cp . Echo "hello"
```
***
Assuming that you compiled with:
```
Copyjavac Echo.java
```
Then there is a chance that the "current" directory is not in your classpath ( where java looks for .class definitions )
If that's the case and listing the contents of your dir displays:
```
Copy
```
Then any of this may work:
```
Copyjava -cp . Echo "hello"
```
or
```
Copy
```
And later as [Fredrik](https://stackoverflow.com/questions/1279542/how-to-execute-a-java-class-from-the-command-line/1279560#1279560) points out you'll get another error message like.
> *Exception in thread "main" java.lang.NoSuchMethodError: main*
When that happens, go and read his answer :)
[Share](https://stackoverflow.com/a/1279556 "Short permalink to this answer")
Share a link to this answer
Copy link
[CC BY-SA 2.5](https://creativecommons.org/licenses/by-sa/2.5/ "The current license for this post: CC BY-SA 2.5")
[Improve this answer](https://stackoverflow.com/posts/1279556/edit)
Follow
Follow this answer to receive notifications
[edited May 23, 2017 at 10:31](https://stackoverflow.com/posts/1279556/revisions "show all edits to this post")
[](https://stackoverflow.com/users/-1/community)
[Community](https://stackoverflow.com/users/-1/community)Bot
111 silver badge
answered Aug 14, 2009 at 18:53
[](https://stackoverflow.com/users/20654/oscarryz)
[OscarRyz](https://stackoverflow.com/users/20654/oscarryz)
200k119119 gold badges399399 silver badges577577 bronze badges
Sign up to request clarification or add additional context in comments.
## 1 Comment
Add a comment
[](https://stackoverflow.com/users/59572/fredrik)
Fredrik
[Fredrik](https://stackoverflow.com/users/59572/fredrik)
[Over a year ago](https://stackoverflow.com/questions/1279542/how-to-execute-a-java-class-from-the-command-line#comment1108622_1279556)
The -cp . to tell Java that he should add . (current working directory) to the classpath, meaning the place where he is looking for code.
2009-08-14T19:24:39.143Z+00:00
3
Reply
- Copy link
This answer is useful
53
Save this answer.
Show activity on this post.
With Java 11 you won't have to go through this rigmarole anymore\!
Instead, you can do this:
```
Copy> java MyApp.java
```
You don't have to compile beforehand, as it's all done in one step.
You can get the Java 11 JDK here: [JDK 11 GA Release](https://jdk.java.net/11/)
[Share](https://stackoverflow.com/a/52640128 "Short permalink to this answer")
Share a link to this answer
Copy link
[CC BY-SA 4.0](https://creativecommons.org/licenses/by-sa/4.0/ "The current license for this post: CC BY-SA 4.0")
[Improve this answer](https://stackoverflow.com/posts/52640128/edit)
Follow
Follow this answer to receive notifications
answered Oct 4, 2018 at 6:12
[](https://stackoverflow.com/users/5486/ryan-lundy)
[Ryan Lundy](https://stackoverflow.com/users/5486/ryan-lundy)
211k4141 gold badges187187 silver badges216216 bronze badges
## 1 Comment
Add a comment
[](https://stackoverflow.com/users/929263/jr)
Jr.
[Jr.](https://stackoverflow.com/users/929263/jr)
[Over a year ago](https://stackoverflow.com/questions/1279542/how-to-execute-a-java-class-from-the-command-line#comment93459597_52640128)
\> copy C:\\Desenv\\workspaceServer\\TestProject\\src\\WebServiceStandAlone\\MyApp.java . /y && javac MyApp.java && java -cp . MyApp && del MyApp.class && del MyApp.java Some guys do prefer the old fashion way =)
2018-11-13T20:15:04.063Z+00:00
0
Reply
- Copy link
This answer is useful
21
Save this answer.
Show activity on this post.
You need to specify the classpath. This should do it:
```
Copyjava -cp . Echo "hello"
```
This tells java to use `.` (the current directory) as its classpath, i.e. the place where it looks for classes. Note than when you use packages, the classpath has to contain the root directory, not the package subdirectories. e.g. if your class is `my.package.Echo` and the .class file is `bin/my/package/Echo.class`, the correct classpath directory is `bin`.
[Share](https://stackoverflow.com/a/1279559 "Short permalink to this answer")
Share a link to this answer
Copy link
[CC BY-SA 2.5](https://creativecommons.org/licenses/by-sa/2.5/ "The current license for this post: CC BY-SA 2.5")
[Improve this answer](https://stackoverflow.com/posts/1279559/edit)
Follow
Follow this answer to receive notifications
answered Aug 14, 2009 at 18:54
[](https://stackoverflow.com/users/16883/michael-borgwardt)
[Michael Borgwardt](https://stackoverflow.com/users/16883/michael-borgwardt)
347k8181 gold badges491491 silver badges726726 bronze badges
## Comments
Add a comment
This answer is useful
21
Save this answer.
Show activity on this post.
You have no valid main method... The signature should be: public static void main(**String\[\]** args);
Hence, in your case the code should look like this:
```
Copy
```
**Edit:** Please note that [Oscar](https://stackoverflow.com/questions/1279542/how-to-execute-a-java-class-from-the-command-line/1279556#1279556) is also right in that you are missing . in your classpath, you would run into the problem I solve after you have dealt with that error.
[Share](https://stackoverflow.com/a/1279560 "Short permalink to this answer")
Share a link to this answer
Copy link
[CC BY-SA 3.0](https://creativecommons.org/licenses/by-sa/3.0/ "The current license for this post: CC BY-SA 3.0")
[Improve this answer](https://stackoverflow.com/posts/1279560/edit)
Follow
Follow this answer to receive notifications
[edited May 23, 2017 at 12:02](https://stackoverflow.com/posts/1279560/revisions "show all edits to this post")
[](https://stackoverflow.com/users/-1/community)
[Community](https://stackoverflow.com/users/-1/community)Bot
111 silver badge
answered Aug 14, 2009 at 18:54
[](https://stackoverflow.com/users/59572/fredrik)
[Fredrik](https://stackoverflow.com/users/59572/fredrik)
5,85922 gold badges2929 silver badges3333 bronze badges
## 2 Comments
Add a comment
[](https://stackoverflow.com/users/4952917/erich-k%C3%BCster)
Erich KĂĽster
[Erich KĂĽster](https://stackoverflow.com/users/4952917/erich-k%C3%BCster)
[Over a year ago](https://stackoverflow.com/questions/1279542/how-to-execute-a-java-class-from-the-command-line#comment135798271_1279560)
The only one to give the right result with newer java's.
2023-09-03T12:42:17.977Z+00:00
0
Reply
- Copy link
[](https://stackoverflow.com/users/59572/fredrik)
Fredrik
[Fredrik](https://stackoverflow.com/users/59572/fredrik)
[Over a year ago](https://stackoverflow.com/questions/1279542/how-to-execute-a-java-class-from-the-command-line#comment135857198_1279560)
Keep in mind this thread dates back to 2009 :-)
2023-09-08T07:30:46.43Z+00:00
0
Reply
- Copy link
This answer is useful
8
Save this answer.
Show activity on this post.
If you have in your java source
```
Copypackage mypackage;
```
and your class is hello.java with
```
Copypublic class hello {
```
and in that hello.java you have
```
Copy public static void main(String[] args) {
```
Then (after compilation) changeDir (cd) to the directory where your hello.class is. Then
```
Copyjava -cp . mypackage.hello
```
Mind the current directory and the package name before the class name. It works for my on linux mint and i hope on the other os's also
Thanks Stack overflow for a wealth of info.
[Share](https://stackoverflow.com/a/49333968 "Short permalink to this answer")
Share a link to this answer
Copy link
[CC BY-SA 3.0](https://creativecommons.org/licenses/by-sa/3.0/ "The current license for this post: CC BY-SA 3.0")
[Improve this answer](https://stackoverflow.com/posts/49333968/edit)
Follow
Follow this answer to receive notifications
answered Mar 17, 2018 at 8:22
[](https://stackoverflow.com/users/8344297/gerard-doets)
[Gerard Doets](https://stackoverflow.com/users/8344297/gerard-doets)
8111 silver badge22 bronze badges
## 1 Comment
Add a comment
[](https://stackoverflow.com/users/1300170/ruvim)
ruvim
[ruvim](https://stackoverflow.com/users/1300170/ruvim)
[Over a year ago](https://stackoverflow.com/questions/1279542/how-to-execute-a-java-class-from-the-command-line#comment87017442_49333968)
NB: the class file should be foundable via `./mypackage/hello.class` name. See also: "[Running java in package from command line](https://stackoverflow.com/questions/19433366/running-java-in-package-from-command-line)" question.
2018-04-24T11:40:03.46Z+00:00
3
Reply
- Copy link
This answer is useful
3
Save this answer.
Show activity on this post.
My situation was a little complicated. I had to do three steps since I was using a .dll in the resources directory, for JNI code. My files were
```
Copy
```
My code contained the following line
```
CopySystem.load(HelloWorld.class.getResource("/dlls/HelloWorld.dll").getPath());
```
First, I had to move to the classpath directory
```
Copycd /D "S:\Accessibility\tools\src\test\java"
```
Next, I had to change the classpath to point to the current directory so that my class would be loaded and I had to change the classpath to point to he resources directory so my dll would be loaded.
```
Copyset classpath=%classpath%;.;..\..\..\src\main\resources;
```
Then, I had to run java using the classname.
```
Copyjava com.accessibility.HelloWorld
```
[Share](https://stackoverflow.com/a/18157831 "Short permalink to this answer")
Share a link to this answer
Copy link
[CC BY-SA 3.0](https://creativecommons.org/licenses/by-sa/3.0/ "The current license for this post: CC BY-SA 3.0")
[Improve this answer](https://stackoverflow.com/posts/18157831/edit)
Follow
Follow this answer to receive notifications
answered Aug 10, 2013 at 1:30
[](https://stackoverflow.com/users/2663340/scott-izu)
[Scott Izu](https://stackoverflow.com/users/2663340/scott-izu)
2,3192525 silver badges1414 bronze badges
## Comments
Add a comment
This answer is useful
0
Save this answer.
Show activity on this post.
First, have you compiled the class using the command line javac compiler? Second, it seems that your main method has an incorrect signature - it should be taking in an array of String objects, rather than just one:
```
Copypublic static void main(String[] args){
```
Once you've changed your code to take in an array of String objects, then you need to make sure that you're printing an element of the array, rather than array itself:
```
CopySystem.out.println(args[0])
```
If you want to print the whole list of command line arguments, you'd need to use a loop, e.g.
```
Copy
```
[Share](https://stackoverflow.com/a/1279608 "Short permalink to this answer")
Share a link to this answer
Copy link
[CC BY-SA 2.5](https://creativecommons.org/licenses/by-sa/2.5/ "The current license for this post: CC BY-SA 2.5")
[Improve this answer](https://stackoverflow.com/posts/1279608/edit)
Follow
Follow this answer to receive notifications
answered Aug 14, 2009 at 19:03
[](https://stackoverflow.com/users/149125/quanticle)
[quanticle](https://stackoverflow.com/users/149125/quanticle)
5,07066 gold badges3535 silver badges4242 bronze badges
## Comments
Add a comment
Start asking to get answers
Find the answer to your question by asking.
[Ask question](https://stackoverflow.com/questions/ask)
Explore related questions
- [java](https://stackoverflow.com/questions/tagged/java "show questions tagged 'java'")
See similar questions with these tags.
- The Overflow Blog
- [DeveloperWeek 2026: Making AI tools that are actually good](https://stackoverflow.blog/2026/03/05/developerweek-2026/?cb=1)
- [Building brains for bulldozers](https://stackoverflow.blog/2026/03/06/building-brains-for-bulldozers/?cb=1)
- Featured on Meta
- [Logo updates to Stack Overflow's visual identity](https://meta.stackexchange.com/questions/417394/logo-updates-to-stack-overflows-visual-identity?cb=1)
- [Policy: Generative AI (e.g., ChatGPT) is banned](https://meta.stackoverflow.com/questions/421831/policy-generative-ai-e-g-chatgpt-is-banned?cb=1)
- [New site design and philosophy for Stack Overflow: Starting February 24, 2026...](https://meta.stackoverflow.com/questions/438177/new-site-design-and-philosophy-for-stack-overflow-starting-february-24-2026-at?cb=1 "New site design and philosophy for Stack Overflow: Starting February 24, 2026 at beta.stackoverflow.com")
- [I’m Jody, the Chief Product and Technology Officer at Stack Overflow. Let’s...](https://meta.stackoverflow.com/questions/438369/i-m-jody-the-chief-product-and-technology-officer-at-stack-overflow-let-s-talk?cb=1 "I’m Jody, the Chief Product and Technology Officer at Stack Overflow. Let’s talk about the site redesign")
[Visit chat](https://chat.stackoverflow.com/)
Community activity
Last 1 hr
- Users online activity
5300 users online
- 9 questions
- 11 answers
- 30 comments
- 108 upvotes
Popular tags
[javascript](https://stackoverflow.com/questions/tagged/javascript)[python](https://stackoverflow.com/questions/tagged/python)[sql](https://stackoverflow.com/questions/tagged/sql)[ios](https://stackoverflow.com/questions/tagged/ios)[c\#](https://stackoverflow.com/questions/tagged/c)[php](https://stackoverflow.com/questions/tagged/php)
Popular unanswered question
[Changing the locality/language of a google maps widget on flutter without having to change the locality of the device itself](https://stackoverflow.com/questions/78883189)
[android](https://stackoverflow.com/questions/tagged/android)[flutter](https://stackoverflow.com/questions/tagged/flutter)[google-maps](https://stackoverflow.com/questions/tagged/google-maps)
[](https://stackoverflow.com/users/25666952)
[Raphael Goldstein](https://stackoverflow.com/users/25666952)
- 11
568 days ago
#### Linked
[0](https://stackoverflow.com/questions/60514067/my-command-javac-does-not-show-any-result?lq=1 "Question score (upvotes - downvotes)")
[My command javac does not show any result](https://stackoverflow.com/questions/60514067/my-command-javac-does-not-show-any-result?noredirect=1&lq=1)
[0](https://stackoverflow.com/questions/57963045/custom-command-for-command-line-for-invoking-java-class?lq=1 "Question score (upvotes - downvotes)")
[Custom Command for command line for invoking java class](https://stackoverflow.com/questions/57963045/custom-command-for-command-line-for-invoking-java-class?noredirect=1&lq=1)
[1](https://stackoverflow.com/questions/42247648/run-selenium-standalone-script-via-cmd?lq=1 "Question score (upvotes - downvotes)")
[Run selenium standalone script via cmd](https://stackoverflow.com/questions/42247648/run-selenium-standalone-script-via-cmd?noredirect=1&lq=1)
[1](https://stackoverflow.com/questions/41889405/how-to-remove-the-java-error-class-not-found-or-loaded?lq=1 "Question score (upvotes - downvotes)")
[How to remove the java error "Class not found or loaded"?](https://stackoverflow.com/questions/41889405/how-to-remove-the-java-error-class-not-found-or-loaded?noredirect=1&lq=1)
[\-5](https://stackoverflow.com/questions/36318222/finding-main-class-in-hello-world-program-in-java?lq=1 "Question score (upvotes - downvotes)")
[Finding Main Class in "Hello World" program in Java](https://stackoverflow.com/questions/36318222/finding-main-class-in-hello-world-program-in-java?noredirect=1&lq=1)
[44](https://stackoverflow.com/questions/19433366/running-java-in-package-from-command-line?lq=1 "Question score (upvotes - downvotes)")
[Running java in package from command line](https://stackoverflow.com/questions/19433366/running-java-in-package-from-command-line?noredirect=1&lq=1)
[7](https://stackoverflow.com/questions/52392430/how-do-i-run-a-main-method-in-a-java-gradle-project-from-command-line?lq=1 "Question score (upvotes - downvotes)")
[How do I run a main method in a Java Gradle project from command line?](https://stackoverflow.com/questions/52392430/how-do-i-run-a-main-method-in-a-java-gradle-project-from-command-line?noredirect=1&lq=1)
[4](https://stackoverflow.com/questions/2120942/running-java-program-from-python?lq=1 "Question score (upvotes - downvotes)")
[Running Java program from Python](https://stackoverflow.com/questions/2120942/running-java-program-from-python?noredirect=1&lq=1)
[2](https://stackoverflow.com/questions/7593320/error-in-java-basic-test-program?lq=1 "Question score (upvotes - downvotes)")
[error in java basic test program](https://stackoverflow.com/questions/7593320/error-in-java-basic-test-program?noredirect=1&lq=1)
[2](https://stackoverflow.com/questions/50678531/how-do-i-need-to-run-maven-file-in-powershell?lq=1 "Question score (upvotes - downvotes)")
[How do I need to run maven file in powershell?](https://stackoverflow.com/questions/50678531/how-do-i-need-to-run-maven-file-in-powershell?noredirect=1&lq=1)
[See more linked questions](https://stackoverflow.com/questions/linked/1279542?lq=1)
#### Related
[3](https://stackoverflow.com/questions/1774203/running-a-java-class-from-a-jar-thru-a-command-line?rq=3 "Question score (upvotes - downvotes)")
[Running a Java Class from a Jar thru a command line](https://stackoverflow.com/questions/1774203/running-a-java-class-from-a-jar-thru-a-command-line?rq=3)
[25](https://stackoverflow.com/questions/2864622/how-do-i-run-class-files-on-windows-from-command-line?rq=3 "Question score (upvotes - downvotes)")
[How do I run .class files on windows from command line?](https://stackoverflow.com/questions/2864622/how-do-i-run-class-files-on-windows-from-command-line?rq=3)
[1](https://stackoverflow.com/questions/4137877/run-class-file-using-cmd-commands?rq=3 "Question score (upvotes - downvotes)")
[Run class file using cmd commands](https://stackoverflow.com/questions/4137877/run-class-file-using-cmd-commands?rq=3)
[7](https://stackoverflow.com/questions/4225815/execute-java-from-command-line?rq=3 "Question score (upvotes - downvotes)")
[Execute Java from command line](https://stackoverflow.com/questions/4225815/execute-java-from-command-line?rq=3)
[214](https://stackoverflow.com/questions/6780678/run-class-in-jar-file?rq=3 "Question score (upvotes - downvotes)")
[Run class in Jar file](https://stackoverflow.com/questions/6780678/run-class-in-jar-file?rq=3)
[3](https://stackoverflow.com/questions/9234488/running-java-class-from-terminal?rq=3 "Question score (upvotes - downvotes)")
[Running java class from terminal](https://stackoverflow.com/questions/9234488/running-java-class-from-terminal?rq=3)
[1](https://stackoverflow.com/questions/16263859/running-a-java-class-file-using-unix-syntax?rq=3 "Question score (upvotes - downvotes)")
[Running a java class file using unix syntax?](https://stackoverflow.com/questions/16263859/running-a-java-class-file-using-unix-syntax?rq=3)
[0](https://stackoverflow.com/questions/21785626/executing-a-class-from-a-java-file-with-java-commands-in-it?rq=3 "Question score (upvotes - downvotes)")
[executing a .class from a java file with java commands in it](https://stackoverflow.com/questions/21785626/executing-a-class-from-a-java-file-with-java-commands-in-it?rq=3)
[0](https://stackoverflow.com/questions/43149444/execute-a-java-class-file-that-uses-class-from-a-jar-file?rq=3 "Question score (upvotes - downvotes)")
[Execute a java class file that uses class from a jar file](https://stackoverflow.com/questions/43149444/execute-a-java-class-file-that-uses-class-from-a-jar-file?rq=3)
[0](https://stackoverflow.com/questions/49887865/how-to-execute-a-java-main-class-from-the-command-line?rq=3 "Question score (upvotes - downvotes)")
[How to execute a java main class from the command line](https://stackoverflow.com/questions/49887865/how-to-execute-a-java-main-class-from-the-command-line?rq=3)
#### [Hot Network Questions](https://stackexchange.com/questions?tab=hot)
- [How can I transfer money from Wise account to bank account without fee?](https://money.stackexchange.com/questions/169366/how-can-i-transfer-money-from-wise-account-to-bank-account-without-fee)
- [青岛 (qīngdǎo) meaning?](https://chinese.stackexchange.com/questions/63968/%E9%9D%92%E5%B2%9B-q%C4%ABngd%C7%8Eo-meaning)
- [Would the following biconditional express an "intrinsically plural object"?](https://philosophy.stackexchange.com/questions/136859/would-the-following-biconditional-express-an-intrinsically-plural-object)
- [Integer factorization-nr138132535\_version2\_2](https://codereview.stackexchange.com/questions/301528/integer-factorization-nr138132535-version2-2)
- [Achashverosh was a slave?](https://judaism.stackexchange.com/questions/155429/achashverosh-was-a-slave)
- [Why is mass coupled to inertia but the other fundamental charges aren't?](https://physics.stackexchange.com/questions/869912/why-is-mass-coupled-to-inertia-but-the-other-fundamental-charges-arent)
- [What does “adultery” mean in the sense of “intruding upon a bishopric?”](https://english.stackexchange.com/questions/639143/what-does-adultery-mean-in-the-sense-of-intruding-upon-a-bishopric)
- [Is this normal behavior for child and grandparent](https://parenting.stackexchange.com/questions/46894/is-this-normal-behavior-for-child-and-grandparent)
- [Isotope composition of uranium slugs after they were ejected from the Hanford piles in 1945?](https://physics.stackexchange.com/questions/869857/isotope-composition-of-uranium-slugs-after-they-were-ejected-from-the-hanford-pi)
- [How to correct for LaTeX and/or package changes affecting parsing of text in TikZ decoration for TL2020 vs TL2021 vs TL2022/3 vs TL2024 vs TL2025/6?](https://tex.stackexchange.com/questions/760597/how-to-correct-for-latex-and-or-package-changes-affecting-parsing-of-text-in-tik)
- [An equation in the middle between several equations in cases or dcases](https://tex.stackexchange.com/questions/760586/an-equation-in-the-middle-between-several-equations-in-cases-or-dcases)
- [How do I load secrets from a \`.env\` file?](https://mathematica.stackexchange.com/questions/318952/how-do-i-load-secrets-from-a-env-file)
- [Why would an HSA company mandate a third party linking service for reimburements?](https://money.stackexchange.com/questions/169355/why-would-an-hsa-company-mandate-a-third-party-linking-service-for-reimburements)
- [What does the intransitive verb "scale" mean in business English?](https://ell.stackexchange.com/questions/374217/what-does-the-intransitive-verb-scale-mean-in-business-english)
- [Is photon budget a bad word?](https://physics.stackexchange.com/questions/869840/is-photon-budget-a-bad-word)
- [What are these "unusual" cable outer caps for?](https://bicycles.stackexchange.com/questions/100064/what-are-these-unusual-cable-outer-caps-for)
- [Is a dealer offering a rebate for financing a car a red flag?](https://money.stackexchange.com/questions/169352/is-a-dealer-offering-a-rebate-for-financing-a-car-a-red-flag)
- [How do I fix the leaky pressure valve?](https://gaming.stackexchange.com/questions/418103/how-do-i-fix-the-leaky-pressure-valve)
- [Lying tricep extension ROM](https://fitness.stackexchange.com/questions/48806/lying-tricep-extension-rom)
- [Ivrmi, Ernie, erh xli Geiwev Gshi Irmkqe](https://puzzling.stackexchange.com/questions/137346/ivrmi-ernie-erh-xli-geiwev-gshi-irmkqe)
- [1 Cor 8 : Why didn't St Paul impose a blanket ban on eating of food offered to idols?](https://hermeneutics.stackexchange.com/questions/115178/1-cor-8-why-didnt-st-paul-impose-a-blanket-ban-on-eating-of-food-offered-to-i)
- [What is an Employee Word™?](https://puzzling.stackexchange.com/questions/137333/what-is-an-employee-word)
- [Blender mysterious file size bloat from rig](https://blender.stackexchange.com/questions/345588/blender-mysterious-file-size-bloat-from-rig)
- [How to justify a large population practicing universal endocannibalism being healthy?](https://worldbuilding.stackexchange.com/questions/272837/how-to-justify-a-large-population-practicing-universal-endocannibalism-being-hea)
[Question feed](https://stackoverflow.com/feeds/question/1279542 "Feed of this question and its answers")
# Subscribe to RSS
Question feed
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

lang-java
# Why are you flagging this comment?
Probable spam.
This comment promotes a product, service or website while [failing to disclose the author's affiliation](https://stackoverflow.com/help/promotion).
Unfriendly or contains harassment/bigotry/abuse.
This comment is unkind, insulting or attacks another person or group. Learn more in our [Abusive behavior policy](https://stackoverflow.com/conduct/abusive-behavior).
Not needed.
This comment is not relevant to the post.
```
```
Enter at least 6 characters
Something else.
A problem not listed above. Try to be as specific as possible.
```
```
Enter at least 6 characters
Flag comment
Cancel
You have 0 flags left today
# 
# Hang on, you can't upvote just yet.
You'll need to complete a few actions and gain 15 reputation points before being able to upvote. **Upvoting** indicates when questions and answers are useful. [What's reputation and how do I get it?](https://stackoverflow.com/help/whats-reputation)
Instead, you can save this post to reference later.
Save this post for later
Not now
##### [Stack Overflow](https://stackoverflow.com/)
- [Questions](https://stackoverflow.com/questions)
- [Help](https://stackoverflow.com/help)
- [Chat](https://chat.stackoverflow.com/?tab=explore)
##### [Business](https://stackoverflow.co/)
- [Stack Internal](https://stackoverflow.co/internal/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=footer&utm_content=teams)
- [Stack Data Licensing](https://stackoverflow.co/data-licensing/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=footer&utm_content=data-licensing)
- [Stack Ads](https://stackoverflow.co/advertising/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=footer&utm_content=advertising)
##### [Company](https://stackoverflow.co/)
- [About](https://stackoverflow.co/)
- [Press](https://stackoverflow.co/company/press/)
- [Work Here](https://stackoverflow.co/company/work-here/)
- [Legal](https://stackoverflow.com/legal)
- [Privacy Policy](https://stackoverflow.com/legal/privacy-policy)
- [Terms of Service](https://stackoverflow.com/legal/terms-of-service/public)
- [Contact Us](https://stackoverflow.com/contact)
- Cookie Settings
- [Cookie Policy](https://policies.stackoverflow.co/stack-overflow/cookie-policy)
##### [Stack Exchange Network](https://stackexchange.com/)
- [Technology](https://stackexchange.com/sites#technology)
- [Culture & recreation](https://stackexchange.com/sites#culturerecreation)
- [Life & arts](https://stackexchange.com/sites#lifearts)
- [Science](https://stackexchange.com/sites#science)
- [Professional](https://stackexchange.com/sites#professional)
- [Business](https://stackexchange.com/sites#business)
- [API](https://api.stackexchange.com/)
- [Data](https://data.stackexchange.com/)
- [Blog](https://stackoverflow.blog/?blb=1)
- [Facebook](https://www.facebook.com/officialstackoverflow/)
- [Twitter](https://twitter.com/stackoverflow)
- [LinkedIn](https://linkedin.com/company/stack-overflow)
- [Instagram](https://www.instagram.com/thestackoverflow)
Site design / logo © 2026 Stack Exchange Inc; user contributions licensed under [CC BY-SA](https://stackoverflow.com/help/licensing) . rev 2026.3.6.40623 |
| Readable Markdown | This question shows research effort; it is useful and clear
146
Save this question.
Show activity on this post.
I have a compiled java class:
**Echo.class**
```
public class Echo {
public static void main (String arg) {
System.out.println(arg);
}
}
```
I `cd` to the directory and enter: `java Echo "hello"`
I get this error:
```
C:\Documents and Settings\joe\My Documents\projects\Misc\bin>java Echo "hello"
Exception in thread "main" java.lang.NoClassDefFoundError: Echo
Caused by: java.lang.ClassNotFoundException: Echo
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
Could not find the main class: Echo. Program will exit.
```
What is the simplest way to get my java code in a form that I can run from the command line as apposed to having to use Eclipse IDE?
[](https://stackoverflow.com/users/4720935/mel)
[Mel](https://stackoverflow.com/users/4720935/mel)
6,10510 gold badges40 silver badges42 bronze badges
asked Aug 14, 2009 at 18:52
[](https://stackoverflow.com/users/5653/joe)
1
This answer is useful
144
Save this answer.
Show activity on this post.
Try:
```
java -cp . Echo "hello"
```
***
Assuming that you compiled with:
```
javac Echo.java
```
Then there is a chance that the "current" directory is not in your classpath ( where java looks for .class definitions )
If that's the case and listing the contents of your dir displays:
```
Echo.java
Echo.class
```
Then any of this may work:
```
java -cp . Echo "hello"
```
or
```
SET CLASSPATH=%CLASSPATH;.
java Echo "hello"
```
And later as [Fredrik](https://stackoverflow.com/questions/1279542/how-to-execute-a-java-class-from-the-command-line/1279560#1279560) points out you'll get another error message like.
> *Exception in thread "main" java.lang.NoSuchMethodError: main*
When that happens, go and read his answer :)
[](https://stackoverflow.com/users/-1/community)
answered Aug 14, 2009 at 18:53
[](https://stackoverflow.com/users/20654/oscarryz)
1 Comment
[](https://stackoverflow.com/users/59572/fredrik)
The -cp . to tell Java that he should add . (current working directory) to the classpath, meaning the place where he is looking for code.
2009-08-14T19:24:39.143Z+00:00
This answer is useful
53
Save this answer.
Show activity on this post.
With Java 11 you won't have to go through this rigmarole anymore\!
Instead, you can do this:
```
> java MyApp.java
```
You don't have to compile beforehand, as it's all done in one step.
You can get the Java 11 JDK here: [JDK 11 GA Release](https://jdk.java.net/11/)
answered Oct 4, 2018 at 6:12
[](https://stackoverflow.com/users/5486/ryan-lundy)
[Ryan Lundy](https://stackoverflow.com/users/5486/ryan-lundy)
211k41 gold badges187 silver badges216 bronze badges
1 Comment
[](https://stackoverflow.com/users/929263/jr)
\> copy C:\\Desenv\\workspaceServer\\TestProject\\src\\WebServiceStandAlone\\MyApp.java . /y && javac MyApp.java && java -cp . MyApp && del MyApp.class && del MyApp.java Some guys do prefer the old fashion way =)
2018-11-13T20:15:04.063Z+00:00
This answer is useful
21
Save this answer.
Show activity on this post.
You need to specify the classpath. This should do it:
```
java -cp . Echo "hello"
```
This tells java to use `.` (the current directory) as its classpath, i.e. the place where it looks for classes. Note than when you use packages, the classpath has to contain the root directory, not the package subdirectories. e.g. if your class is `my.package.Echo` and the .class file is `bin/my/package/Echo.class`, the correct classpath directory is `bin`.
answered Aug 14, 2009 at 18:54
[](https://stackoverflow.com/users/16883/michael-borgwardt)
Comments
This answer is useful
21
Save this answer.
Show activity on this post.
You have no valid main method... The signature should be: public static void main(**String\[\]** args);
Hence, in your case the code should look like this:
```
public class Echo {
public static void main (String[] arg) {
System.out.println(arg[0]);
}
}
```
**Edit:** Please note that [Oscar](https://stackoverflow.com/questions/1279542/how-to-execute-a-java-class-from-the-command-line/1279556#1279556) is also right in that you are missing . in your classpath, you would run into the problem I solve after you have dealt with that error.
[](https://stackoverflow.com/users/-1/community)
answered Aug 14, 2009 at 18:54
[](https://stackoverflow.com/users/59572/fredrik)
[Fredrik](https://stackoverflow.com/users/59572/fredrik)
5,8592 gold badges29 silver badges33 bronze badges
2 Comments
[](https://stackoverflow.com/users/4952917/erich-k%C3%BCster)
The only one to give the right result with newer java's.
2023-09-03T12:42:17.977Z+00:00
[](https://stackoverflow.com/users/59572/fredrik)
Keep in mind this thread dates back to 2009 :-)
2023-09-08T07:30:46.43Z+00:00
This answer is useful
8
Save this answer.
Show activity on this post.
If you have in your java source
```
package mypackage;
```
and your class is hello.java with
```
public class hello {
```
and in that hello.java you have
```
public static void main(String[] args) {
```
Then (after compilation) changeDir (cd) to the directory where your hello.class is. Then
```
java -cp . mypackage.hello
```
Mind the current directory and the package name before the class name. It works for my on linux mint and i hope on the other os's also
Thanks Stack overflow for a wealth of info.
answered Mar 17, 2018 at 8:22
[](https://stackoverflow.com/users/8344297/gerard-doets)
1 Comment
[](https://stackoverflow.com/users/1300170/ruvim)
This answer is useful
3
Save this answer.
Show activity on this post.
My situation was a little complicated. I had to do three steps since I was using a .dll in the resources directory, for JNI code. My files were
```
S:\Accessibility\tools\src\main\resources\dlls\HelloWorld.dll
S:\Accessibility\tools\src\test\java\com\accessibility\HelloWorld.class
```
My code contained the following line
```
System.load(HelloWorld.class.getResource("/dlls/HelloWorld.dll").getPath());
```
First, I had to move to the classpath directory
```
cd /D "S:\Accessibility\tools\src\test\java"
```
Next, I had to change the classpath to point to the current directory so that my class would be loaded and I had to change the classpath to point to he resources directory so my dll would be loaded.
```
set classpath=%classpath%;.;..\..\..\src\main\resources;
```
Then, I had to run java using the classname.
```
java com.accessibility.HelloWorld
```
answered Aug 10, 2013 at 1:30
[](https://stackoverflow.com/users/2663340/scott-izu)
[Scott Izu](https://stackoverflow.com/users/2663340/scott-izu)
2,31925 silver badges14 bronze badges
Comments
This answer is useful
0
Save this answer.
Show activity on this post.
First, have you compiled the class using the command line javac compiler? Second, it seems that your main method has an incorrect signature - it should be taking in an array of String objects, rather than just one:
```
public static void main(String[] args){
```
Once you've changed your code to take in an array of String objects, then you need to make sure that you're printing an element of the array, rather than array itself:
```
System.out.println(args[0])
```
If you want to print the whole list of command line arguments, you'd need to use a loop, e.g.
```
for(int i = 0; i < args.length; i++){
System.out.print(args[i]);
}
System.out.println();
```
answered Aug 14, 2009 at 19:03
[](https://stackoverflow.com/users/149125/quanticle)
[quanticle](https://stackoverflow.com/users/149125/quanticle)
5,0706 gold badges35 silver badges42 bronze badges
Comments
Start asking to get answers
Find the answer to your question by asking.
[Ask question](https://stackoverflow.com/questions/ask)
Explore related questions
See similar questions with these tags. |
| Shard | 169 (laksa) |
| Root Hash | 714406497480128969 |
| Unparsed URL | com,stackoverflow!/questions/1279542/how-to-execute-a-java-class-from-the-command-line s443 |