specify classpath of app launched from intellij [duplicate] - java

This question already has answers here:
How to add directory to classpath in an application run profile in IntelliJ IDEA?
(8 answers)
Closed 9 years ago.
In IntelliJ, I have setup a runtime configuration for a Java (console) application. I need to add the directory c:/tmp to the classpath that the app is run with. I guess I need to add -cp c:/tmp to one of the boxes in this dialog, but I can't figure out which one:

Classpath is configured in the Module Dependencies.

The classpath is a VM parameter, so it should belong in there. However, I'm unsure whether you can overwrite the classpath at all using this technique...
If you want to specify the classpath for adding additional jars, you should use libraries instead.
Also, are you sure you want to change the classpath and not the working folder? (The classpath determines where to find classes, the working folder defines the starting directory of your application.)

Related

Java Textio file addition [duplicate]

This question already exists:
how to add new class(textio.java) in jdk 12.0.1
Closed 3 years ago.
Where to put the textio.java and its classes. I am using jdk 12.0.1. it is the error cmd is showing on comipiling file that uses textio class.
Please tell me the correct directory where to put textio class file in jdk 12.0.1 folder.
There are numerous ways to add third party libraries to your runtime. Without more information, the lowest energy way you're looking for isn't quite clear.
I believe the CLASSPATH environment variable is what you want. If it's not defined, you may define one by whatever process matches your operating system.
CLASSPATH is one way for your machine to tell your java installation which folders jars and other jvm bytecode are located in so that your java runtime can load them when your program starts. This can be anywhere, but depending on your operating system, there are standard locations that are often good to follow.
In linux/unix/OSX, you may run echo $CLASSPATH, and a non-empty result means that it's set. If it doesn't exist, then you may run this command to create it pointing to the folder you choose. export CLASSPATH=/path/to/jars. Just keep in mind that without some extra steps, this will be reset on next reboot.
If you're using an IDE such as eclipse or idea, then there are project-specific solutions that can be done as configuration options within your IDE, which will keep things you don't want globally available on your machine from being loaded up every time.

modulepath environment variable in Java similar to CLASSPATH [duplicate]

This question already has answers here:
Is it possible to mix --class-path and --module-path in javac (JDK 9)?
(2 answers)
Closed 3 years ago.
In previous versions of Java if you had for example a user-defined package org.mypackage in windows with directory structure
D:\myprogram\
|
---> org\
|
---> mypackage\
|
---> HelloWorld.class
---> SupportClass.class
---> UtilClass.class
you make the JVM aware of the package by using the CLASSPATH environment variable.
For example:
set CLASSPATH=D:\myprogram
Does set MODULEPATH command instead make the JVM aware of the location of user-defined modules in the latest versions of Java ?
If so why is classpath command still around and what purpose does it serve now?
No, the jdk does not read any MODULEPATH environment variable or similar.
The MODULEPATH only exists as a command-line argument --module-path, but not as an environment variable.
See http://mail.openjdk.java.net/pipermail/jigsaw-dev/2016-September/009345.html
Unlike CLASSPATH, there is no equivalent MODULEPATH to get the option off of the command line.
Also see the linked question Is it possible to mix --class-path and --module-path in javac (JDK 9)? for more details on the differences between boths paths.
If you put a normal jar on the modulepath, it becomes an automatic module, exporting its packages (and reserving those packages for itself).
While you can put any normal jar file into the MODULEPATH, there is a restriction: If 2 jar files contain the same package, then java will not allow both of them in the modulepath. That's why Java still has the classpath, because several existing jar files still have overlapping packages, and it is not possible to put them all on the MODULEPATH. They have to be cleaned up first.
In the far future, when all libraries have been migrated to modules (or cleaned up), maybe classpath can be removed.

Add classpath to Eclipse run configuration [duplicate]

This question already has answers here:
How to import a jar in Eclipse?
(11 answers)
Closed 4 years ago.
I have JDBC driver connected to my Eclipse project. According to MS manual I need to set claspath for JDBC. How to do this in Eclipse? I suppose it should be somewhere in run configuration, but I can't find it. Where I can set classpath in Eclipse?
I need to set CLASSPATH environment variable and not just add jar file. I can connect to SQL server from my project while LOG4J library can't. I suppose since it loads driver at runtime.
Project menu -> Properties -> Java Build Path
You can add .jar files to your classpath in the Libraries tab

