I have a class file which requires a .properties external file with the configuration to run. I am able to run it via the terminal. However, I want to achieve this using eclipse.
java path/to/class --properties path/to/properties.properties
Does anyone know where and what do I need to set in order to achieve this?
Run->Run Configuration->Java Application->[Right Click] New and in Arguments section provide your arguments.
eclipse will load properties from the root of your source directory
or
the eclipse runner (i.e. running any class) has a tab that allows you to manually specify jvm args.
Related
I have written a small application to parse a large XML file using SAX with Intellij.
I pass -DentityExpansionLimit=0 option to my application by going to Run\Edit Configurations... and set VM options.
It works perfectly when I run the application with Intellij, but when I create the artifact with intellij it doesn't work and I get the error which needed to set that option. This is obvious that the option didn't pass to the created jar file.
How should I achieve this goal?
Is there any command that I create with a batch file or something to set this option for my user? Is there any setting file that I can modify to set this option for my machine? (I use windows 10)
Usually, to send system properties to a jar, the command is something like that:
java -DentityExpansionLimit=0 -jar thejar.jar
You are mixing up two things here:
the JVM command line command, and the fact that you can pass arguments to your application, or properties to the JVM itself
your deployment artefact (probably a JAR file)
Meaning: It seems like you want to either pass command line arguments (to some main function) or properties to your application. But the JAR file doesn't have support for that.
JAR files are just a container of class files. You can add some META information via the manifest (which class to run), but that is about it. You can't magically push your IntelliJ "runtime configuration settings" into the JAR.
In other words: IntelliJ has no way of putting these values into your JAR.
When you invoke java -jar Your.jar ... then you (or some other tooling) has to add the required values to the command line.
I have built a jar file which has a log4j.properties file in it (mvn package put it there by default from the resources directory). But when I run this jar file, I want to pass a different logging config, so I add -Dlog4j.configuration=file:{path to file}. The issue that bugs me is that the order matters here as follows:
When I run java -jar {path to jar} -Dlog4j.configuration=file:{path to file} then it reads the log file packaged in the jar.
When I run java -Dlog4j.configuration=file:{path to file} -jar {path to jar}, then it reads the config from the file I pass in the parameters.
I have rough understanding how classpaths work in java and that if I were to load several java classes with the same name, it would make a difference. But this way I am passing a config parameter with a -D prefix, so the way I expect this to work is for some code in log4j library to check whether -Dlog4j.configuration is set and if so, then load the config from there, otherwise try to find it on the classpath.
Any ideas on what I am missing?
If you provide anything after naming the JAR file, it is treated as an argument to your main method. For Log4J you actually have to define a property, and this needs to be done before you specify -jar.
I would like to use the library "Lucene" with java. The instructions to use it tell me I have to put the jar's cointaining the classes inside the CLASSPATH.
The CLASSPATH is the directory containing all the default classes of Java? Or the directory of my specific project? I'm using Eclipse as IDE.
Really confused about that! Thank you.
USEFUL SOLUTION: http://www.avajava.com/tutorials/lessons/how-do-i-use-lucene-to-index-and-search-text-files.html
The Classpath is a collection of directories and JAR files inside which the Java runtime will look for classes.
It can be configured via an environment variable named CLASSPATH, but this usage is not recommended, as it tends to constantly result in problems.
The preferred way to configure the classpath when running a Java program is to pass it via the -cp command line switch when you start the Java interpreter. Usually you write a shell script so you don't have to type it out every time.
If your issue is with using the classes inside the IDE where you write your code, then it depends of course on the IDE. For eclipse, the "Java Build Path" tab of the project properties is where you configure the classpath.
I am working on a program that will allow for modular addons to be installed by simply dropping the correct .jar file into a folder. I was not quite sure how to add a file like this to the build path and use a method from that file. I know how to do this if I know exactly what file I will be using, but in this case I do not. I will need to be able to run a method from the external jar without telling eclipse ahead of time that this is the file I will be using.
You can load the jar using method described on this answer: How to load a jar file at runtime
I would assume you also need an interface to act as a binding contract so you know what to cast your class to
When I finished to write my classes I put them into a package structure and then I jarred all.
Now which is the best way to deploy and use my jar?
setting classpath;
use CLASSPATH variable;
using the extension mechanism.
Don't update the user's CLASSPATH environment variable because there is a risk that your deployed application will interfere with other Java applications that the user might want to run.
Don't deploy using the Extension mechanism because there is a risk that you will interfere with applications run by any user using the JVM that you have "extended".
The best solution is to create and deploy a wrapper script that uses the "-cp" argument, or a shortcut that runs a self-launching JAR file ... as suggested by other answers.
I you have a main class in that jar that you want to run the best approach is to put a META-INF/MANIFEST.MF file in the jar and launch your class using java -jar mypackage.jar
The manifest should contain a Class-Path and a Main-Class attribute
See http://java.sun.com/j2se/1.3/docs/guide/jar/jar.html#JAR%20Manifest
If you mean by 'deploy and use' that you want to use it in another application, set it on the classpath of that application.
If you use the extension mechanism (I assume you mean putting the jar in pathtojava/lib/ext) every application using that jvm will have the jar loaded, the samecounts for the CLASSPATH as system variable. That is most likely not necessary?
If you ment to execute/use the jar as standalone application; just run it commandline, no classpath stuff needed. If you want to deploy on a server you probably wanted to make a war or ear fiel instead.
Best to my opinon is to provide batch files and shell scripts to start the application.
Sophisticated scripts check for environment variables like $JAVA_HOME (%JAVA_HOME%) and use them, if defined or use a default value.
Inside the script you could build the classpath in an internal variable and start the app with a line like
%JAVA_HOME%\bin\java.exe -cp %LIBRARIES% com.example.Main
I would prefer this solution over the java -jar Application.jar alternative, because this one requires that you setup the classpath inside the jars manifest. So deploying an application that depends on existing libraries on the target system is pretty difficults, just because you have to know the library paths before you build the application.
Setting the classpath (using -cp) is the best way, as you can have a different classpath for each application. The CLASSPATH environment variable is global for all Java applications running on the machine, as is the extension mechanism so you probably don't want to use those.
If your code is executable (ie it has a public static void main(String[] args) method) then you can also specify the class that the main method is in, and the associated classpath within the manifest file inside the Jar file. Once you have done that you can do this:
java -jar myJar.jar
And Java will work out the rest.