Running java application written using Eclipse from command prompt - java

I have a java application that I created using Eclipse 3.7 and also run from this IDE.
Now I want to run it from command prompt. How should I go for it?
Do i have to create jar file to do this ?
And I am also setting some system properties in run configurations, so also specify how to pass them while running it frm command prompt.

There are many tutorial. here is the general process.
Compile all classes, you may use Eclipse or javac
You do not need to Jar it up. Go to your package root. And run it as
java -cp .:path/to/requires/jars:/some/more/jar FullyQualifiedMainClassNameWithoutExtension param1 param2
If you have properties to be feed into you may have that at package root and use ClassLoader#getResourceAsStream("/filename.properties")
If you look into args of your main(String[] args) you will find param1, param2 there.
Refer
http://www.cs.usfca.edu/~parrt/course/601/lectures/java.tools.html
http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/ClassLoader.html
Note:
If you have multiple Jars use wldcard to include your Jars. http://docs.oracle.com/javase/6/docs/technotes/tools/windows/classpath.html
the classpath delimiter is : for Unix, and ; for Windows
This tutorial is the best example of spoon feeding. But, whatever makes you go Compile and run Java program in package from command line

Switch to the "debug" perspective and run your program in Eclipse.
Write-click the process (even after it terminates) in the "debug" window and select "properties".
In the properties dialog you see the entire command line used by eclipse. You can copy and paste it in a shell to execute it.
You might want to change the command from "javaw" to "java". Up to you.

Related

How to call a main class in a jar package from eclipse?

Currently, there is bat file that calls the main class in a jar file. Now I want to run it using eclipse. How do I configure the eclipse to run it?
I have tried the Run > External tools > External tools configuration. But I don't know what to type in...
#echo off
set MODULE2_HOME=%~dp0..
set JAVA_HOME=C:/Program Files/Java/jdk1.6.0_71
set CLASSPATH="%MODULE2_HOME%/classes;%MODULE2_HOME%/lib/*;%MODULE2_HOME%/lib/oracle/*;%MODULE2_HOME%/lib/aspose/*;%MODULE_HOME%/aspose/*"
set SETUP_PROPERTIES="%MODULE2_HOME%\conf\setup.properties"
set PATH=%JAVA_HOME%\bin;%PATH%
java -cp %CLASSPATH% -Dsetup.properties=%SETUP_PROPERTIES% com.module.fast.main.Module2Main %*
How do I configure eclipse to run exactly like this command?
Since it is a batch file which will run in Windows. You can go to the command prompt and run the batch file using .bat. You do not need Eclipse. You can run the java class in eclipse. Still if you want to run, you can check the following screenshot.
While configuring, make sure to give the complete path of batch file. If the bat file needs extra commandline params, you can give the batch file location with space and the command line parameters separated with spaces.
It does not work to run the jar file which contains main class, you have to give the working directory location.

Compile project and include libraries using batch script

