I wonder how it is possible to use specific weka utility.
For exaple, I want to use the following utility TextDirectoryToArff.
I tried to run it as follows
javac TextDirectoryToArff.java
TextDirectoryToArff.java:21: package weka.core does not exist
and like following
java -jar /usr/share/java/weka-3.6.6.jar TextDirectoryToArff.java
and in this case weka starts and not the utility.
How it's possible to run the utility.
You first have to compile the TextDirectoryToArff.java file. This is done with javac. The error message in your case just indicates that you did not tell him where to find the weka classes that are required for the compilation. You can tell him where he can find these classes by specifying the classpath (cp) :
javac -cp /usr/share/java/weka-3.6.6.jar TextDirectoryToArff.java
This should create a file TextDirectoryToArff.class in the current directory.
In order to start this utility, you can simply call
java -cp .:/usr/share/java/weka-3.6.6.jar TextDirectoryToArff
Note that you do not use the -jar parameter, because you do not want to run what is inside the JAR, but the TextDirectoryToArff class directly. Therefore, you again have to specify the classpath. In this case, the classpath contains current directory (indicated by the .) (because it contains the TextDirectoryToArff.class file), and, separated from that with a :, the path to the weka JAR.
You'll also have to specify a directory name, so the full command line will be something like
java -cp .:/usr/share/java/weka-3.6.6.jar TextDirectoryToArff your/directory/name
Related
I am looking for a way to run the standard class file as produced by Intellij for hello world, using a gradle build with kotlin-stdlib-1.3.11 as the only dependency.
I know I could make a jar file and run that but that is not the question. That question is already answered in many places, but please do not answer with those solutions as that is not the question I am asking.
The simple class file for 'hello world' needs access to the kotlin-stdlib-1.3.11.jar, and I am looking for a way to run the class file and manually specify jars to use for satisfying the dependencies.
I am making notes for team members on why:
java HelloKt
in folder where the class file is located, should give a NoClassDefFoundError and also looking to then show how manually specifying 'run the class but with the kotlin std lib as well' should then work.
I repeat, I am not trying to just get the program to run. I am trying to show how run the standard library is required to run the class file.
This is about the classpath.
The easy answer is to run kotlin instead of java, as that adds the Kotlin support to the classpath automatically:
> kotlin HelloKt
However, if you need to run java, then you'll need to set up the classpath yourself.
The manpage for java says:
The Java runtime searches for the startup class, and other classes used, in three sets of locations: the bootstrap class path, the installed extensions, and the user class path.
The first two are part of the Java installation, and rarely touched, so it's the user class path that you need to look at.
You need it to contain both kotlin-runner.jar and the path for your HelloKt.class file. The latter could simply be . for the current directory; the former will depend where you've installed Kotlin. (For example, I installed it using Homebrew, and that jar is currently /usr/local/Cellar/kotlin/1.3.31/libexec/lib/kotlin-runner.jar.)
The manpage continues:
-classpath classpath
-cp classpath
Specifies a list of directories, JAR archives, and ZIP archives
to search for class files. Class path entries are separated by
colons (:). Specifying -classpath or -cp overrides any setting
of the CLASSPATH environment variable.
If -classpath and -cp are not used and CLASSPATH is not set, the
user class path consists of the current directory (.).
So there are two ways you can do this: either set the $CLASSPATH environment variable before running java:
> export CLASSPATH="/usr/local/Cellar/kotlin/1.3.31/libexec/lib/kotlin-runner.jar:."
> java HelloKt
Or pass a -classpath or -cp flag:
> java -cp /usr/local/Cellar/kotlin/1.3.31/libexec/lib/kotlin-runner.jar:. HelloKt
(Or, as you say, you could build a jar file which includes the Kotlin support classes as well as your own. That's probably the best option if you're going to distribute it to machines which might not have Kotlin installed. But it's not the only option.)
As stated by #gidds, yes it is about the classpath.
The command java HelloKt is telling java the class to run is HelloKt, which is the class Kotlin uses to provide a containing class for an app called Hello, but that command does not identify where the code is that should be run. The code must be specified by the classpath. With the HelloKt.class file in the current directory, then '.' as a classpath with allow the HelloKt class to be found and the code to start, but it will quickly die because the repository 'kotlin-stdlib-1.3.11' as specified in the gradle build, must also be available to supply classes for run time. so
java -cp ".";"<path to stdlib>\kotlin-stdlib-1.3.11.jar"
will successfully run the file. Note, each jar must be a entry in the classpath, just having the folder containing the jar is not enough. On windows ';' separates entries, on mac or linux, use ':'. Each entry can be in quotes, and will need to be only if there are special characters in the path.
I have read in docs, especially here
https://docs.oracle.com/javase/7/docs/technotes/tools/solaris/java.html
https://docs.oracle.com/javase/1.5.0/docs/guide/jar/jar.html#JAR%20Manifest
that -classpath is ignored when java is called with -jar option.
In this case, resources are read from manifest file.
Maybe I misunderstanding and the way to set paths from out resource is exist when call java -jar?
If not why so?
This is just how the java programs was coded to behave.
What's important is that you can still use a JAR and specify the class path on the command line. This requires that you make the jar itself part of the class path, and then specify the class, such as:
java -cp javafile.jar fully.qualified.ClassName
Java will run the fully.qualified.ClassName main class, which it will find on the class path (in which the jar file is included).
Trying to compile and run my java program from the commandline that is set up a bit weird. The file structure is as follows:
[ROOT]/
|
|____libs/
| |____myExtraJar.jar
|
|____src/
|____main/
|____com/
|____example/
|____myClass.java
The package is defined at the top of the java file as
package com.example;
I am able to compile the program fine (I think) while in the root folder, using
javac -classpath "/libs/myExtraJar.jar" src/main/com/example/*.java
I don't get any compilation errors (such the ones that occur if I leave off the classpath) and I can see that .class files are being created in the com/example/ folder. However, I can't find any way to run the compiled program. Running
java src/main/com/example/myClass
results in the message
Error: Could not find or load main class src.main.com.example.myClass
Any help would be appreciated.
You need to specify the classpath when you run it, and you also need to use the fully-qualified classname. Like,
java -cp "libs/myExtraJar.jar:src/main" com.example.myClass
Elliot is right. More precisely, you need to add the build directory to your classpath. It is the directory containing your *.class files, and is sometimes named target/.
$ java -cp "target:lib/myExtraJar.jar" com.example.myClass
Moreover, src/main/com/example/myClass should be com.example.myClass, which is the fully-qualified class name. See http://www.manpagez.com/man/1/java/ for details of the java command.
After I build this project from an ant file, I recieve a jar that contains all of the classes I built. When I try to run this jar, I get the following error:
Exception in thread "main" java.lang.NoClassDefFoundError: javax/media/j3d/SceneGraphObject
This error indicates that a one of the jars, specifically the j3dcore.jar from java3d, I am using can not be found. However, this jar is on the classpath when compiling through the ant build into the class files.
Why can this class not be found at runtime, but it is found at compile time? Do I have to manually change my classpath in my shell when running the jar as well as change it in the ant build?
If I add the jars to my classpath using java -cp j3d/*.jar -jar idv.jar
I get the error Error: Could not find or load main class j3d.j3dutils.jar
Do I have to manually change my classpath in my shell when running the jar as well as change it in the ant build?
Yes, absolutely. Making a class available at compile-time doesn't embed the class into your output or anything like that. It just makes it available to the compiler (to find out what methods are present etc).
If I add the jars to my classpath using java -cp j3d/*.jar -jar idv.jar
Yes, it would - because that's being expanded into:
java -cp j3d/foo.jar j3d/bar.jar ... -jar idv.jar
It's not clear to me whether -cp is meant to work at all with -jar, given this documentation:
When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored.
One option is to set the classpath within the manifest of the jar file itself. For example:
Class-Path: j3d/foo.jar j3d/bar.jar
Another would be to ignore the -jar command-line option for now, and use:
java -cp j3d/*:idv.jar your.class.name.Here
Note the * rather than *.jar, as documented:
As a special convenience, a class path element containing a basename of * is considered equivalent to specifying a list of all the files in the directory with the extension .jar or .JAR (a java program cannot tell the difference between the two invocations).
Okay, I'm very new to linux and command line, and fairly new to java. I got an internship building a java program. I finally got it done on my machine (windows) and now I have to migrate it to a linux machine to test and then have it run as an executable. I have done much reading and researching on linux and understanding classpaths but it is still all very hard to fully comprehend. It's just not clicking for me yet. Can anyone explain the purpose of classpath in a simplified way using examples? One of the most confusing aspects to me is actually defining the physical path to the jar. Do I start all the way from usr or do I only need to begin from the jvm folder? If it matters, my java program is not located in the jvm folder. Can anyone shed some light for me?
EDIT: thank you guys very much for your help, I can't say that I'm fully in the clear but my understanding of my situation is a lot better.
Say you have multiple jar files a.jar,b.jar and c.jar. To add them to classpath while compiling you need to do
$javac -cp .:a.jar:b.jar:c.jar HelloWorld.java
To run do
$java -cp .:a.jar:b.jar:c.jar HelloWorld
You use the -classpath argument. You can use either a relative or absolute path. What that means is you can use a path relative to your current directory, OR you can use an absolute path that starts at the root /.
Example:
bash$ java -classpath path/to/jar/file MyMainClass
In this example the main function is located in MyMainClass and would be included somewhere in the jar file.
For compiling you need to use javac
Example:
bash$ javac -classpath path/to/jar/file MyMainClass.java
You can also specify the classpath via the environment variable, follow this example:
bash$ export CLASSPATH="path/to/jar/file:path/tojar/file2"
bash$ javac MyMainClass.java
For any normally complex java project you should look for the ant script named build.xml
The classpath is the place(s) where the java compiler (command: javac) and the JVM (command:java) look in order to find classes which your application reference.
What does it mean for an application to reference another class ? In simple words it means to use that class somewhere in its code:
Example:
public class MyClass{
private AnotherClass referenceToAnotherClass;
.....
}
When you try to compile this (javac) the compiler will need the AnotherClass class. The same when you try to run your application: the JVM will need the AnotherClass class.
In order to to find this class the javac and the JVM look in a particular (set of) place(s). Those places are specified by the classpath which on linux is a colon separated list of directories (directories where the javac/JVM should look in order to locate the AnotherClass when they need it).
So in order to compile your class and then to run it, you should make sure that the classpath contains the directory containing the AnotherClass class. Then you invoke it like this:
javac -classpath "dir1;dir2;path/to/AnotherClass;...;dirN" MyClass.java //to compile it
java -classpath "dir1;dir2;path/to/AnotherClass;...;dirN" MyClass //to run it
Usually classes come in the form of "bundles" called jar files/libraries. In this case you have to make sure that the jar containing the AnotherClass class is on your classpaht:
javac -classpath "dir1;dir2;path/to/jar/containing/AnotherClass;...;dirN" MyClass.java //to compile it
java -classpath ".;dir1;dir2;path/to/jar/containing/AnotherClass;...;dirN" MyClass //to run it
In the examples above you can see how to compile a class (MyClass.java) located in the working directory and then run the compiled class (Note the "." at the begining of the classpath which stands for current directory). This directory has to be added to the classpath too. Otherwise, the JVM won't be able to find it.
If you have your class in a jar file, as you specified in the question, then you have to make sure that jar is in the classpath too , together with the rest of the needed directories.
Example:
java -classpath ".;dir1;dir2;path/to/jar/containing/AnotherClass;path/to/MyClass/jar...;dirN" MyClass //to run it
or more general (assuming some package hierarchy):
java -classpath ".;dir1;dir2;path/to/jar/containing/AnotherClass;path/to/MyClass/jar...;dirN" package.subpackage.MyClass //to run it
In order to avoid setting the classpath everytime you want to run an application you can define an environment variable called CLASSPATH.
In linux, in command prompt:
export CLASSPATH="dir1;dir2;path/to/jar/containing/AnotherClass;...;dirN"
or edit the ~/.bashrc and add this line somewhere at the end;
However, the class path is subject to frequent changes so, you might want to have the classpath set to a core set of dirs, which you need frequently and then extends the classpath each time you need for that session only. Like this:
export CLASSPATH=$CLASSPATH:"new directories according to your current needs"
For linux users, you should know the following:
$CLASSPATH is specifically what Java uses to look through multiple directories to find all the different classes it needs for your script (unless you explicitly tell it otherwise with the -cp override). Using -cp (--classpath) requires that you keep track of all the directories manually and copy-paste that line every time you run the program (not preferable IMO).
The colon (":") character separates the different directories. There is only one $CLASSPATH and it has all the directories in it. So, when you run "export CLASSPATH=...." you want to include the current value "$CLASSPATH" in order to append to it. For example:
export CLASSPATH=.
export CLASSPATH=$CLASSPATH:/usr/share/java/mysql-connector-java-5.1.12.jar
In the first line above, you start CLASSPATH out with just a simple 'dot' which is the path to your current working directory. With that, whenever you run java it will look in the current working directory (the one you're in) for classes. In the second line above, $CLASSPATH grabs the value that you previously entered (.) and appends the path to a mysql dirver. Now, java will look for the driver AND for your classes.
echo $CLASSPATH
is super handy, and what it returns should read like a colon-separated list of all the directories you want java looking in for what it needs to run your script.
Tomcat does not use CLASSPATH. Read what to do about that here: https://tomcat.apache.org/tomcat-8.0-doc/class-loader-howto.html
Step 1.
vi ~/.bashrc
Step 2. Append this line on the last:
export CLASSPATH=$CLASSPATH:/home/abc/lib/*; (Assuming the jars are stored in /home/abc/lib)
Step 3.
source ~/.bashrc
After these steps direct complile and run your programs(e.g. javac xyz.java)