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

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.

Related

Unable to run JAR file outside Intellij

I was able to run the JAR file inside IntelliJ when I do Shift + f10.
However when trying to execute the JAR file from my directory, nothing happens. My META-INF is place as followed here: https://stackoverflow.com/a/49147689/12973878.
This is my JAR structure
JAR structure
File structure Image
Image
Can I know what is the problem here?
Based on the description, I assume you're in a windows environment. Try to run it in terminal. Change the current directory to the location of the file and the terminal command you need to give is:
java -jar <jarfilename>
Some suggestions:
Use the -verbose:class option when starting the java application to determine if classes are loaded from the correct places.
See if there's additional settings in the run configuration of IntelliJ. You'll find it in the upper right corner of the editor window next to the executuin button - dropdown will show 'Edit Configurations'.
If there's really no error at all - are you sure you have the correct starting Class as entry point? You might simply be executing the wrong main method from terminal while
IntelliJ picks the correct one (look for "main class" in run configuration).
After running a build for the project (or a mvn package) your jar is placed in <your_project_path>/target.
There you will find: <your_project_name>-0.0.1-SNAPSHOT.jar. Now just open command prompt (on win) / terminal (on mac).
Go to the project path
cd <your_project_path>/target
and run
java -jar <your_project_name>-0.0.1-SNAPSHOT.jar
Don't forget to check that you have java path in your system $PATH
run to verify
java -version

Batch file not executing the required commands in windows 8.1

I have created a batch file to set the path of a jar file whenever i need to use that jar file.But even after executing the file,the system is unable to recognize the jar file and when i compile my java program which uses that jar file,it gives compile time error(i.e the path is not set).
And when i simply use the classpath command in command prompt which i wrote inside the batch file,it works.
But i want to make a batch file so that whenever i need to set classpath,i can use that batch file.
Help will be appreciated.
Following is the batch file.
set classpath=jsoup.jar;.;%classpath%
Solution found by OP:
Thanks,I solved the problem.I just made a .cmd file containing the command.Now whenever i will need to set the path i will run that cmd file in command prompt.

Unable to run jar file from command line

I have an excel spreadsheet which launches several jar files from the command line. For some reason one of my jars has failed to run from the command line, yet when I double click on the executable, it runs fine.
I need the sheet to be able to run it from the command line. What could the issue be?
There are certain steps you need to perform to run jar through command line without specifying main class.
Add following line to your manifest file , while creating jar.
Main-Class : mypackage.myclass.yourclassnamehere
Here is the link [ http://www.mkyong.com/java/how-to-make-an-executable-jar-file/ ] for sample tutorial.
If you want to run from excel file, add java.exe as default program for jar extension executable. (In windows you can do it from control panel or right click on executable and select java.exe from other programs and ticking option as default program for .jar extension)

Creation of a .bat file of a java application

I am a bit confused about the process to create a .bat file of a java application. I have exported the executable jar using IDE say Application.jar in C: directory. Then I have written two lines in a .txt file as stated below and saved it as .bat file in the same directory where i have my application.jar. But on double click of the .bat file, the application is not getting executed.
.BAT file code
javac Application.java
java -cp . Application
Note: I have also set the JRE and JDK path in my environment variables till bin path in My Computer properties. But it is not working. Can someone suggest me how can I fix this, because I want to execute my code by doubleclickng on a .bat file. It will be nice if someone can provide me every step I need to follow to accomplish this as I havent ever done this before.
Thanks ,
The first line in your batch file is attempting to compile your program !?
The second line is attempting to run the Application.class file.
What you want if you have produced an executable jar file is:
java -jar Application.jar
But you don't really need the batch file at all. If you double click on the jar file and it runs your program then you can just create a shortcut to it.
Your .bat is just fine. When you double click it might be executing and then closes. This is because your program might not have any UI and it isnt waiting for any input. To verify this take a command prompt and then execute your bat file via that.
In other case I assume that you have a java class called Application and you need to run this via a batch file. In that case if the class have a main method then you just need one line in .bat file
java -cp <the path to class file> Application
So you might be using a javac just to take advantage of class path as current directory. So when you say
javac Application.java
java -cp . Application
It compiles the class to current folder and set that as class path and then execute. This is absolutely file as long as the Application.java doesnt have any third party dependency. But in this case again you need not set -cp to . (current directory will be taken as classpath automatically unless otherwise specificed). So below will also work fine.
javac Application.java
java Application
I support Jurgen reply. If you have an executable jar file and a jre in path then double clicking it will run the application. The META-INF folder inside the jar will have a MANIFEST.MF file which uses a property called Main-Class: to specify the main executing class. And on double clicking this class gets executed. However its only useful if you have a UI. Else it'll also have no effect.
In all these context the Application.jar you mentioned is irrelevant. If that is a third party jar that you need to run the you should include that in -cp argument.

How to refer to a properties file in a batch file

I'm trying to run my Java program using a batch file, I'm able to run it properly. However, when I run the batch file after inserting the code to read a properties file from the Java program, I'm getting the following error.
Can't find bundle for base name app1, locale en_US
Actually i have a conf folder under which I have this properties folder, then I came to know that I need to keep this conf folder in the class path. But I have actually added it as class folder using Eclipse. However, I'm getting the same error. Please let me know what exactly I need to do for running the Java program using batch file. Using Eclipse I'm able to run the properly.
Thanks,
Balaji.
From the comments your batch script:
#echo off
java -Djava.ext.dirs=lib -classpath ./bin com.myapp.app1.demoprogram
pause
Notice how the /conf/ directory is not listed on the classpath. The easiest way to get it there is to just add it to the -classpath argument being passed to the JVM. Something closer to:
#echo off
java -Djava.ext.dirs=lib -classpath ./bin;./conf com.myapp.app1.demoprogram
pause
This is assuming that /conf/ is in the same directory as bin. You might have to do some tweaking to get the setup to work for you application, but the root problem is that while you added the /conf/ folder to the project classpath in eclipse, you need to do the same thing for the batch script so the JVM can find it

Categories