Java compilation through command prompt issue - java

I tried to compile my class file "checker.class" into "checker.java" through the use of the javac command, planning to run it with the java command, but javac either gave me the issue of "file not found" OR "invalid flag" (when I tried to put checker in quotations.)
I would also like to understand the issue itself better and not just know the solution. Thanks.
What I attempted:
C:\Users\jaede\Desktop\everything>javac checker.class
C:\Users\jaede\Desktop\everything>javac "checker.class"
Results with error messages:
error: file not found: Checker.java
Usage: javac
use --help for a list of possible options
error: invalid flag: Checker.class Usage: javac
use --help for a list of possible options
planned to do:
C:\Users\jaede\Desktop\everything>java checker.java
*Sidenote: I probably do not have an environment variable issue, since I could run this with another .java files with the java command

I got .class and .java mixed up. The comments to the original post have the full rundown.

Related

What are the possible causes for javac giving me an invalid flag error?

I'm trying to compile a few java classes and they need external libraries.
I believe I have the right ones but I keep getting an invalid flag error if I plug in any of them other than one.
This is the command I'm running:
javac -d classdir -classpath "libraries\*.jar" -sourcepath src src\main\java\sc\fiji\cellCounter\*.java
and the error I'm getting is:
error: invalid flag: libraries\scijava-common-2.83.3.jar
my libraries folder has just two .jar files at the moment, ij-1.53c.jar and the one in the error above. If I remove scijava from the folder and run the same command, it runs but says its missing
a bunch of packages that start with 'org.scijava'.
What could be causing the invalid flag error?
Any other help to get this to compile would be greatly appreciated.
So, strangely enough, removing the .jar after * solved the problem.

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.

javac works on its own but doesn't work with scons

From the command line, I can do this and get expected output
c:\Users\SomeUser\SomePath\sandbox> javac Sandbox.java
If I use the SConstruct in conjunction with scons, in the same location, I get
c:\Users\SomeUser\SomePath\sandbox> scons
scons: done reading SConscript files.
scons: Building targets ...
javac -d classes -sourcepath . Sandbox.java
'javac' is not recognized as an internal or external command,
operable program or batch file.
scons: *** [classes\Sandbox.class] Error 1
scons: building terminated because of errors.
This looks like the answer to #1 of our "most frequently asked" FAQs at http://scons.org/faq.html could help you out. By default, SCons doesn't import the variables like $PATH from the surrounding shell environment. You have to pull in your $PATH for properly detecting the javac executable explicitly...check the mentioned FAQ entry for a more detailed discussion about why things are as they are, and how to provide the required $PATH definitions to your build environments.

How to compile and run HelloWorld.java example from Sphinx4 without and IDE in Linux?

I have been testing the examples (HelloWorld.java) from Sphinx4 with Eclipse, but I would like to compile and run them from the command line.
The application needs 5 .jars to be able to run, I have read that in order to compile a java class with multiple .jars I need to execute the following command (as an example I will show short names):
javac -cp one.jar:two.jar:three.jar:four.jar:five.jar HelloWorld.java
The console does not throw any error messages:
parias001#parias001-pc:~/Projects/citadel_voices/sphinx_test > javac -cp jsapi.jar:sphinx4.jar:TIDIGITS_8gau_13dCep_16k_40mel_130Hz_6800Hz.jar:WSJ_8gau_13dCep_8kHz_31mel_200Hz_3500Hz.jar:WSJ_8gau_13dCep_16k_40mel_130Hz_6800Hz.jar HelloWorld.java
parias001#parias001-pc:~/Projects/citadel_voices/sphinx_test >
I think that the compilation succeeded. Now I would like to run the application, I read that in order to do this, I have to execute the command as follows (Using short name example as before):
java -cp one.jar:two.jar:three.jar:four.jar:five.jar HelloWorld
This is the message that the console throws:
parias001#parias001-pc:~/Projects/citadel_voices/sphinx_test > java -cp jsapi.jar:sphinx4.jar:TIDIGITS_8gau_13dCep_16k_40mel_130Hz_6800Hz.jar:WSJ_8gau_13dCep_8kHz_31mel_200Hz_3500Hz.jar:WSJ_8gau_13dCep_16k_40mel_130Hz_6800Hz.jar HelloWorld
Error: Could not find or load main class HelloWorld
I don't know what is going on here, I should also say that I do not have a lot of experience using external .jars.
The names of the .jars are:
jsapi.jar
sphinx4.jar
TIDIGITS_8gau_13dCep_16k_40mel_130Hz_6800Hz.jar
WSJ_8gau_13dCep_8kHz_31mel_200Hz_3500Hz.jar
WSJ_8gau_13dCep_16k_40mel_130Hz_6800Hz.jar
I appreciate any help you can give me.
You have to include current directory in classpath:
java -cp .:one.jar:two.jar:three.jar:four.jar:five.jar HelloWorld
Note the leading .:
From this reference:
The default class path is the current directory. Setting the CLASSPATH variable or using the -classpath command-line option overrides that default, so if you want to include the current directory in the search path, you must include "." in the new settings.

calling a java program from CentOS 7 terminal

I am using the code from this tutorial to test the JDBC connection. I changed the name of the class to TestJDBC and I altered the database name and query, but otherwise it is identical. When I run the class as a Java application from within eclipse on my devbox, the program runs properly. However, when I copy the file to /home/username/ on a remote CentOS 7 server, typing java TestJDBC.java into the terminal produces the following error:
Error: Could not find or load main class TestJDBC.java
I also the same error when I try java TestJDBC and when I upload the .class file in addition to just the .java file. What else do I need to do in order to call the Java class from the CentOS 7 terminal?
Note that javac TestJDBC.java results in -bash: javac: command not found. And I did try java somepackage.TestJDBC with same results of Error: Could not find or load main class TestJDBC.java as above.
ANSWER NOTE: The answer required getting the development version of openJDK using yum. The PATH variable was not part of the solution. However, I am marking the answer below as accepted because the user who submitted it contributed substantially to the solution.
You should be able to run it after compiling it
javac TestJDBC.java
java TestJDBC
Note that you do not need to add .class when running it from the commandline.
If this still does not work, please paste your code.
EDIT after request
So you've now stated that you're missing javac from your PATH. I'll show you how to add it:
$> export JAVA_HOME=/path/to/jdk/jdk.1.8.0_20
$> export PATH=$PATH:$JAVA_HOME/bin
Verify by running
javac -version
It should print something like
javac 1.8.0_20

Categories