cannot compile java files using cygwin - java

I am trying to compile some files using cygwin shell.
java file that I am trying to compile is C:\Users\Programs\x.java
For windows command prompt
C:\Users\Programs>javac x.java
No errors.
For Cygwin shell
$ javac /cygdrive/c/Users/Programs/x.java
Error:
javac: file not found: \cygdrive\c\Users\Programs\x.java
Why do I get an error in cygwin shell though it runs in windows command prompt

javac is Windows application. It has no clue about /cygdrive. Always keep it in mind and pass valid Windows paths.
So use javac 'c:/Users/Programs/x.java' or javac c\:/Users/Programs/x.java
Also, as in most Unix shells \ must be escaped if you need to use it.

I think that the problem is that "cygdrive" is a pseudo-device that is implemented in a Cygwin-specific library. If the executable you are running has not been linked against that library (and the Oracle Java executables have not!) it won't correctly resolve the "/cygdrive" path component.

Related

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

Cannot compile java file using javac command under JDK on ubuntu (APARAPI)

I have run the same command under windows using the windows Java Development Kit and it worked.Now i try to run it ubuntu and i get this error.Please help
Different OS have different dist package manager, you try to install Java environment in your Linux system by below Reference
It looks like you are specifying the -cp argument incorrectly.
On Linux, the path separator character is ":" not ";". The ";" character separates commands on the command line, assuming that you are using bash or similar as your shell.
It looks like your command has been interpreted as two commands:
$ javac -g -cp ../dist_windows_x86_64/something
$ . GPUGalaxySim.java
The javac command fails because there are no source filenames on that command line ... just like the error message says.
Then the second command:
The dot command (".") is a built-in shell command that "sources" a file and attempts to interpret it as shell commands.
If you attempt to "source" a file that is actually Java source code, you get nonsense error messages, basically because the shell has no clue what Java code means.

Run and compile java code in cygwin

I just installed cygwin and I am wondering how do I compile and run my java code through cygwin?
My java code is at my desktop saved in a file named Java.
Assuming you have Java SDK for Windows installed.
In the simplest case:
Ensure/Add java to PATH in cygwin:
export PATH=$PATH:"/cygdrive/C/Program\ Files/Java/jdk1.8.0_31/bin/"
(don't forget the backslash after Program)
cd to your desktop:
cd /path/to/Desktop
run java compiler:
javac HelloWorld.java
In complex projects you will need to provide a bunch or arguments to javac to make it compile.

Cross-compile java applications

I am working on an embedded system with openwrt root files system and linux kernel.
I have compiled the trunk, no problem with that. I have installed the Java resources in /usr/bin, /usr/lib and /usr/share, but I haven't been able to compile some simple programs that I have done in Eclipse. I have used javac to compile a hello world and I obtain the .class file but when I try to execute the helloworld.class file in my embedded system with:
java helloworld.class
it does nothing, it just says:
/usr/bin/java: line 1: syntax error: unexpected word (expecting ")")
When I execute this in my computer it runs, so I guess it is because I have to cross-compile the java files, so how can I do that?
The problem is not with your class, but with a syntax error in the /usr/bin/java script - try cat /usr/bin/java
Just try the java command withoud .class extension like
java helloworld

Running a jar from shell script

I have a jar file named umar.jar in /root/umar/bin directory. I have a shell script file run.sh in same directory. Following is the content of run.sh
#!/bin/bash
"$JAVA_HOME"/bin/java -jar /root/umar/bin/umar.jar
Now when I run the shell script, I get the following error
Exception in thread "main" java.lang.UnsupportedClassVersionError: Bad version number in .class file
After some googling, I found (as a folk from stackoverflow mentioned) that such errors occur when the Jar was compiled with a later version of the JDK than your JRE.
Now strange thing is, if I run this command directly on shell
java -jar umar.jar
it works perfectly fine. If the jar was compiled with a later version of JDK than my JRE, it shouldn't have had run at all even from the shell.
What do you suggest?
compare "java -version " and ""$JAVA_HOME"/bin/java -version"
You probably have multiple JVMs installed
As already mentioned - you're trying to use byte code compiled by the later compiler with old jvm.
Please note that if your PATH contains multiple java executables of different versions you can switch between them easily using '-version' key.
Suppose you have java5 and java6 at your PATH and java5 is located before java6 there. You can see that java5 is used by default then (if you execute 'java -version' it prints corresponding information). However, you can start java6 easily using command like 'java -version:1.6 ...' (e.g. if you execute 'java -version:1.6 -version' you see that java6 is used).
Export the java and jre home which you need to use in your sh file.
Run the jar using the exported java home
e.g
export JAVA_HOME=/opt/java6
export JRE_HOME=/opt/java6/jre
/opt/java6/bin/java -Xms2048m -Xmx2048m -jar abc.jar $1

Categories