Clojure REPL not launching at Windows command prompt - java

I have placed the clojure-1.4.0.jar path (C:\clojure-1.4.0\clojure-1.4.0.jar) in my CLASSPATH environment variable. Now when I try to launch the REPL from the command line with the following code:
java -cp clojure-1.4.0.jar clojure.main
I get an error:
Error: Could not find or load main class clojure.main
It used to work before I set up emacs. Any ideas.

You can either add clojure jar file to CLASSPATH environment variable:
/some/where % CLASSPATH=/tmp/clojure-1.4.0.jar java clojure.main
or specify it directly in java arguments:
/some/where % java -cp /tmp/clojure-1.4.0.jar clojure.main
Setting CLASSPATH variable and providing -cp argument to java command at the same time is pointless, because -cp argument overrides CLASSPATH completely. This is the cause of your problem: you seem to be invoking java command not from the directory where clojure-1.4.0.jar is located, so -cp clojure-1.4.0.jar switch makes java program try to locate clojure-1.4.0.jar in the current directory and ignore CLASSPATH. Since there is no clojure-1.4.0.jar in the current directory, the command fails.

Related

Run Jar Files in PATH

I store jar files in C:\Users\myuser\javatools\avro-tools
And added them to my PATH:
echo %PATH%
...;
C:\Users\myuser\javatools\avro-tools;
I can run them by specifying the full path:
java -jar C:\Users\myuser\javatools\avro-tools\avro-tools-1.8.1.jar
But cannot run them without the full path:
java -jar avro-tools-1.8.1.jar
Error: Unable to access jarfile avro-tools-1.8.1.jar
I need to run jar files without changing to the directory, nor specifying these full paths.
UPDATE: Added %*
I'd recommend creating a batch file and run that instead.
avro-tools-1.8.1.bat
#echo off
java -jar C:\Users\myuser\javatools\avro-tools\avro-tools-1.8.1.jar %*
Place .bat file somewhere in PATH, and run by simply typing:
avro-tools-1.8.1.bat -abc def
The %* in the .bat file gets replaced with any argument passed to the .bat file, so the -abc def arguments are passed to the avro-tools program in the args array to the main method.
If you have multiple versions of Java installed, you can then choose which one to use when running that .jar file, by also qualifying the java command.
avro-tools-1.8.1.bat
#echo off
"C:\Program Files\Java\jdk1.8.0_181\java.exe" -jar C:\Users\myuser\javatools\avro-tools\avro-tools-1.8.1.jar %*
Now that code will run with Java 8, even if Java 8 is not the default Java on your machine.

Failed to run a Java app with Git Bash

