how to invoke javac from windows git shell/gnu makefile - java

I was working home office with my linux box,
was sure to write portable code.
Used shell and gnumake and compiled java through
javac -Xlint:deprecation -Xlint:unchecked --module-path libs/nrjavaserial-4.0.1.jar:libs/jep-3.9.0.jar:libs/commons-beanutils-1.9.4.jar:libs/commons-collections-3.2.2.jar:libs/commons-logging-1.2.jar:src/ src/main/de/name/product/Product.java -d target/classes/
All ok.
Now in office with windows 10 but still gnumake, does not work.
Also from git bash, no.
I would be content as a first step to get it run in gitshell.
I use javac 11.0.7 and 2.26.0.windows.1.
The errors (i cannot show in full because related to customer)
show that no jar has been accepted:
error: cannot find symbol
NRSerialPort serial = getSingleSerialPort(baudRate);
^
symbol: class NRSerialPort
location: class xxx
shows that libs/nrjavaserial-4.0.1.jar is not accepted athough present
and
error: cannot find symbol
} catch(JepException e) {
^
symbol: class JepException
location: class XXX
shows that libs/jep-3.9.0.jar is not accepted.
Note that on linux all works fine on an ordinary bash shell.
Maybe separators wrong for windows?
I always thought, separators are transparent on git bash?
or only / but not :?
I tried several combinations, without success.
I suspect all quite easy... right?

You've guessed right, you need to use different separators on Windows.
See https://docs.oracle.com/en/java/javase/13/docs/specs/man/javac.html
In the following lists of options, an argument of path represents a
search path, composed of a list of file system locations separated by
the platform path separator character, (semicolon ; on Windows, or
colon : on other systems.) Depending on the option, the file system
locations may be directories, JAR files or JMOD files.
If you want portability, ease-of-use and maintainability, use something like ant, maven or gradle.

Related

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.

Java ProcessBuilder using periods in a string rather than slash when passing parameters

I'm trying to open an xterm terminal in Java, and run a Java file in it. Here's the Java code that is opening up the terminal:
Process p2 = new ProcessBuilder("xterm", "-hold", "-e", "java", "/home/harry/main.class").start();
xterm opens fine, but it's saying that it can't find the main class home.harry.main.class. I'm using slashes, but they're being changed to dots. What am I doing wrong?
The mention of xterm is misleading. The question (agreeing with #elliott-frisch and #user2533521) is how to run a Java class on the command-line. The full pathname and classfile name are two aspects which have to be separated.
Not quite a duplicate, these links can give some insight:
How to run this java class in command line in windows?
How to execute a java .class from the command line
That is (referring to the documentation):
the "/home/harry" can be specified using the classpath -cp option (see Setting the class path).
the ".class" suffix is not useful; only the class name is used (probably "main").

ANTLR Warning: TestRig moved to org.antlr.v4.gui.TestRig

I have been trying to get ANTLR to work all day. I have used several websites including antlr.org, numerous StackOverflow queries and the textbook by Terence Parr. I am on a Windows 7 machine (work machine, can't change to Linux) but I have tried this on both CommandPrompt and Cygwin, with the same result.
No matter what I do, when it comes time to use the grun file, I always come up with the same error in the end:
Warning: TestRig moved to org.antlr.v4.gui.TestRig; calling automatically
Can't load Hello as lexer or parser
I have the most recent versions of the JRE and JDK on my computer, as well as the most recent version of cygwin.
I can run the batch files for grun and antlr4 from anywhere, so I'm strongly guessing my path is set correctly. I can run antlr4.bat with absolutely no issue. It creates every file the text book says I should see:
Hello.g4 HelloBaseListener.java HelloLexer.tokens HelloParser.java Hello.tokens HelloLexer.java HelloListener.java
I can run javac on those files and it generates various CLASS files:
HelloBaseListener.class HelloLexer.class HelloListener.class HelloParser$RContext.class HelloParser.class
But then, when I try to run grun on it, I get this:
Warning: TestRig moved to org.antlr.v4.gui.TestRig; calling automatically
Can't load Hello as lexer or parser
I've tried several combinations of commands to use the grun file, but the one it says to use in the book is:
grun.bat Hello r -tree
I've also changed the .bat file with grun several times, just to experiment, using different combinations in the books and on the sites, but I always come back to this:
java org.antlr.v4.runtime.misc.TestRig %*
That is what my boss told me to use, and it works for him.
Use this in your grun.bat command
java org.antlr.v4.gui.TestRig %*
I had the same problem, there is actually already a thread with the solution here --> antlr4-Can't load Hello as lexer or parser
The first part of the message "Warning: TestRig moved to org.antlr.v4.gui.TestRig; calling automatically" just means that the TestRig order is saved to another location, it works anyway but I changed my batch data accordingly and did not get the Warning anymore. The second part of the message is the actual problem (see link). In my case I forgot the "." (dot) in my CLASSPATH in Windows.

Java compiler does not see packages if jar on classpath

Why doesn't following command work?
$ javac -encoding UTF8 -classpath ./piccolo-1.2.jar:./piccolox-1.2.jar com/google/scrollview/ui/SVAbstractMenuItem.java
It produces multiple errors like
com\google\scrollview\ui\SVAbstractMenuItem.java:22: package com.google.scrollview.events does not exist
import com.google.scrollview.events.SVEventType;
^
This is real code in file SVAbstractMenuItem.java line 22, but warning statement is wrong, because package exists:
$ ls com/google/scrollview/events/
Makefile Makefile.am Makefile.in SVEvent.java SVEventHandler.java SVEventType.java
How to teach this compiler to see packages?
UPDATE
Code is not mine, this is the makefile from tesseract and I am trying to run it under cygwin. Everything looks correct.
Including current directory gives the same result:
$ javac -encoding UTF8 -classpath ./piccolo-1.2.jar:./piccolox-1.2.jar:. com/google/scrollview/ui/SVAbstractMenuItem.java
com\google\scrollview\ui\SVAbstractMenuItem.java:22: package com.google.scrollview.events does not exist
import com.google.scrollview.events.SVEventType;
^
com\google\scrollview\ui\SVAbstractMenuItem.java:56: cannot find symbol
symbol : class SVWindow
location: class com.google.scrollview.ui.SVAbstractMenuItem
public void performAction(SVWindow window, SVEventType eventType) {}
^
com\google\scrollview\ui\SVAbstractMenuItem.java:56: cannot find symbol
symbol : class SVEventType
location: class com.google.scrollview.ui.SVAbstractMenuItem
public void performAction(SVWindow window, SVEventType eventType) {}
^
3 errors
UPDATE 2
I found this occur if JAR present in classpath parameter.
UPDATE 3
I realized that if a colon would separate paths, then Windows/DOS drive letter can't handled. Hence, separating with colon is wrong.
SOLVED
This was Cygwin/Java incompatibility. Windows' Java uses semicolon to separate paths, while linux and makefiles expexts linux-style, i.e. colon. So it was not working under cygwin. Even replacing colon to semicolon in makefile wasn't solving the problem.
Only extracting all jars content into single plain directory helped.
Using javac in cygwin can be difficult. Java compiler is not a cygwin but a windows program. So you must use Windows-style paths.
Because you use the colon ":" in your classpath, your classpath cannot be correctly interpreted by the Windows Java compiler. You would have to use semicolon ";" as path seperator in your classpath. But because you are in a Unix-style shell ";" terminates your command.
Either compile your java sources without cygwin. Since the result are java class files it does not matter. Or put your parameters like classpath and also path to source files in single quotes.
Example:
javac -encoding UTF8 -classpath '.\piccolo-1.2.jar;.\piccolox-1.2.jar;.' 'com\google\scrollview\ui\SVAbstractMenuItem.java'
See also this post:
setting multiple jar files as classpath in cygwin
You forgot the current directory "." in your classpath, so javac isn't even searching the correct path.
Include it in -classpath:
$ javac -encoding UTF8 -classpath ./piccolo-1.2.jar:./piccolox-1.2.jar:. com/google/scrollview/ui/SVAbstractMenuItem.java
what i found worked after many trials was an escaped backslash at the start of the path only
and semicolons as separators.
export CLASSPATH=C:\\Users/username/bla/bla.jar;C:\\Users/username/bla/bla.jar

Javac command in cmd

I have problems compiling Java programs from command line interfaces (both command prompt and NetBeans terminal).
I added the jdk directory to the PATH system variable but not all commands are recognizable, some commands (in the jdk1.6.0_37\bin folder) are recognized (like: java, javaw, packger)and others I'd have to type the full directory name for it to work (like: jar, javac, javah, javap) (this applies for both cmd and NB).
I don't know why this should be, all of these files are .exe , all are Java Platform SE binary, the only difference I can see is the icon of the files, the ones that work have the Java logo (Coffee Mug) and the rest have the standard .exe logo. Any help would be appreciated. Thanks
You have to add the jdk1.6.0_37\bin directory to the PATH variable. That is where all the commands that you want to run are in. After you add the directory to the PATH variable, open a new command prompt and run those commands again.
I'm searched many answers that suggest me to type in cmd:
set path = "%path%;c:program files\java\jdk1.7.0\bin"
but this is WRONG!
the right solution is that you leave "set" and just type
path = %path%;c:program files\java\jdk1.7.0\bin
P/s: of course you have to replace "jdk1.7.0" folder by your current java version folder

Categories