Java adding to the classpath without overwriting it - java

Is there a way in Java to add a file to the classpath without overwriting the existing one?
So that the application would take what is provided to it (whatever other means are used for that) but adding just this one more file I provide.
I don't want to do it on the fly by the application - but on starting it.
-cp overwrites the classpath - That's what I don't like. Is there another option?

Related

How do I include java stuff in .jar files?

Okay. So here's my question: I am making a data parser in Clojure. One part of my program is that it has to be able to graph the data. I figure, I'll use jFreeChart. However, I have absolutely NO IDEA how to include stuff in JAR files. What I mean is: if I have a app.jar file in my classpath, I don't seem to be able to do:
import app.thing.thing2
without changing the classpath to be inside the jar file.
The idea here is that I don't think I can change my classpath since I need to set it to run Clojure (Or do I?). The global classpath is currently /usr/share/java.
And please don't ask me to use Maven, Ant or any project-building tool unless it is the only way to do this. This is a script for personal use that doesn't need or want a whole lot of overhead.
I wonder if I should just unpack every JAR file, so that I can reference the directory structure? Is this bad?
Let me know if you need any clarifications!
The content of the (Java) CLASSPATH environment variable is available to Clojure so if you add your jar to the global classpath before to run Clojure, you'll "see" it:
export CLASSPATH=/path/to/jfreechart.jar:$CLASSPATH
But, in my opinion, this is not the "clean" way to add a jar to Clojure's classpath (because this makes the library visible to any Java program and may not be desired). Instead, you should use the CLOJURE_EXT environment variable. This is how this variable is documented:
# CLOJURE_EXT The path to a directory containing (either directly or as
# symbolic links) jar files and/or directories whose paths
# should be in Clojure's classpath. The value of the
# CLASSPATH environment variable for Clojure will be a list
# of these paths followed by the previous value of CLASSPATH
# (if any).
On my system, it is defined as below:
export CLOJURE_EXT=~/.clojure
So, to add jfreechart.jar (or any other library) to Clojures's classpath, copy it (or add a symlink pointing to it) in the directory defined in the CLOJURE_EXT variable.
And by the way (I'm sorry but your question is not that clear), if you want to bundle some Java classes into a jar, the command is something like that:
$ jar cf myjarfile *.class
You'll find documentation of jar - the Java Archive Tool - here.
I completely respect your desire not to use a project management tool, though I just spent longer typing this sentence than it takes to set up leiningen. For your one-off script any tool is going to be overkill and Pascal Thivent's answer covers this very well. For people reading this question who perhaps want to produce a jar file, or easily load their Clojure into emacs/slime-swank I cant recommend leiningen too strongly.
If you going to basics you can inline your classpath to include the hardcoded location of your jars, so if you on windows it will look something like
java -cp .;%CLASSPATH%;C:/here/it/is/foo.jar com.foo.MyClass
Not sure how clojure is run, but don't you just add the jar file to the classpath?
i.e.
/usr/share/java:/home/user/myjarfile.jar

Adding properties file to java classpath at runtime

My code generates few properties file at runtime and these will be used by other portion of code.But the other portion of code expects that those properties files in the classpath.
Is there any way to generate the files in classpath at runtime.
You could simply create empty files which are set on the classpath up front and the application appends to these as necessary.
I'm wondering though, do you need to touch the hard disk at all? Could you not use a cache instead?

Java - Get list of file names in the current JAR

I need to get the file names of each file that is in a particular folder inside my program's JAR while it is running. Is it possible to do this? I'm not sure where to start.
It needs to be done programmatically and be platform independent.
To list the contents of a jar file, simply run:
$jar tf MyJar.jar
Are you looking to do this programmatically from within java?
To do it programmatically, see this example.
You are asking for something that isn't available in general. Classes are loaded via ClassLoader instances, which may get class bytecode from many different places (network, jar file, .class files in a directory, dynamically generated).
The most you can know is the package hierarchy, which you can get from
myObject.getClass().getClassLoader().getPackages()
which returns a list of packages available at the point of invocation. For a given package you probably won't be able to tell where it came from.

Better way to add custom Java classes to Matlab?

I can create Java classes, compile them into *.class files and may be pack them into *.jar files and place anywhere.
But I don't like editing of javaclasspath.txt, because it is systemwide and located somewhere in Matlab directory.
Also I don't like issuing 'javaaddpath' command and similar, because it will require doing this each time.
I would like to attach some jars or class-files to directory. For example, just put them there. Or I would like to add ajr files or classpath entries with Set Path menu of Matlab GUI.
Fortunately, it is said, that "You can also put the JAR file on the MATLAB path." in manual.
But unfortunately, I can activate this. Adding path entries feature does not see jar files and if I add java classpath entry, it does not work.
Is it possible to add classpath entries to Matlab in my suggested way?
You can place the javaaddpath(...) call in a startup.m file that you create and is placed anywhere on your MATLAB path, such as your default start directory.
You will not need to call javaaddpath each time you launch MATLAB because a startup.m on your path is automatically called every time.
It will not be systemwide unless you happen to place the startup.m in one of the MATLAB system paths.
see doc startup
Another variation is to use the -r flag and specify an m-file to run on launch that can be named anything. You can modify the Windows shortcut to use this flag, or create an alias if using Linux.
Follow the "Specifying Startup Options in the MATLAB Startup File" link from the doc startup page mentioned above for more info.
Using a startup file or function is the proper way to handle it - as explained in the other answer.
To see a better way to add jar files dynamically than using javaaddpath, refer to https://stackoverflow.com/a/21489260/2043349.

JAVA ClassLoad same class name

Yesterday i thought one question ,below is the detail:
I have 3 JAR files, a.jar, b.jar ,c.jar . both these jars files have a class named com.test.Test ,and sayHello() was defined in this class.
I create a web application, i reference a.jar,b.jar,c.jar . And in main method, i involve sayHello(); .at this time, which com.test.Test will be load?
the result is a.jar.
any body tell me the reason ?? thanks in advance!!!
That is what java language specification says. It loads what ever the class first occurs in classpath and ignores other.
Instead of focusing on which one will be loaded, realize that the stuff within the JAR files probably need their com.test.Test class instead of someone else's com.test.Test to work properly. That means for a functional system you'll have to make a way that a.jar finds a.jar's com.test.Test instead of the one in b.jar. The same goes for b.jar finding it's classes in preference to a.jar's.
The only way to do this is to use a framework which adds name spacing beyond the java package mechanism. This is typically done with multiple classloaders, often one for each JAR file. You can write such a thing yourself (Tomcat did), where you need to specify the rules for cross-loader discovery, or use something akin to a OSGi framework.
Whichever Jar File comes first in your classpath will be used..
You can modify your CLASSPATH environment variable to the path of your Jar file
Suppose you modify it as below: -
set CLASSPATH = %CLASSPATH%;.;a.jar;b.jar
then a.jar will be used..
You can also modify it by: -
set CLASSPATH = %CLASSPATH%;.;b.jar;a.jar
In this case, b.jar will be used..
These commands you need to run from your Command Line..
** NOTE: - If you are using any IDE, then they don't use System Classpath.. You need to set different classpath for the IDE you are using..
If you are using an IDE, such as eclipse, you can modify your classpath on the properties of the project, then go to Build Path, and then you have the Order and Export tab where you can move up and down the jars. The one of the top will be the first taken by your application.
This you can also do manually by editing the file called "classpath" which is on your project and move to the top the jar you want your application to use first.

Resources