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
Related
I want to run my Java code in remote server with external jar file. I referred to this tutorial and run IntelliJ IDE to generate a jar file of my whole project. Now I can run the following code in local terminal successfully.
javac -cp /Users/natsuapo/out/artifacts/main_jar/main.jar: new_server_try.java
java -cp /Users/natsuapo/out/artifacts/main_jar/main.jar: new_server_try
The code will run successfully. However, when I try the same thing in server. The compile process can be finished without any errors or warnings.
javac -cp /Users/natsuapo/out/artifacts/main_jar/main.jar: new_server_try.java
I have checked that the class file new_server_try.class is generated in the directory.
However the second step will return error as Could not find or load main class new_server_try and I am not sure why this happens.
on the second command try giving the full package name .. like shown below
java -cp "/Users/natsuapo/out/artifacts/main_jar/main.jar:lib/*" my.package.MainClass
also with operating system the command differs, check below
Windows
java -cp "Test.jar;lib/*" my.package.MainClass
Unix
java -cp "Test.jar:lib/*" my.package.MainClass
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
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.
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.
this is the command.
java -cp clojure.jar;sum.jar CalculateSum
sum.jar is a jar file made from clojure and java code.
CalculateSum is file which contains main method of java.
error from cygwin
can't execute binary file, Error 126
Cygwin provides you with a *nix environment within Windows, so that you might have to change the classpath separator to colons:
java -cp clojure.jar:sum.jar CalculateSum
Try:
java -cp clojure.jar:sum.jar:. CalculateSum
If you execute in the place you have the root of packages for CalculateSum.class
java -cp "clojure.jar;sum.jar" CalculateSum this is working