calling a java program from CentOS 7 terminal - java

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

Related

"File Not found" while trying to execute java file on MacBook Pro

Am trying to execute a java method, am having my java first.java which I compiled using javac first.java, After which a class file got created in the same path but when I tried executing the class file using command javac first.class it throws error saying file not found. Any idea by the way I have both JDK and JRE in the path variable. Am using java 1.8
Use java first to execute, javac is for compile.

Unable to find a java runtime

I got an error when I run a file exe.
unable to find a java runtime, please download and install a java runtime from http://www.borland.com/jbuilder/resources/javaruntime.html
I use Java 1.8.0_151.
To fix it, I know I can run: javaw -jar exe_name class_name in cmd.
Ex: javaw -jar soflW.exe org.jgraph.JGraphad, but I don't know how to find the class name?
I have found a solution to my problem.
To find class name: I need zip .exe file and open it by winzip, then open file manifest.mf and you can see class name.
In my case, I need run with Java 7 because Java 8 hasn't this class and use java instead of javax as I said.
In other cases, I can run with Java 8 and use javax. I have not tried with java yet.

Failed to run java in remote command line with external jar file

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

Error compiling Java from command line

I am doing a Friend Suggester application in Java using Hadoop 2.7.2 (latest atm) and I just finished the code. I want to compile the .java file and create the .class files but when I run the command
javac -d "/home/gramanas/" -cp "/srv/hadoop/share/hadoop/common/hadoop-common-2.7.2.jar:/srv/hadoop/share/hadoop/mapreduce/hadoop-mapreduce-client-core-2.7.2.jar:/usr/lib/jvm/java-1.7.0-openjdk-amd64" FriendSuggest.java
I get the following error:
FriendSuggest.java:63: error:
error while writing FriendMapper: /home/gramanas/FriendSuggest$FriendMapper.class
public static class FriendMapper
^
I don't think it has something to do with the code since it compiles normally in IDEA. I want to compile it from command line but I can't get it to work.
What is the problem? How can I fix it?
Additional Info:
Ubuntu 16.04
Hadoop 2.7.2 pseudo distributed mode
java-1.7.0-openjdk-amd64
That type of error might be caused by not having writing permissions on the path you are executing the compilation.
You should try to compile where/when you are sure to have such permissions.

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

Categories