Running a java program using -cp - java

so I've checked many posts here and tried everything people suggested and I'm still getting "Could not find or load main class" error, and my professor wasn't very helpful in his answer when I asked for help.
This is what my professor said is supposed to work (he is using OSX).
java -cp classes:lib/json.jar cst420.jsonrpc.client.GroupServerJavaStub http://127.0.0.1:8080
After reading some other posts on this site, I also tried:
java -cp classes:"lib/json.jar" cst420.jsonrpc.client.GroupServerJavaStub http://127.0.0.1:8080
java -cp "lib/json.jar" cst420.jsonrpc.client.GroupServerJavaStub http://127.0.0.1:8080
Still nothing works. I'm in the base directory in a bash prompt ~\GroupJsonRPC and the class file that is in ~\GroupJsonRPC\classes\cst420\jsonrpc\client\GroupServerJavaStub.class exists and is ready for running. The same goes for ~\GroupJsonRPC\lib\json.jar. Any insight into how to get this darn thing running would be greatly appreciated!
PS: I'm using Windows.

The proper command is:
java -cp "classes;lib/json.jar" cst420.jsonrpc.client.GroupServerJavaStub http://127.0.0.1:8080
You need to be sure to include the directory of your class files as well as the jar file(s) with ';' separators, and don't use the -jar option. It invalidates the -cp option.

Related

Javac will not run despite Java being installed

While I have some experience in Python and JavaScript, I am new to Java and am trying it out for the first time. To get started, I went to http://www.java.com, downloaded the dmg there, and then used the package it gave me to install Java. After doing so, I received confirmation that Java had been installed successfully and I closed and trashed the dmg and package. Afterwards, I hopped on my terminal (I am using a Mac running on MacOS Monterey) and tried to use javac on a script I wrote. I received the following error message:
The operation couldn’t be completed. Unable to locate a Java Runtime that supports javac.
Please visit http://www.java.com for information on installing Java.
I went online and did some typical troubleshooting searches. First, I found a site suggesting that I create an environment variable $JAVA_HOME and set it equal to $(/usr/libexec/java_home) in .zshenv (yes, I use zsh not bash). I followed this instruction and when I run echo $JAVA_HOME I get /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home. However, running javac still did not work.
Once more, I went online and this time, I found the suggestion of adding javac to $PATH. So I went into .zshenv and added usr/bin/javac to $PATH (the $PATH export line now looks like this: export PATH="/Users/[redacted]/Library/Python/3.8/bin:/usr/bin/javac:$PATH"). This alteration was confirmed when I exited an reentered terminal and ran echo $PATH. However, once again, running javac yielded the same error.
I feel as if I am facing what must be a pretty common and easily fixable issue, but yet, I haven't yet been able to find a solution that works despite perusal of several other StackOverflow posts and tech articles. Still, I know I'm probably missing something simple, and if this is a duplicate of another question, please link that question in a comment, and I'll take this one down.
Thanks!

JDBC classpath issue

I don't like to post a question when so many suggestions pop up when I type the question title in but looking through them all and seeing no solution is somewhat distressing. I've been following a tutorial for java->postgres connections and I am constantly getting slaughtered by
"Could not find or load main class Seb"
error messages.
I've tried using
SET CLASSPATH=%CLASSPATH%:<path to work directory>;<path to jdbc jar>
compiling like so:-
javac -cp .:jdbc.jar Seb.java
and executing like so:-
java -cp .:jdbc.jar Seb
and can't see through the light -_-
The thing is, following the tutorial down to the letter (and watching the demonstration video) doesn't seem to work for me.
I'm simply using notepad and cmd.exe on Windows 8 for development as it's only a learning opportunity, not a big project - but I'm baffled as to why I can't get the thing to run!
JDBC postgres files are in the same directory as .java file.
Code is available if needed as is any other information I can provide.
Thanks in advance for any help,
-Tim!
Use a semi-colon classpath separator for Windows
java -cp .;jdbc.jar Seb
^
Read: PATH and CLASSPATH

