35

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 Intellij in creating project,I can run the java program in command prompt.

I do this like this.

java myjava ->this would work.

but the project created by Intellij,this is the path.

C:\myjava\sampl1\src\com\myexample\test>

when I issue this command

java myjava -> Error: Could not find or load main class myjava

but I am inside in that directory.

Thank you in advance.

2
  • 1
    You are in src directory. go into the bin directory and do it, or else first do javac which will create a class file, and then run it via java command. Commented Nov 24, 2014 at 16:01
  • Its the out folder. Check that out. The class files are written there. If they don't exist for some reason, just build your project and you'll find them. Then run the file with the java command, as shown in the answers. Commented Nov 24, 2014 at 16:41

6 Answers 6

36

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.

IntellijIDEA program running to copy the command line

Copying the command line

2.Copy and paste the command line that it uses to use with your params.

Adding my own param and that's all.

Sign up to request clarification or add additional context in comments.

4 Comments

That's what the intelliJ forums/docs say too, but that command is not echoed in the run window anywhere I can see in IntelliJ 2018.2 on Mac
Thanks. Solved mine too. The problem is the ClassPath: it works when I use only the "C:\Program Files\Java\jdk1.8.0_192\bin\java.exe" and ALL the -classpath "C..."; do not need to use the -javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2018.3.4\lib\idea_rt.jar=56936:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2018.3.4\bin" nor the -Dfile.encoding=UTF-8
So if you are not able to see the full application path is because the app is running in the background while the logs are been sent to the Run panel. In order to see the command you can go to: IntelliJ Preferences | Build, Execution, Deployment | Build Tools | Maven | Runner there you need to uncheck the option Run in background. Next time you run your project you will be able to see the command line. I recommend turning it back after.
see my answer if you don't want to have to mess around with copying 9 lines every time you run it
29

Three issues:

  1. 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.

  2. 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).

  3. 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

6 Comments

bin or out ?,I did not see bin folder
Then use out. I do not know what IntelliJ-IDEA and/or you have configured. But it will contain .class files.
Where is the myjava.class file?
C:\myjava\sampl1\out\production\com\myexample\test\mjava.class
Then the directory to go to is C:\myjava\sampl1\out\production\. Run the java command from there.
|
2

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

3 Comments

bin or out ?,I did not see bin folder
whereever the class files are being written
My class files are scattered deep below the out dir. There is no bin. They work from IntelliJ but not the command prompt- from subdirs of out\production dir, which is the only place .class's appear. I think I need to setup from scratch.
1

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

Comments

0

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.

intellij screenshot

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.