How do I run my Java program in command prompt, my project was created in Intellij, and I am having difficulties running it in the command prompt...without using the Intelli
It looks like the class is in a package com.myexample.test
. Try running
java com.myexample.test.myjava
from the project's bin
directory
Three issues:
You have to specify the fully qualified class name (that means including the package name) to the java
command. It looks like your myjava
class is in a package com.myexample.test
. So its fully qualified name is com.myexample.test.myjava
.
When you run the java
command you have to be in the directory that is at the base of the package hierarchy (or put that directory on the classpath).
You're using the src
directory, which contains .java
source files, but the java
command expects compiled .class
files, so you need to use the project's output directory. Its location in your project will depend on your IDE and configuration but it will contain same-named structure as inside src
, except with .class
files instead of .java
files.
In your case, navigate to:
C:\myjava\sampl1\out\production\
Then run:
java com.myexample.test.myjava
If you were using a Maven project for example, Just locate the relevant .class
file under /target
folder. Then execute it using 'java' command like you would typically run any application.
I hope this can help somebody, a little late but, just I had this problem, ok my solution it next: 1. Run your code normally and copy the command line that the IntellijIDEA made, see the screenshot.
2.Copy and paste the command line that it uses to use with your params.
In my terminal the current directory is the root project directory named Practice.
When you issue the java command make sure to specify the classpath using the -cp flag.
The classpath will extend from your project root directory through out/production/<project name>
.
For example, my classpath was: -cp /Users/ashton/java/IdeaProjects/Practice/out/production/Practice
Then you add a space, then the package name including class name you want to run, then any parameters. So the entire command including parameters was this:
java -cp /Users/ashton/java/IdeaProjects/Practice/out/production/Practice us.debie.ian.ljbe.FileSearchApp one two three
You should be able to piece it all together by examining the screenshot below to see what current directory is in, what classpath was specified, etc.
From intellij, open your project structure, and click on Modules. You'll be able to see the output path. Usually, it's something like (if on mac):
~/../out/production/{context}.
Back to your example in windows. The path you gave is, C:\myjava\sampl1\src\com\myexample\test>, so you have your project in :
C:\myjava
directory where your context (or project name) is sampl1
.
That means that your output (*.class files) will be in
C:\myjava\sampl1\out\production\sampl1
Navigate into the folder and then run the command:
java com.myexample.test.myjava