Trouble running something from the console - java

I just installed Ubuntu on my pc and I have trouble running my code. I installed eclipse and the latest java jdk. I can compile the with the following command:
javac -cp .:Downloads/jsoup1.8.2.jar workspace/Währungsrechner/src/Crawl.java
but when I try to run it by using the command:
java -cp .:Downloads/jsoup1.8.2.jar workspace/Währungsrechner/src/Crawl
it says that the Class cannot be found and yes I have a method called public static void main(String args[]).

You need to specifiy the class to run when executing the command.
Use java -cp . Downloads/jsoup1.8.2.jar workspace/Währungsrechner/src/Crawl.
By the way, specifying "-cp ." is unnecessary when running a class, so you can just use java Downloads/jsoup1.8.2.jar workspace/Währungsrechner/src/Crawl

Related

How to solve VScode UnsupportedClassVersionError?

Problem
When i run my HelloWorld, it returns:
"java.lang.UnsupportedClassVersionError: HelloWorld has been compiled
by a more recent version of the Java Runtime (class file version
52.65535), this version of the Java Runtime only recognizes class file versions up to 52.0 "
How do I solve this problem?
Screenshot
Code
public class HelloWorld{
public static void main(String[] args) {
System.out.println("123");
}
}
The issue is connected with Java Debugger extension. I had the same problem and it has been logged in issue log: https://github.com/Microsoft/vscode-java-debug/issues/555
Problem is only with single file applications like HelloWorld. It is ok if you run maven project.
Medsonk's instruction worked for me: https://github.com/Microsoft/vscode-java-debug/issues/555#issuecomment-478464496
summary:
1. make sure uninstall jdk8 clean
2. install jdk11
3. add "vmArgs": "--enable-preview" in launch.json
4. F1, "Java: Clean ……" and "Java: Force ……"
5. run standalone file again
My situation is version conflict between java and javac on Ubuntu;
just run this code:
sudo update-alternatives --config javac

Error: Main class could not be found or loaded

I am new to Java and I want to run a simple test-program on an Ubuntu server via the command line. "Hello World" worked well.
But now I want to add jar-files to my test program. So I followed this simple tutorial.
I created a file "MyTest.java" like this:
import org.apache.commons.lang3.*;
public class MyTest
{
public static void main (String[] args)
{
// Print Hello World!
String x = "Hello World!";
System.out.println(StringUtils.capitalize(x));
}
}
Compiling the java-file with
javac -cp jars/commons-lang3-3.7.jar MyTest.java
worked well without errors and a MyTest.class() file is generated. But when I want to run the test-program with
java -cp jars/commons-lang3-3.7.jar MyTest
I end up with the following Error:
Error: Main class MyTest could not be found or loaded
What am I missing? Why is the compiler successful but yet the program unable to be executed?
EDIT:
To make it clear: I do NOT want to build a jar file (at least not yet). I just want to build and run a simple java program referencing a jar-file i downloaded from the internet. Like described here: https://www.programcreek.com/2014/01/compile-and-run-java-in-command-line-with-external-jars/
Java Version:
> java -version
openjdk version "1.8.0_151"
OpenJDK Runtime Environment (build 1.8.0_151-8u151-b12-0ubuntu0.16.04.2-b12)
OpenJDK 64-Bit Server VM (build 25.151-b12, mixed mode)
JAVAC Version:
> javac -version
javac 1.8.0_151
You forgot to specify "MainClass" to the Manifest.mf
That should be in the last step of Eclipse Jar Export Wizard.
Tell me if you're on another IDE or using Maven, that changes the procedure.
Your current command should run successfully.
The solution was to include the current directory.
java -cp jars/commons-lang3-3.7.jar:. MyTest
Thought that would happen automatically. But the dot at the end of the -cp argument was necessary.

Why can I not execute my java project from a script?

I am working on a project that has many module jar files, one of which conains my main class; I am trying to write a shell script that will setup the class path and then start the application; here is my script.
#!/bin/sh
java -cp "modules/*;lib/*" com.example.Launcher
In this example, com.example.Launcher is the class that contains public static void main(String[] args)...
The issue that I am facing is that when executing my script by ./myscript I am give the output
Error: Could not find or load main class com.example.Launcher
This would be simple, there is something wrong with the classpath right?
But if directly from the command line I execute
java -cp "modules/*;lib/*" com.example.Launcher
the exact same command from the script, in the directory the script resides, everything works just fine.
Any thoughts?
side note
I am running this via CygWin
another side note
This might be an issue with sh in CygWin. I coppied this build to a CentOS machine and tried executing it, changing the ; to a : allowed for execution from the script.
The issue persists on my window machine even with the change.
a third sidenote
It would again appear that the issue is with sh in CygWin, my final solution was this:
launcher.sh will invoke java using a : in the classpath
launcher.bat will invoke java using a ; in the classpath
On Unix-like systems the seperator is a : (not a ;). Change
java -cp "modules/*;lib/*" com.example.Launcher
to
java -cp "modules/*:lib/*" com.example.Launcher

Java code not running?

I've been trying to compile java programs, and every single time these errors show up:
I'm compiling using cmd, and javac test.java works fine, returning no errors, but java test is resulting in the string of errors. What is the problem?
EDIT:
Here is the result of javac -version and java -version.
wrong name: Test
Your Test class is defined in a package but you didn't provide its package name on the command line.
To run a java program from the command-line, you need to consider the following:
Specify the fully qualified name of the main class you want to run, i.e.:
java com.somepackage.MainClass
Add the main class plus any dependent classes/jars to the classpath.
See: Setting the Class Path.
Try running these commands with cmd:
javac -version
java -version
If one of them is not set properly you won't see your JDK version so you have to set right your environment variables.

Getting error "cannot find or load main class HelloWorld"

I have this simple code:
public class HelloWorld{
public static void main(String[] args){
System.out.println("HelloWorld");
}
}
And the filename HelloWorld.java
In command prompt, I type in:
javac HelloWorld.java
java HelloWorld
(same directory)
I am getting the error: "cannot find or load main class HelloWorld"
I'm sure it has nothing to do with improper installation because I reinstalled jdk and jre twice.
Edit:
This was working before, and the next day, no change of code, directory, or anything, its started giving an error.
You could get this behaviour if you have an incorrect / inappropriate setting of the CLASSPATH environment variable; e.g. the current directory isn't on the classpath. (It is by default ... )
Try this:
java -classpath . HelloWorld
Assuming that works ... the problem is your understanding of the concept of the "classpath". This is explained well by the Oracle documentation:
The java & javac command documentation ... particular the -classpath argument
Setting the Classpath.
The Java Tutorial - PATH and CLASSPATH
try:
java -cp "C:\WhatEverDirectoryYourFileIsIn" HelloWorld
In CMD, instead of typing:
java HelloWorld
Try typing:
java HelloWorld.class

Categories

Resources