Can I Prevent Eclipse Fom Adding the JRE Jars to the Classpath?

I noticed that when running a Java application or unit test in Eclipse, it explicitly adds the jars of the JRE (rt.jar, etc.) to the -classpath argument of java.
First question: Why? This seems unnecessary, since those jars are implicitly on the bootstrap classpath anyway, right?
Second question: Is there a way to prevent Eclipse from doing that? I found that I can remove those jars from -classpath by deleting the "JRE System Library" from the "Bootstrap Entries" in the Classpath tab for a specific run configuration, but I would like to configure this for the whole project, no for every run config separately.

Java command too long to run [duplicate]

This question already has answers here:
Avoid error when running java tests which generate super long command lines?
(2 answers)
Closed 9 years ago.
I've a Java console application with a lot of dependencies. I've to run it in production, so I created a runnable jar. The fact is that in order to run it, I need to write something like this:
java -classpath C:\Users\dandini\workspace\detror\target\classes;C:\Users\dandini\.m2\repository\mysql\mysql-connector-java\5.1.10\mysql-connector-java-5.1.10.jar;C:\Users\dandini\.m2\repository\org\hibernate\hibernate-entitymanager\4.2.4.Final\hibernate-entitymanager-4.2.4.Final.jar;C:\Users\dandini\.m2\repository\org\jboss\logging\jboss-logging\3.1.0.GA\jboss-logging-3.1.0.GA.jar;C:\Users\dandini\.m2\repository\dom4j\dom4j\1.6.1\dom4j-1.6.1.jar;C:\Users\dandini\.m2\repository\org\hibernate\hibernate-core\4.2.4.Final\hibernate-core-4.2.4.Final.jar;C:\Users\dandini\.m2\repository\antlr\antlr\2.7.7\antlr-2.7.7.jar;C:\Users\dandini\.m2\repository\org\jboss\spec\javax\transaction\jboss-transaction-api_1.1_spec\1.0.1.Final\jboss-transaction-api_1.1_spec-1.0.1.Final.jar;C:\Users\dandini\.m2\repository\org\hibernate\javax\persistence\hibernate-jpa-2.0-api\1.0.1.Final\hibernate-jpa-2.0-api-1.0.1.Final.jar;C:\Users\dandini\.m2\repository\org\hibernate\common\hibernate-commons-annotations\4.0.2.Final\hibernate-commons-annotations-4.0.2.Final.jar;C:\Users\dandini\.m2\repository\org\javassist\javassist\3.15.0-GA\javassist-3.15.0-GA.jar;C:\Users\dandini\.m2\repository\commons-dbcp\commons-dbcp\20030825.184428\commons-dbcp-20030825.184428.jar;C:\Users\dandini\.m2\repository\commons-pool\commons-pool\20030825.183949\commons-pool-20030825.183949.jar;C:\Users\dandini\.m2\repository\c3p0\c3p0\0.9.1.2\c3p0-0.9.1.2.jar;C:\Users\dandini\.m2\repository\org\slf4j\slf4j-api\1.6.6\slf4j-api-1.6.6.jar;C:\Users\dandini\.m2\repository\org\slf4j\jcl-over-slf4j\1.6.6\jcl-over-slf4j-1.6.6.jar;C:\Users\dandini\.m2\repository\org\slf4j\slf4j-log4j12\1.6.6\slf4j-log4j12-1.6.6.jar;C:\Users\dandini\.m2\repository\log4j\log4j\1.2.15\log4j-1.2.15.jar;C:\Users\dandini\.m2\repository\commons-httpclient\commons-httpclient\20020423\commons-httpclient-20020423.jar uk.co.dandini.Detrot uk.co.dandini.Detrot
Isn't there a shorter way? How is it usually deployed and run in production?
Usually it's packed into an executable JAR which contains a manifest with a classpath which Java uses to load classes: How To Make A Java Exe File Or Executable JAR File.
Or you could created your own loader which sets the classpath for your app as it's done in Maven AppAssembler plugin (recommended).
You are using maven, so it's very easy. Probably the best option is to build a standalone "uberjar" which will contain all the dependencies, and will be runnable with just
java -jar myApp.jar
Please refer to this answer on StackOverflow.
you add the dependencies to the MANIFEST file
http://docs.oracle.com/javase/tutorial/deployment/jar/downman.html

Categories

Resources