33

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.

kgui
  • 4,015
  • 5
  • 41
  • 53
ashTon
  • 1,101
  • 4
  • 14
  • 23
  • 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. – Aman Agnihotri Nov 24 '14 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. – Aman Agnihotri Nov 24 '14 at 16:41

6 Answers6

32

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.

Oscar Albert
  • 803
  • 8
  • 13
  • 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 – yokeho Jan 15 '19 at 19:56
  • 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 – Marcelo Scofano Diniz Mar 23 '19 at 15:24
  • 1
    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. – Felipe Plets Jan 27 '20 at 22:32
  • see my answer if you don't want to have to mess around with copying 9 lines every time you run it – luckyguy73 Sep 17 '20 at 22:25
28

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
Boann
  • 48,794
  • 16
  • 117
  • 146
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

Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • bin or out ?,I did not see bin folder – ashTon Nov 24 '14 at 16:14
  • whereever the class files are being written – Reimeus Nov 24 '14 at 16:14
  • 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. – John Jun 17 '15 at 22:12
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

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

luckyguy73
  • 1,850
  • 2
  • 11
  • 21
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.

Dammike
  • 21
  • 3