0

Say I setup a program in intelliJ in java and I have multiple classes and everything setup. I am trying to run this program from the command line, but I keep seeing in tutorials they are using the command javac programname.java and they are compiling the program and then running it.

But I have multiple classes and I'm using intelliJ to do everything. Do I just compile it from intellij or what's the best way to do this? Do I even need to compile it, or is it all ready compiled?

I just want to be able to run my main class from the command line.

king
  • 1,304
  • 3
  • 23
  • 43

3 Answers3

0

You do not need to compile it. Just run your main class and IDE will do the rest.

Do remember to add required parameters to you main program, if any.

This link would be helpful https://www.jetbrains.com/idea/help/running-applications.html

Nimble Fungus
  • 513
  • 3
  • 22
  • I don't want to run it using the IDE. I want to put it somewhere else and have it run seperate from the IDE completely, with the IDE not running – king Dec 04 '15 at 18:29
  • @simon There are two ways for this 1. Make an executable jar, which i guess is difficult for you. 2. Put the required classes, in another folder and first compile and then execute. If you already have compiled classes u can just execute the main method. This would be helpful for you http://tutorials.jenkov.com/java/main-method.html – Nimble Fungus Dec 04 '15 at 19:58
  • nothing is difficult for me to do, i can do whatever i need to do, i just don't know what i'm supposed to do? where can i make an executable jar inside intelliJ, is it possible? i don't see anything anywhere for how to do it...... – king Dec 04 '15 at 20:04
  • to do it in intellij.. it says go to build and build,artifact, but it is greyed out for me. what is the proper way to do this? are there instructions anywhere that explains if you have a full project made, how to run it from the command line? not the basic helloworld stuff, something with multiple classes, not just one file to compile? – king Dec 04 '15 at 20:05
  • 2
    @simon its already answered here http://stackoverflow.com/questions/2025607/how-to-create-a-jar-file-or-export-jar-on-intellij-like-eclipse-java-archive-e AND here http://stackoverflow.com/questions/1082580/how-to-build-jars-from-intellij-properly – Nimble Fungus Dec 04 '15 at 20:06
  • @simon If its a big project and not a Hello world type then either use Maven or Ant to Buid the jar file. use this http://stackoverflow.com/questions/9874550/how-to-create-a-bundled-runnable-jar-using-ant – Nimble Fungus Dec 04 '15 at 20:10
  • got it working.. another person linked to their blog.. i didnt select a main method with it.. thanks for the help – king Dec 04 '15 at 20:16
  • 1
    @simon Glad i was of some help..!! – Nimble Fungus Dec 04 '15 at 20:20
0

You should think of the main method() as the entry point in your program. In other words, main() is the method that starts your program. So, when you are adding other classes intellij just adds imports to your other classes just like when your importing from the api. So, assuming that your imports are correct you need to take the following steps.

  1. Compile all of your .java files (including the file with main which is sometimes referred to as a test client) using javac myFile.java

  2. Run your compiled, main class with main method() by using java myClass

Edit: you must ensure that you add the location of your .class file to your classpath. So, if its in the current folder then add . to your classpath. Note that the windows classpath separator is a semi-colon ie ;

Then you can use java -cp to compile and run

javac -cp . PackageName/*.java
java -cp . PackageName/ClassName_Having_main
Michael Queue
  • 1,340
  • 3
  • 23
  • 38
  • i don't see a .java file anywhere in my directory structures for the file, the three classes that are used are in .class files – king Dec 04 '15 at 18:39
  • ok so .class files are just java classes that have been compiled down into bytecode. So your files are already compiled. Just use `java myFile` to run. – Michael Queue Dec 04 '15 at 18:40
  • it says could not find or laod main class. i'm looking at this to try to figure out the right way to type it. http://stackoverflow.com/questions/18093928/what-does-could-not-find-or-load-main-class-mean – king Dec 04 '15 at 18:41
  • Don't use `java myFile.class` just use `java myFile` – Michael Queue Dec 04 '15 at 18:45
  • doesnt work... is it supposed to be out of the top directory? – king Dec 04 '15 at 18:46
  • if you built this in intellij then there is going to be a package hierarchy. Try taking a look at this answer. https://stackoverflow.com/questions/20365885/how-do-i-run-java-program-with-multiple-classes-from-cmd – Michael Queue Dec 04 '15 at 18:48
  • yeah it definetely has a hirearchy. i have a com folder, then a bunch of folders underneath it. i have a classes folder, then com, then a bunch of others, then it gets to the classes. how do you know where to start at? – king Dec 04 '15 at 19:30
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/97011/discussion-between-michael-queue-and-simon). – Michael Queue Dec 04 '15 at 19:34
  • can you tell me here, where are you supposed to run the java command from? from the highest folder in the structure or where? i have tried the highest structure and i've tried the where the file is located and neither works, and i add the path too like.. "java com.whatever.whatever.MainFile" – king Dec 04 '15 at 19:44
0

You can make an executable jar :

More information : http://www.mkyong.com/java/how-to-make-an-executable-jar-file/

Intellij : How to build jars from IntelliJ properly?

Community
  • 1
  • 1
Fundhor
  • 3,369
  • 1
  • 25
  • 47