I'm trying to compile a java program that uses multiple .jar files but am experiencing compile problems. I also can't copy the full output from the virtual machine, but i'll try to post the relevant information.
javac -g Model.java
This ends up getting 33 errors (GL11 cannot find symbol)
javac -g -classpath /media/sf_vm_source/java/java_pkg/lwjgl-*/jar/lwjgl.jar Model.java
This ends up with 5 errors (GL11 is found) [5 errors are from other jars but i'm trying to get it to work with 2 .jars first]
javac -g -classpath /media/sf_vm_source/java/java_pkg/lwjgl-*/jar/lwjgl.jar:/media/sf_vm_source/java/java_pkg/lwjgl-*/jar/lwjgl_util.jar Model.java
This ends up with 33 errors again (GL11 cannot find symbol)
Other info:
sf_vm_source is auto-mounted by virtualbox and has 777 permissions recursively including /media.
Moving all the jar files to a local lib file and using wildcards removes the compile error, but of course has its own issues.
If you are using the relative path in your classpath, then you may have to add the current path(using a dot(.)) to all the jars path. Something like this:
javac -g -classpath ./media/sf_vm_source/java/java_pkg/lwjgl-*/jar/lwjgl.jar Model.java
Also the use of wildcard lwjgl-* may not work here
Related
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.
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
How can I add two or more jar file in the compile step using cmd?
using one jar file:
javac -g -cp YOUR_JAR.jar YOUR_FILE_NAME.java
Any ideas how to compile two jar files?
You can do that using the -classpath flag.
javac -classpath your.jar:my.jar ...
The delimiter between jars changes according to your platform.
You can read about that by running javac -help or reading the javac documentation online and About Setting the Class Path.
You will notice there that the documentation says:
Multiple path entries are separated by semicolons with no spaces
around the equals sign (=) in Windows and colons in Oracle Solaris.
So, all Xnix operating systems use a : as delimiter, whereas Windows use a ;.
As far as I understand, your question is about using multiple jars as the classpath when compiling java source code.
To use multiple jar/classpath files when compiling, you should separate them by your target platforms path separator, this is ';' on windows, and ':' on linux, example:
javac -g -cp FIRST.jar;SECOND.jar MY_FILE_NAME.java (windows)
javac -g -cp FIRST.jar:SECOND.jar MY_FILE_NAME.java (linux)
Sources: Including jars in classpath on commandline (javac or apt) (java and javac have the same classpath parsing
I am using Ubuntu Linux 32bit system, in terminal it shows no class specified
sanidhya09#sanidhya09:~/workspace/NdkFooActivity$ javah -jni
-classpath adt-bundle-linux-x86/sdk/platforms/android-4.2/android.jar;bin/classes/
-d jni/ com.example.NdkFooactivity Error: no classes specified bash: bin/classes/: Is a directory
even i have tried this
sanidhya09#sanidhya09:~/workspace/NdkFooActivity/bin/classes$ javah
-jni -classpath adt-bundle-linux-x86/sdk/platforms/android-4.2/android.jar; com.example.NdkFooactivity.NdkFooActivity Error: no classes specified com.example.NdkFooactivity.NdkFooActivity: command not found
even i tried External tools configuration
where in argument section i wrote
-d /home/sanidhya09/workspace/NdkFooActivity/jni -classpath /home/sanidhya09/adt-bundle-linux-x86/sdk/platforms/android-4.2/android.jar:/home/sanidhya09/workspace/NdkFooActivity/bin/classes com.example.NdkFooactivity
even in eclipse i get error saying
make:
[HelloJni] Error 127 HelloJni C/C++ Problem
sanidhya09#sanidhya09:~/workspace/NdkFooActivity$ javah -jni -classpath bin/classes:~/adt-bundle-linux-x86/sdk/platforms/android-16/android.jar com.example.NdkFooactivity.NdkFooActivity
should be fine, assuming your app package is com.example.NdkFooactivity and the class you are interested in is NdkFooActivity.
Note that you must pass the path to android.jar you actually used to compile. In my SDK, the directory is called android-16, not android-4.2. Also, you need to have the bin/classes directory in classpath. You may need more jars in your classpath.
But there is one more thing that I would like to add. (And which has taken up most of my morning solving that)
While specifying the classpath = Refrain from using ./bin/classes:~/ProjectFolder/bin/classes ( the tilde representing the Home directory)
But use the "../../" to switch directories. For some reason javah doesn't recognizes the tilde operator for the home directory.
Also one other thing if the class has a dependency on other project then you would need that as well in the classpath
user#laptop:~/SomeProject javah -classpath ./bin/classes:../<Whereever the path is>/SomeOtherDependentProject:../<Path to android-sdk>/android.jar <Qualified class path>
I'm building with Maven and getting a seemingly innocuous "symbol not found" error - problem is I've verified that the build is correct with all the lower level Java tools I know and it looks airtight. I have built with this command line:
javac -d /home/dan/EmailClient/Initial-Client/target/classes -classpath <many jars>:
/thepath/Common/1.0-SNAPSHOT/Common-1.0-SNAPSHOT.jar /* I need this one */
-sourcepath /thepath/src/main/java: <the files>
-s /thepath/target/generated-sources/annotations
-g -nowarn -target 1.7 -source 1.7 -encoding UTF-8
Get this error:
Launcher.java:119: error: cannot find symbol
return Promise.onPool(getterPool).continueWith(new Transformer<Void, Promise<List<Void>>>() {
(with error pointing on onPool)
And have run jar xf Common-1.0-SNAPSHOT.jar (cding by copying and pasting the directory in the command line), then
javap Promise.class
which shows
public static me.unroll.functional.Promise<java.lang.Void> onPool(java.util.concurrent.Executor);
So this looks airtight to me - what debug step am I missing?
Furthermore this builds fine on a separate machine and runs fine from Eclipse with all relevant source files opened, just not from my actual build machine.
I suggest you to inspect the <many jars> section in your classpath, you are likely to find an older version of your Maven-built jar in there, which is winning the classname resolution game.