Here's a simple program that I try to run:
public class TutorialExample {
public static void main(String[] args) {
boolean example = true;
assert example == true;
}
}
I have tried to follow these steps exactly for this common issue, but am unsuccessful. I still get the resulting error that reads:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
assert cannot be resolved to a type
Duplicate local variable example
Syntax error on token "==", = expected
Here is a sample image of my run configurations with the VM argument -ea included.
The Run Configuration only sets options for when you run your code. You have a problem with compiling the code.
Open the Preferences and go to the 'Java > Compiler' page. Check that the 'Compiler compliance level' is set the same level as the Java release you are running (but must be at least 1.4 for assert).
It is also possible to have Java settings specific to a project so also check the project 'Java Compiler' page in the Properties for the the project.
Related
On Windows 10 I downloaded and installed Eclipse Oxygen [Release (4.7.1a)
Build id: 20171005-1200] using the Eclipse Installer.
I then created a trivial Java project (no module stuff) using the New Project wizard:
public class Demo1 {
public static void main(String[] args) {
new Demo1().test(0L);
}
void test(int i) {
doStuff();
}
void test(long l) {
doStuff();
}
void doStuff() {
String s = "abcde";
s = s.substring(2,4);
System.out.print("s=");
System.out.println( s.toString());
}
}
It runs OK, and if I do CTL=>Open Implementation for test() or doStuff() or System or String or toString() or substr() everything is fine.
However, for println() and print() an error message is displayed:
Also, a stack trace is written to the Error Log:
!ENTRY org.eclipse.jdt.ui 4 0 2017-10-23 01:42:54.695 !MESSAGE An
error occurred while searching for implementations of 'print'. See
error log for details. !STACK 1 Java Model Exception: Java Model
Status [ is not on its project's build path] at
org.eclipse.jdt.internal.core.JavaElement.newJavaModelException(JavaElement.java:570)
at
org.eclipse.jdt.internal.core.Openable.generateInfos(Openable.java:247)
at
org.eclipse.jdt.internal.core.Openable.openAncestors(Openable.java:505)
at
org.eclipse.jdt.internal.core.Openable.generateInfos(Openable.java:241)
at
org.eclipse.jdt.internal.core.Openable.openAncestors(Openable.java:505)
at
org.eclipse.jdt.internal.core.Openable.generateInfos(Openable.java:241)
at
org.eclipse.jdt.internal.core.SourceRefElement.generateInfos(SourceRefElement.java:107)
at
org.eclipse.jdt.internal.core.JavaElement.openWhenClosed(JavaElement.java:583)
at
org.eclipse.jdt.internal.core.BinaryType.getElementInfo(BinaryType.java:287)
at
org.eclipse.jdt.internal.core.JavaElement.getElementInfo(JavaElement.java:306)
at
org.eclipse.jdt.internal.core.BinaryType.isInterface(BinaryType.java:725)
at
org.eclipse.jdt.internal.ui.javaeditor.JavaElementImplementationHyperlink$1.run(JavaElementImplementationHyperlink.java:237)
at
org.eclipse.jface.operation.ModalContext$ModalContextThread.run(ModalContext.java:119)
!SUBENTRY 1 org.eclipse.jdt.core 4 1006 2017-10-23 01:42:54.695
!MESSAGE is not on its project's build path
However, after clicking OK, the Implementation popup is displayed as expected, and clicking PrintStream sends me to the correct method implementation:
This looks like a bug in Eclipse or its installer, but the issue is very basic yet I don't see any reports for it. I have a few questions arising:
Does anyone else get this issue, or is it working?
Is there any configuration I could/should do to fix this?
Any ideas why Open Implementation would be giving the error only for print() and println()?
This seems to be a bug in the current implementation as pointed out by #ZhekaKozlov and from another thread where #Stephan has shared a work in progress documentation for the future release's New & Noteworthy for Photon M3 states that:-
Note: It is not mandatory to run Eclipse with Java Runtime 9 to get the Java 9 support. However, a Java runtime 9 is required to be on
a project's build path to compile a modular project against the system
modules.
When a Java Runtime 9 is added to a project's build path, the system modules are listed under the System library in the package explorer:
I am assuming the --add-modules=ALL-SYSTEM was solving this while users were appending this as a -vmargs arg to eclipse.ini.
I'm trying to install TensorFlow for Java on Windows 10 using this Article
. I followed the steps carefully but the windows commands didn't work with me so I decided to do it manually.
The first command is to make the .jar part of the classpath and I did it manually
but the second step was to ensure that the following two files are available to the JVM: the .jar file and the extracted JNI library
but I don't know how to do that manually
The code:
package securityapplication;
import org.tensorflow.TensorFlow;
import org.tensorflow.Graph;
import org.tensorflow.Session;
import org.tensorflow.Tensor;
public class SecurityApplication {
public static void main(String[] args) throws Exception {
try (Graph g = new Graph()) {
final String value = "Hello from " + TensorFlow.version();
// Construct the computation graph with a single operation, a constant
// named "MyConst" with a value "value".
try (Tensor t = Tensor.create(value.getBytes("UTF-8"))) {
// The Java API doesn't yet include convenience functions for adding operations.
g.opBuilder("Const", "MyConst").setAttr("dtype", t.dataType()).setAttr("value", t).build();
}
// Execute the "MyConst" operation in a Session.
try (Session s = new Session(g);
Tensor output = s.runner().fetch("MyConst").run().get(0)) {
System.out.println(new String(output.bytesValue(), "UTF-8"));
}
}
}
}
could someone help? cuz my program that uses TensorFlow still have the following error
The text in the image is :
Exception in thread "main" java.lang.UnsatisfiedLinkError: Cannot find TensorFlow native library for OS: windows, architecture: x86. See https://github.com/tensorflow/tensorflow/tree/master/tensorflow/java/README.md for possible solutions (such as building the library from source). Additional information on attempts to find the native library can be obtained by adding org.tensorflow.NativeLibrary.DEBUG=1 to the system properties of the JVM.
at org.tensorflow.NativeLibrary.load(NativeLibrary.java:66)
at org.tensorflow.NativeLibrary.load(NativeLibrary.java:66)
at org.tensorflow.TensorFlow.init(TensorFlow.java:36)
at org.tensorflow.TensorFlow.<clinit>(TensorFlow.java:40)
at org.tensorflow.Graph.<clinit>(Graph.java:194)
at securityapplication.SecurityApplication.main(SecurityApplication.java:15) Java Result: 1 BUILD SUCCESSFUL (total time: 4 seconds)
The result after running the first command in cmd:
The result after running the second command in Windows PowerShell:
Any suggestions?!
Thank you
The first command failure (javac) suggests that the javac command is not in your PATH environment variables. See for example, this StackOverflow question
For the second command failure, I believe the space after -D is what is causing you trouble as Holger suggested.
IDEs like Eclipse and others also provide a means to set the java.library.path property for the JVM (see this StackOverflow answer for example).
Background: TensorFlow for Java consists of a Java library (packaged in a .jar file) and a native library (.dll on Windows, distributed in a .zip file). You need to ensure that the .jar file is in the classpath and the directory containing the .dll is in included in the java.library.path of the JVM when executing a program.
Hope that helps.
I run in command line the following program as an example app:
java -cp "D:\projects\PDFJavaFX\lib\PDFRenderer-0.9.1.jar"
com/sun/pdfview/PDFViewer
Then I run in command line the JDI trace example:
java -cp "C:\Program
Files\Java\jdk1.7.0_13\lib\tools.jar;D:\projects\JDI_Trace\jar\trace.jar;D:\projects\PDFJavaFX\lib\PDFRenderer-0.9.1.jar"
com/sun/tools/example/trace/Trace com/sun/pdfview/PDFViewer
I get this error:
Error: Could not find or load main class com.sun.pdfview.PDFViewer
-- VM Started --
-- The application exited --
The example app runs correctly, and it is included in the classpath.
What's the cause of this?
What am I missing?
Thanks
Edit: It looks like it is classpath related.
I did get this to work (well, it popped up the GUI but then crashed pretty quickly). I used the classpath environment variable instead of -cp:
C:\cos126\dev\debug>set CLASSPATH=%CLASSPATH%;c:\tmp\PDFRenderer-0.9.1.jar;c:\tmp\debug
So, not pretty, but then it did work. So it looks like the newly created VM doesn't automatically inherit -cp. I am optimistic, but not sure, that there might be an option you can change when starting the new VM to do this for you. To see the "documentation" for the VM launching options, you can add some code like
for (Map.Entry<String, Connector.Argument> arg : arguments.entrySet()) {
System.out.println(arg.getValue().name()+" "+arg.getValue().description());
}
to Trace.java. When I do this, it prints out
home Home directory of the SDK or runtime environment used to launch the application
options Launched VM options
main Main class and arguments, or if -jar is an option, the main jar file and arguments
suspend All threads will be suspended before execution of main
quote Character used to combine space-delimited text into a single command line argument
vmexec Name of the Java VM launcher
so maybe one of those is useful? Good luck!
By the way, this is what I used JDI for:
http://cscircles.cemc.uwaterloo.ca/java-visualize/
I am in the process of making the source shareable, if you want to see it (although I'm not 100% sure it will be of use).
Your command :
java -cp "C:\Program Files\Java\jdk1.7.0_13\lib\tools.jar;
D:\projects\JDI_Trace\jar\trace.jar;
D:\projects\PDFJavaFX\lib\PDFRenderer-0.9.1.jar"
com/sun/tools/example/trace/Trace com/sun/pdfview/PDFViewer
Explanation :
The new VM which is created by the Trace has different class path. main class PDFViewer is in the PDFRenderer**.jar,but the new VM didn't know the jar, so it can't find the main class. I also met this problem when I used Eclipse. And by changing the working directory, I can run it successfully.
In fact, the Trace class uses JDI to launch the new VM, but it only set the main option and discard the vm options. The code below is quoted from the Thrace class source file, and I add some lines to print the options.
Map<String, Connector.Argument> connectorArguments(LaunchingConnector connector, String mainArgs) {
Map<String, Connector.Argument> arguments = connector.defaultArguments();
Connector.Argument mainArg =
(Connector.Argument)arguments.get("main");
//added by me: begin
java.util.Set<String> argsString = arguments.keySet();
System.out.println("connector args size is :" + argsString.size());
for (String arg : argsString) {
System.out.println(arg + "=="+ arguments.get(arg).description()+"=="+arguments.get(arg).value()) ;
//added by me: end
}
if (mainArg == null) {
throw new Error("Bad launching connector");
}
mainArg.setValue(mainArgs);
The output of the arguments size is 6, and they are "home, options, main, suspend, quote and vmexec". If we want to configure the new VM options, we can set the "options" by setValue method like setting "main".
I'm learning Java am having trouble running an example program.
I have two files:
GoodDog.java:
class GoodDog {
private int size;
public int getSize() {
return size;
}
public void setSize(int s) {
size = s;
}
void bark() {
if (size > 60) {
System.out.println("Wooof! WoooF!");
} else if (size > 14) {
System.out.println("Ruff! Ruff!");
} else {
System.out.println("Yip! Yip!");
}
}
}
GoodDogTestDrive.java:
class GoodDogTestDrive {
public static void main (String[] args) {
GoodDog one = new GoodDog();
one.setSize(70);
GoodDog two = new GoodDog();
two.setSize(8);
System.out.println("Dog one: " + one.getSize () );
System.out.println("Dog two: " + two.getSize () );
one.bark();
two.bark();
}
}
They are typed out exactly the way they are in the book and compile without issue. When I try to run GoodDogTestDrive I get this:
nephi-shields-mac-mini:/Developer/MyProjects/GoodDog nephishields$ java GoodDogTestDrive.class
java.lang.NoClassDefFoundError: GoodDogTestDrive/class
Exception in thread "main" nephi-shields-mac-mini:/Developer/MyProjects/GoodDog nephishields$
What am I doing wrong?
Don't include the .class in the command:
java GoodDogTestDrive
It is important to note that while the resolution was simple for this problem case (simply removing .class from the Java command line), java.lang.NoClassDefFoundError can be hard to pinpoint in the context of Java Web application developments.
This Java exception means that a “runtime” Java class could not be loaded and/or found in the current Thread context classloader. This is normally the results of:
Missing library in your runtime classpath.
Static {} block code initializer execution failure (preventing class loading of the affected class).
JAR file packaging / class loader tree problems such as mix of JAR files deployed at the child & parent class loader.
It is recommended for any Java beginner to properly understand this type of problem in anticipation of future troubleshooting episodes.
just run the program with "java yourClass".
Do not use .class in the end.
Try it and let us know.
If the GoodDog class file is not located at the current working directory you will need to set the classpath on your java command....
java GoodDogTestDrive -cp ./path/to/gooddog/
Issue-java.lang.NoClassDefFoundError
Root Cause: Incorrect Java path set in Environment Variable Section
Solution: Set correct JAVA_HOME Path
Steps->Environment Variable Setting (My Comp-Right Click ->Properties->Env Variable->Advance Tab ->Variable)
Create new JAVA_HOME Environment Variable.
JAVA_HOME **.;C:\Program Files (x86)\Java\jdk1.6.0_14**
Set JAVA_HOME variable in PATH Variable section.
PATH %JAVA_HOME%\bin
Set JAVA_HOME variable in CLASSPATH Variable
CLASSPATH %JAVA_HOME%\jre\lib
Restart System
Verify all variable
echo %CLASSPATH%
echo %JAVA_HOME%
echo %PATH%
Compile java class javac Test.java
Run Java program java Test
Thrown if the JVM or a ClassLoader instance tries to load in the definition of a class (method call or creating a new instance) and no definition of the class could be found.
The reason for NoClassDefFoundError is that a particular class is not available on runtime but was available during compile time.
1.The class belongs to a missing JAR or JAVA file or JAR was not added into classpath.
2.Class is not available in Java Classpath.
3.NoClassDefFoundError in Java due to Exception in Static Initializer block. when your class perform some static initialization in static
block, and if static block throw an Exception, the class which is
referring to this class will get NoclassDefFoundError in Java.
4.Your Classpath, PATH or JAVA_HOME is not setup properly or JDK installation is not correct. which can be resolved by re-installing JDK.
5.Do a maven clean-install and simply restart your server with necessary Source(.java) files.
I was facing the same problem as you.
I resolved the problem like this:
Check the class path in evironment variables
I opened the project in navigator of eclipse and I checked the class files. My class files were in different folders not in the bin folder. I just copied the missing files into the bin folder then the problem was resolved.
Simply copying the missing .class files to bin directory of eclipse project resolved the problem.
HI All,
I got an issue, all of a sudden Java stopped working completely. I start getting error like "Could not create the virtual machine". There is no issue with the memory (it has 3GB RAM) and was working fine for over a 6 months in this system without any issue.
Here are some peculiar behaviors -
When I start eclipse I see Java virtual machine dialog box with error messages like
"Could not find main class org.eclipse......support.legacysystemproperties"
Eclipse is able to start(with above error), but while running the program, I get error like "Could not create Java Virtual Machine" in a dialog box and after I click OK on that dialog box, I see error like "unrecognized option -dfile.encoding=cp1252
I used text editor, wrote a class Test.java (without any package), compiled it (Edit #1:javac Test.java). But when I execute the program (Edit #1:java Test), I get the following error -
Exception in thread "main" java.lang.NoClassDefFoundError: test (wrong name: Test).
Edit #1:
Note : The compiled file, Test.class is successfully created in the directory. I did recheck the path and classpath environment variables. All seem to be correct.
Please note that there seems to be some issues with cases which affected the Java.
I did uninstall Java (all versions), re-installed, but nothing helped. Also, I did run CCleaner to clean registry, Malwarebytes' Anti-Malware, but none helped so far.
Appreciate if someone could help me to resolve the issue.
I did googled for this and found that some have experienced similar issues, but none of them have found solution yet other than some suggestion that re-installation of Windows OS itself, which I want to avoid it. I did system restore, but that failed for some other
reason.
Please note that am using Java for over 10 years. This is first time am having such issue. This is something to do with Windows Registry or some other system configuration, but I am not able to find out the exact problem.
Anyways awaiting some good suggestion.
EDIT: Okay, so it looks like the Java executable is getting the command line arguments lower-cased.
Step 1: Verify
You can double-check whether this affects all command line arguments by creating a class with a lower-case name which just dumps its arguments:
public class test {
public static void main(String[] args) {
for (String arg : args) {
System.out.println(arg);
}
}
}
Compile and run this with a variety of inputs.
Step 2: Check scope
Assuming step 1 confirms the problem, if you've got .NET installed you can see whether it affects that as well. Create a file Test.cs:
using System;
class Test
{
static void Main(string[] args)
{
foreach (string arg in args)
{
Console.WriteLine(arg);
}
}
}
Compile this with "csc Test.cs" having found csc in the .NET framework directory (e.g. in c:\Windows\Microsoft.NET\Framework\v4.0.30319 for .NET 4).
Run it like this:
Test foo BAR Baz
and see what happens
Step 3: If step 2 showed that the issue is limited to java.exe:
Check your path, and work out where you're actually running java.exe from
Try explicitly running java.exe from your JRE or JDK directory