I downloaded SymmetricDS, a tool for Database replication and tried to run it on my Windows7 machine. The program can be launched from command line and it works with Windows Terminal. However I always prefer Git Bash for command line stuff. When I run command sym though, I got error:
Error: Could not find or load main class org.jumpmind.symmetric.SymmetricLauncher
This tool is written in Java. I have JDK 1.8 installed. Git Bash inherits all environmental variables including $PATH and $JAVA_HOME from Windows. But why is it complaining about not finding the class?
The sym command is really running the following command:
exec "$SYM_JAVA" $SYM_OPTIONS -cp "$CLASSPATH" org.jumpmind.symmetric.SymmetricLauncher "$#"
All the jars are located in lib under the root directory of the application. The classpath is defined in a sym.service.conf inside conf directory:
# Java Classpath
wrapper.java.classpath.1=patches
wrapper.java.classpath.2=patches/*
wrapper.java.classpath.3=lib/*.jar
wrapper.java.classpath.4=web/WEB-INF/lib/*.jar
# Application main class and arguments
wrapper.app.parameter.1=org.jumpmind.symmetric.SymmetricLauncher
I added echo $CLASSPATH right before the exec to print out the class path and it did seem to get all of them right:
/c/Users/dnj0109/Apps/symmetric-server-3.8.29/patches:
/c/Users/dnj0109/Apps/symmetric-server-3.8.29/patches/*:
/c/Users/dnj0109/Apps/symmetric-server-3.8.29/lib/*:
/c/Users/dnj0109/Apps/symmetric-server-3.8.29/web/WEB-INF/lib/*
That could be related to this thread:
On Windows, the path separator is a semicolon (';' instead of ':').
Don't ask why. Traditionally, the semicolon is interpreted by the Bash as
the command separator, so you'll have to escape it:
$ java -cp lib/clojure-1.1.0.jar\;lib/clojure-contrib-1.1.0.jar
If you wonder why it works with PATH: MSys has special handling routines
for that.
Another reason that a java app may run in a Windows CMD shell but not in a Windows git bash shell is that the classpath used to run the app contains one of the following:
relative paths (e.g. ../foo)
network drives (e.g. //servername/bah
See:
https://github.com/git-for-windows/git/issues/1028

Cygwin terminal error in running .sh file because of a jar file

I am trying to run .jar file for my java code from a .sh shell script file. the jar file name contains "." which is making the Cygwin terminal think it is a directory. Here is the command and the results:
java -jar ./lib/javax.json-1.0.jar
Result:
no main manifest attribute, in lib\javax.json-1.0.jar
Then:
error: package javax.json does not exist
import javax.json.Json;
With this mark ^ below the period (right after javax).
How can I solve it? I am working on Windows 10. Thanks!
EDIT:
I have written many forms of the .sh file to get it run, but it won't run. The current one is:
# !bin/bash
java -jar ./lib/javax.json-1.0.jar
java -jar ./lib/javax.json-api-1.0.jar
javac ./src/TimeTester.java
java TimeTester
Does this look good?
I am getting the following error:
.\src\TimeTester.java:22: error: package javax.json does not exist
import javax.json.Json; (With this ^ below the '.')
AND:
.\src\TimeTester.java:159: error: cannot find symbol
private static JsonObject getJsonFromString(String jsonStr){
And many similar lines in the error.. Any help?
EDIT 2:
This is my current file:
javac -cp ./lib/javax.json-1.0.jar:./lib/javax.json-api-1.0.jar ./src/TimeTester.java
java -cp ./lib/javax.json-1.0.jar:./lib/javax.json-api-1.0.jar:./src TimeTester
But I am getting:
.\src\TimeTester.java:22: error: package javax.json does not exist
import javax.json.Json;
^
With With this (^) under the last dot (.Json)
EDIT 3:
The current .sh file is:
#!/usr/bin/env bash
cd src
javac -cp '../lib/javax.json-1.0.jar;../lib/javax.json-api-1.0.jar' TimeTester.java
java -cp '../lib/javax.json-1.0.jar;../lib/javax.json-api-1.0.jar' TimeTester
The first command (javac) works and generates the .class file. BUT, the second command (java) does not work and it gives the following error:
Error: Could not find or load main class TimeTester
Your help is really appreciated!
Final EDIT:
Thanks for Jim, the shell script now works. Now I got a java execution error:
java.io.FileNotFoundException: .\in_input\in.txt (The system cannot find the path specified)
Thanks
TL;DR It is a pain to use Cygwin with programs written for Windows because of the conflicting command-line shell conventions between bash and cmd.exe. To compile and run Java programs it is much better to use an IDE such as Eclipse or Netbeans.
However, if you must...
None of this works because you are trying to pass Linux-style paths to the Windows JVM. However you seem to have a more basic misunderstanding:
# !bin/bash
java -jar ./lib/javax.json-1.0.jar
java -jar ./lib/javax.json-api-1.0.jar
javac ./src/TimeTester.java
java TimeTester
I am surmising that you think the first two statements make the libraries available to the compiler for the third javac line. This is not true, those two lines attempt to execute the jar file, which of course fails since the jar does not contain a main class
What you should be doing is providing those two library paths as arguments to the -cp option of the javac command.
This is where it gets quite tricky, as you are mixing a Linux-style shell emulator with a Windows JVM. Paths that are intended for the shell must remain in Linux style, while paths that are going to be consumed by the JVM must be converted to Windows format, and path strings for the JVM must be delimited with semicolon (Windows style) instead of colon (Linux style). That introduces a further complication since the semicolon in Cygwin (Linux) is the delimiter for multiple commands on one line, so the path string must be quoted to prevent the semicolon from breaking things.
Also problematic is the naming of the class to be compiled. You have not shown us the package declaration of the Java file, but I'm assuming it's in the default package (i.e. there is no package declaration and it's not package src;). In that case you should be in the src directory, not one directory above.
Finally, once you specify -cp, you must also add the current directory to the classpath on Windows if you want it to be included, otherwise it will not find your newly-compiled .class file.
So the compile and execute commands should be
javac -cp '../lib/javax.json-1.0.jar;../lib/javax.json-api-1.0.jar' TimeTester.java
java -cp '.;../lib/javax.json-1.0.jar;../lib/javax.json-api-1.0.jar' TimeTester
For simple relative paths the Windows JVM will accept forward slashes, but if you have absolute Linux paths (i.e. /cygdrive/c/..., or with the cygdrive path set to /, paths like /c/user/...) the JVM will not understand them and they will need to be translated using cygpath.
None of your 4 commands work:
java -jar ./lib/javax.json-1.0.jar does not work because javax.json-1.0.jar is not an executable jar file.
java -jar ./lib/javax.json-api-1.0.jar does not work because javax.json-api-1.0.jar is not an executable jar file.
javac ./src/TimeTester.java does not work because your class requires classes from the javax.json package to be on the classpath, and you haven't set the classpath. Classes from the javax.json package are found in the javax.json-1.0.jar file.
java TimeTester does not work because the compilation failed.
To fix all that, remove the first two lines, and specify the classpath on the other two lines, e.g.
javac -cp ./lib/javax.json-1.0.jar:./lib/javax.json-api-1.0.jar ./src/TimeTester.java
java -cp ./lib/javax.json-1.0.jar:./lib/javax.json-api-1.0.jar:./src TimeTester
Notice that you also had to list ./src on the classpath when executing your program.

Compiling packages in java at windows command line

I have trying to compile java files at the windows command line using commands such as:
java myProg once I have used javac to create class files.
Problems arise when I use packages with a number of source files.
Often but not always I get main not found errors even though a main exists.
I am not quite sure what some of the directives mean and that is why it seems hit or miss.
Question
what does -cp mean exactly? java -cp src\myDirectory.myfile
sometimes I see:
./ infront of source eg .\src\myDirectory.myfile
on other sites I have found
% javac -cp .;stdlib.jar MyProgram.java
% java -cp .;stdlib.jar MyProgram
while compiling a jar library with java source files
what doesthe ".;" mean?
basically how do I compile three java source java files in one package at the windows command line and what does -cp and .; mean?
-cp means class path if I'm not mistaken.
try reading the following java docs
-classpath path
Specifies the path javac uses to look up classes needed to run javac or being referenced by other classes you are compiling. Overrides the default or the CLASSPATH environment variable if it is set. Directories are separated by semi-colons. It is often useful for the directory containing the source files to be on the class path. You should always include the system classes at the end of the path. For example:
javac -classpath .;C:\users\dac\classes;C:\tools\java\classes ...
https://www.cis.upenn.edu/~bcpierce/courses/629/jdkdocs/tooldocs/win32/javac.html
Answering your question directly, -cp means classpath or path.
Details on commandline arguments used while compiling and running a Java application can be found here: javac - Java programming language compiler
Extracting the description of -cp from that page:
-cp path or -classpath path:
Specify where to find user class files, and (optionally) annotation processors and source files. This class path overrides the user class path in the CLASSPATH environment variable. If neither CLASSPATH, -cp nor -classpath is specified, the user class path consists of the current directory. See Setting the Class Path for more details.
. means the current directory.
To compile multiple files in a directory use the following:
javac *.java // compliles all java files in the dir
java MyClass // runs the particular file
There are also a bunch of other related questions that should help you resolve this:
How to run a java program from the command line
How do I run java program with multiple classes from cmd?
Problems running a java program from the command line interface
Can't run multiple-class program from command line using packages

Could not find or load main class when I add external libraries

Hey I got this problem when running a java program through my cmd.
I use to external libraries : lwjgl.jar and slick-util.jar
when I compile
javac -d bin -sourcepath src -cp ;lib/lwjgl.jar;lib/slick-util.jar src/*.java
everything works fine
but when I try to run it
java -cp bin .;lib/lwjgl.jar;lib/slick-util.jar; Game
I get this error
Error: could not find or load main class .;lib.lwjgl.jar;lib.slick-util.jar;
Replace the space in the run command classpath with a classpath separator
java -cp bin;.;lib/lwjgl.jar;lib/slick-util.jar; Game
^
as an improvement to the run command you could use a classpath wilcards, for example
java -cp ".;bin;lib/*" Game
It is important to use quotes under Windows for this option

Categories