Execute Java on Ubuntu: PHP exec() is not working while console works just fine

My website needs PHP to run a Java program in the background. So, PHP issues exec() method to let Java do all the work. The implementation works fine on Windows, but completely failed on Ubuntu. While exec() doesn't work, a stand-alone test with console works just fine.
I've setup a test.php to narrow down the problem:
<?php
$output = exec("java -cp ~/path/to/java/class/file/folder Hello 2>&1");
//$output = exec("whoami");
echo $output;
?>
The Hello.java is simply:
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}
By running test.php on localhost, it shows:
Error: Could not find or load main class Hello
I tried to narrow down the cause of the error, and my thought went like this:
exec() itself is problematic:
unlikely, since whoami prints out apache-user as expected.
what the error message means:
I searched about this error. Post like this one talks about it is caused by the absence of classpath. It's not the case for me either, because in console it works. So the error message means nothing (does it?)
user/group permission:
Is it possible that apache-user is not permitted to run the class file?
I checked, and found the permission code of Hello.class to be rw-r--r--, owned by apache-user:webmasters.
But, even if no one has x permission of the file, in console I can still run it (using my own user).
I'm not sure about the situation here. But my understanding is that by running java program, it is really JVM executing it (or something else); so the permission of Hello.class doesn't matter.
I found another post has a similar situation. But its solution - specifying full path to Java bin /usr/bin/java - doesn't work for me...
What is causing the error?
Can anyone help? Detailed solution is appreciated! I'm a newbie #_#
Many thanks!!!
Have you tried java -cp /path/to/folder/containing/class/file Hello 2>&1? It appears that the class file itself should not be the classpath. It should be in the classpath. If this were a .jar file, on the other hand, then you would provide the filename in the classpath.
I've solved the problem... It's quite unexpected.
I changed the classpath.
Previously it's something like ~/myproject/to/java/class/file/folder.
And I changed it to /home/myuser/myproject/to/java/class/file/folder.
But I completely don't understand why ~ notation doesn't work with exec().
Give the path and Hello.java file free.
Test the rights for the apache user with:
sudo -u webmasters java -cp /path/to/java/class/file/folder Hello
chmod a+r Hello.class

Java setting classpath in bash

I've been having some trouble setting -classpath properly to get a project to run:
What I thought I was supposed to do:
java -classpath /path/to/classes package.Main
The only thing so far that works:
cd /path/to/classes
java -classpath . package.Main
I've also tried to do -classpath /path/to/classes:.
I need to be able to run that class from different directories so I can't use the solution that did work for me.
I am obviously missing something really stupid here.
EDIT: I am using cygwin
The problem was the way classpath is handled in cygwin:
Since java is a windows program it doesn't recognize the cygwin path I gave.
The fix was:
java -classpath `cygpath -wp /path/to/classes/` package.Main
Hope it helps someone else.

Javac can't find .class files, have right classpath

Although I'm not new to programming in general, I am new to java, and it seems to work a little bit differently. When trying to compile a .java with javac it gives me this error: cannot find symbol. I looked the cause of this up, and the error occurs when the .class file that you are referencing could not be found. I ensured that all of my references were spelled correctly, I ensured I had the right classpath, I tried compiling a .java file I know is valid, even on an XP computer just in case my Vista installation was the cause, but to no avail. Here are the contents of the .bat I am using to run javac, it could be the cause of my problems.
#echo off
"C:\Program Files (x86)\Java\jdk1.6.0_21\bin\javac" -encoding UTF8 -cp . hl.java
pause
Thank you for the help, this has been troubling me for quite some time now.
Can you show us this?
I tried compiling a .java file I know is valid
Could it be that you are using classes not compiled on . ? Unless your code is as simple as hello world, I'd say chances are high.
This is a long shot, but looking at your script and the error message you described, you're probably not running javac at all. Try this on the command line and see what it says:
C:\Program Files (x86)\Java\jdk1.6.0_21\bin\javac -version
If that doesn't work, then you're not pointing to a valid javac.

Categories