Running a Java program created in IntelliJ on Windows Command Line - java

I have seen many people ask this question but none of the answers have solved my problem.
I have written a Java project in Intellij which runs fine in Intellij. I need it to run on the command line however.
Intellij automatically builds the .class files in the following directories which is outlined as follows:
C:\git\myProject\out\production\myProject\main
C:\git\myProject\out\production\myProject\openNlpTools
C:\git\myProject\out\production\myProject\sentimentAnalysisTools
I have three sub directories here. The file with the main method is in the \main folder.
To run the program I thought I used the following:
java main.FileMonitor
When I run this however I get the following error:
Error: Could not find or load main class main.FileMonitor
Does anyone have any tips as to why this isn't running?
Intellij runs the program fine, is there anyway I can see the command IntelliJ uses to run the program?
Thanks!

If you can't file a class it usually means you didn't include it in your class path. Some ways to run in the command line
Copy from IntelliJ
Run the program in InetlliJ.
Copy the first line as this will contain full command line
Edit the command line to remove bits you don't need.
Using Maven/Gradle
Set the Main-Class in the assembly plugin
build the program with assembly:single
this will create a Jar you can run on the command line.

Related

Is It necessary to compile entire program from an IDE like IntelliJ to run from command line?

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.
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
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.
Compile all of your .java files (including the file with main which is sometimes referred to as a test client) using javac myFile.java
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
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?

Executing .jar file from command line cannot find the configuration file in the root folder

I wrote an application which starts by reading a configuration (.ini) file and accordingly parses some xml files within a particular folder.
Everything works perfectly when I build and run my application in NetBeans. Now I wanted to run my application through command line (because that's actual requirement of my project), so I am executing the .jar file created by Netbeans to run my application.
I am executing the application as such: java -jar Application.jar.
The issue is when I run this command on command line, the application seems to not be able to find the configuration file which is in the root folder of the project. Its really odd because NetBeans runs it perfectly.
I believe the reason I am getting this exception is because to run the .jar file I am changing my directory to dist and then running the java -jar command and therefore the file is not being found . But then how do I get around this.
Any help is appreciated. Thank you
Sorry got it figured out. I am executing jar file from the root but adding the source in the command as well.
Sorry, you can close this thread.
I had this problem till I installed JRE and did NOT use stand alone.

Executing Eclipse Selenium Webdriver Java Code in Command Line?

I'm using Windows Server 2003,and I'm building a webpage and I want a particular testcase(a Java code) to execute when I click a button in that webpage(the webpage is in same server). SO first I have to find a way to run this eclipse java codes in command line.
On doing some research I found I need to install Maven or Ant. But I know this is not really required. I'm not sure which way to go. I prefer not using Maven or Ant. Can someone please tell me the steps to execute These tests in Command line. ALso the environment variables to add Because I'm unable to find QTjava.zip file in jre7 /lib/ext/ folder.
Have a look at the javac and java command line options. Heres what I did to run selenium tests without requiring eclipse or maven/ant.
this compiles your class, with the jar file (i put it in the same directory, but ideally you will put the full path in the qoutemarks:
javac -classpath "selenium-server-standalone-2.33.0.jar" Example.java
Then you can run the generated class via:
java -classpath ".;selenium-server-standalone-2.33.0.jar" Example
Hope that helps.

Creating build of a Java project

I have a stand alone project in my eclipse.There are few external jar as well.The programme is executing well and giving required result. Now i need it to run from command prompt.and i just want to run the class file generated by eclipse. how i will be able to run it.When i am tring to run it from commend prompt it is giving class not found exception.I also tried to make an executable jar,but that is also not running.do i need to create executable jar or i can simply run it from command prompt..tell me the way out..
thanks
koushik
A step by step guide at
I recommend you to go through jar, class path and other basic building blocks of java for eg check this about jar
try using this command java -cp . package.name.className

What will netbeans IDE do when I press Ctrl+F6 to run a java file?

I have no idea of how Netbeans IDE run a java file.
Firstly, it would ensure the .class file is up-to-date.
Then, execute the class. But from where (working directory) and with which command (parameter)?
I observe difference on how relative path is located when I run the java file from Netbeans IDE and when I run using Windows command prompt (i.e > java pack.age.name.ClassName)
You can find that out by putting this at the start of the main method of the class:
System.out.println(new File(".").getAbsolutePath());
It looks like it will run from the directory that the project is in (eg. ....\NetBeansProjects\JavaApplication1)
You cannot specify command line arguments for a single class (that I am aware of). To do that you have to use the Project | Properties (and there you can also set the working directory).
I would suggest that you do not write code that depends on the working directory if you can avoid it though...
You can get informative results by running the project's build.xml from the command line in verbose mode: ant -verbose run. Look for the [java] command options under run:. Typing ant -p will show you the available targets.

Categories

Resources