In CMD I compile project with including libraries
java -cp app.jar;libs/*;. com.app.Main
and it works, but I want create BATCH script, which do exactly the same. I create test.bat and put code like below:
#ECHO off
java -cp app.jar;libs/*;. com.app.Main
PAUSE
But when I run the test.bat the CMD was shown and there is information "Error: Could not find or load main class com.app.Main".
BATCH script is located at the same folder as app.jar and libs folder.
What is wrong with this batch script?
Probably the characters on the compile line are significant to the batch interpreter; try putting the classpath in double quotes.
After Java 6, Classpaths could be built by using wildcard characters.
You can create a directory named classpath and put your JARs inside it. Then you can create your .bat file like this:
#ECHO off
java -cp .;classpath/* com.app.Main
pause
You should have a structure like this:
com
`---app
`---Main.java
classpath
`---your-crital-code-1.0.jar
compile.bat
I see that a lot of people are asking this question on StackOverflow, so here is a little tip for you guys.
I usually avoid using the cd command, as it may create some hassle. In windows, you can Shift + Right Click to open a command window in a particular directory.
I always prefer relative paths over absolutes, so that I don't get into hassle of managing long paths.
Here is a little program that deals with this kind of problem. You can always refer to and contribute to it so that we can make good examples for Java beginners :)

Error when executing class file in command prompt

I'm currently in the process of learning more about Java. I downloaded a sample program that has about 5 different .java files in the source folder.
Whenever I try to execute the class files in the command prompt using "java ClassNameHere", I get a message in the cmd prompt that says "Error: Could not find or load main class ClassNameHere".
I've tried recompiling them using the javac command and they compiles without any issues. Its just the class file i'm having trouble with.
In addition to this, I also cannot run the java files using Eclipse. Instead of getting an option to "Run as Java Application", "non applicable" appears under the "Run as" tab in Eclipse.
I've tried a number of different solutions already posted online, including another command that uses "java -cp . ClassNameHere" or something along those lines and they have not worked for me. Any help is appreciated.
Edit
Link to program from my drive since I'm not home
https://docs.google.com/file/d/0B9_pcTVZTnfEeVdzZmM0ZmFkTmc/edit?usp=docslist_api
Here is a a procedure that will get the application working in Eclipse. This procedure assumes the following:
Windows 8. I don't think Linux/Mac OS X procedure would differ greatly, but just in case...
In Eclipse:
Right click in Package Explorer, and select New --> Java Project.
In the New Java Project dialog, uncheck Use default location, and click Browse and select the directory you've extracted the zip to, or type the path in the box.
Click Finish.
From this point, you can run this is a Java Application from Eclipse.
Confirm that at least one of the classes has a main() method. That would be required for Eclipse to show "Run as Java Application".
Follow this steps:
Run Command Prompt.
Use cd C:\path\to\your\porject\java classes.
Use set path=%path%;C:\Program Files\Java\jdk1.7.0_65\bin (CHANGE THE PATH TO YOUR jdk1.x.x_xx\bin FOLDER) to tell the system where to find jdk programs.
Use javac MainClass.java to compile the project, MainClass.java si the java class where the main method is.
Use java MainClass (WITHOUT .java EXTENSION) to run the project.
After the last command your project should run correctly.
Sorry for the "use" ripetition but i'm not english.

Java - Eclipse packages

I have the following package on Eclipse:
com.mortgageapp.projects.app
I'm not interested about the package format at the moment, it's just testing. But I'm wondering how to run the app from the terminal (Windows and Mac)?
It contains a Main.java file where it will begin so I have tried locating and entering the src folder. Then doing something like: javac com/mortgageapp/projects/app/Main.java (or: javac com/mortgageapp/projects/app/*.java).
Just wondering if this is current as when I then do: java com/mortgageapp/projects/app/Main I get a few errors.
Your compilation is probably okay, but to run it you need to specify the class name, not a filename:
java com.mortgageapp.projects.app.Main
That's assuming the current directory is in the classpath. If it's not, you may need:
java -cp . com.mortgageapp.projects.app.Main
I assume you are writing an RCP application.
Export the project as a RCP application (click the "export" link in your product configuration). You should get a runnable application file.
If you really want to run the application from the terminal, you need to use the main function in the EclipseStarter. But you don't want to go there unless you are doing something very special.

no output when .jar is executed

I built an application in Netbeans 6.8 and made project.jar file. When I run it, it works only on my computer, but not on any other computer. However, when I made any simple application, that doesnt use any libraries, it works fine on any computer.
Is there any way, how to invoke some error message, where is the problem?
My project use R 2.9.2, so I install this version on other computer and set the System Path variable exactly same. Other libraries listed in lib directory are: AbsoluteLayout.jar,DatePicker-V0.99-2006.09.01.jar,jcommon-1.0.16.jar,jfreechart-1.0.13.jar,jmathplot.jar,JRI.jar,pdf-renderer-1.0.5.jar
Thank you
You don't get any message at all? What do "works" and "not works" look like?
You sound like another person who hasn't taken the time to learn how to do things by hand on the command line without an IDE. I'd recommend doing that. Open a command shell and type in the java -jar -cp ... foo.jar command to run your stuff. The messages you get back will be educational.
Note the -cp command line argument. That's how you add your JARs to the CLASSPATH properly.
I solved this problem as follows, maybe it will help someone.I add 2 paths in PATH system variable:
Start -> Control Panel -> System -> Advanced
Click on Environment Variables, under System Variables, find PATH, and click on it.
In the Edit windows, modify PATH by adding the location of the class to the value for PATH.
you must add both paths, to jri.dll and r.dll, in my case it were these:
C:/Program Files/R/R-2.9.2/bin/;C:/Program Files/R/R-2.9.2/library/rJava/jri/;
I have added these lines already, but with different different slash. So be careful, you must use it / not \ to define path!!!

Categories