Unmappable character - encoding

public class MyFirstApp {
public static void main(String[] args) {
System.out.println(“I Rule!”);
System.out.println(“The World”);
}
}
When try to compile It in cmd using javac MyFirstApp.java it says that there is an unmappable character (0x9D) for encoding. What Is the problem?

The quotation marks in:
System.out.println(“I Rule!”);
System.out.println(“The World”);
are not correct. They are different for left & right. They should be plain ordinary "
System.out.println("I Rule!");
System.out.println("The World");
This type of problem is often caused by using a word processor (e.g., Word) rather than a text editor or "programmer's editor" (simplest typical example is Notepad, but there are plenty of free and low-cost alternatives that are much more powerful).

Related

Unity: Why am I getting an exception when reading a text file into a TextAsset?

I have a text file in Assets/Resources that I'm trying to read into a TextAsset. For some reason it's not being loaded, resulting in a null reference exception. This is the code:
using UnityEngine;
using System.Collections;
public class LoadTextFile : MonoBehaviour
{
public string txtFile = "4.txt";
string txtContents;
void Start ()
{
TextAsset txtAssets = (TextAsset)Resources.Load(txtFile);
txtContents = txtAssets.text;
}
The last statement is where the null exception is reported. I don't understand why the file is not being loaded (I'm assuming it isn't), but it could be something else. The file is very small, as I'm just carrying out a test. Any help would be most welcome!
The parameter to Resources.Load, should be the fileNameWithoutExtension and not fileNameWithExtension.
A note from Resources.Load link stating:
Note that the path is case insensitive and must not contain a file
extension
So seems like just change
public string txtFile = "4.txt";
to
public string txtFile = "4";
Just a word of advice, prefer not to start fileNames with digits or symbols.

Scanner with Delimiter as new line - java.util.InputMismatchException in Eclipse

I have a small program to scan an Integer from console, also I would like to use new line character as delimiter.
import java.util.Scanner;
public class TestScanner {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in).useDelimiter("\n");
System.out.println("Enter the int");
int testInt = scanner.nextInt();
System.out.println(testInt);
}
}
Intellij runs and exist normal way..
Where as Eclipse produce, java.util.InputMismatchException
In Run mode you can observe an exception, with debug mode, code will run with out any issues
Source : jdk1.8.0_161 ( Oracle )
Eclipse :
This is strange. Kindly help me to understand how this can be corrected. Thanking you!
Late but... if you're in Windows you should use "\r\n" instead of just "\n"

Eclipse VM arguments picking directory name without separator

I am setting the VM arguments in eclipse as -DFilePath="C:\file\txt"
But while calling this #FilePath# in java it is giving output as C:filetxt instead of C:\file\txt. This is resulting in file not found exception. Can anyone please help me on this..
The problem must be in how you are "calling this #FilePath#".
I tested with following code:
package test;
import java.io.File;
public class EnvPath {
public static void main(String[] args) {
String path = System.getProperty("FilePath");
System.out.println("Prop: " + path);
File file = new File(path);
System.out.println("File: " + file);
}
}
Started from Eclipse, as you described, or with java -DFilePath="C:\file\txt" test.EnvPath using Windows Command Prompt and using GNU bash - it always produces:
Prop: C:\file\txt
File: C:\file\txt

Reference to method is ambiguous in OpenJDK but not Eclipse - why?

I know that similar questions have been asked (here, here, here), but none of the answers seem to apply to my case.
Consider the following set of interfaces:
public interface I1<X> {
void method(X arg);
}
public interface I2 {
void method(String arg);
}
public interface I3 extends I1<String>, I2 {
// empty
}
Now I want to call method(String) on an instance of I3, like so:
public class C implements I3 {
public void method(String arg) {
// does nothing
}
public static void main(String[] args) {
((I3) new C()).method("arg");
}
}
The OpenJDK (Java 7 or 8, doesn't matter) flags an incompatibility error here:
generics\C.java:10: error: reference to method is ambiguous
((I3) new C()).method("arg");
^
both method method(String) in I2 and method method(X) in I1 match
where X is a type-variable:
X extends Object declared in interface I1
Since X is instantiated to String in I3, I do not see where the problem comes from. Note that Eclipse considers this to be fine.
The answer to the question, "why?" is simple but probably not what you're looking for. It is, "Because Eclipse and Open JDK each have their own compilers and either a) one of them has a bug or b) the language specification (JLS) is ambiguous and they've interpreted it differently."
Figuring out which of a) or b) is the case is a tricky, tedious task. It means, as a starting point, reading the relevant sections of the JLS, trying to compile the same code with Oracle JDK's javac, and possibly diving into the bug tracking systems of Eclipse and OpenJDK.

Inter Type Declaration on Compiled Class File

Is it possible to do Inter Type Declarations with AspectJ on Compiled Class Files at Load Time Weaving?
As an example: I compile some Groovy code and want to add fields or methods with IDT.
Update:
Oh my goodness, you do not need reflection to access members or execute methods. Eclipse shows errors in the editor, but you may just ignore them, the code compiles and runs fine anyway. So the aspect is really much more strightforward and simple:
public aspect LTWAspect {
public static String Application.staticField = "value of static field";
public String Application.normalField = "value of normal field";
public void Application.myMethod() {
System.out.println(normalField);
}
void around() : execution(void Application.main(..)) {
System.out.println("around before");
proceed();
System.out.println("around after");
System.out.println(Application.staticField);
new Application().myMethod();
}
}
Original answer:
Yes, but you have a hen-and-egg problem there, i.e. you cannot just reference the newly introduced fields from your LTW aspect code without reflection. (The last sentence is not true, see update above.) Plus, in order to make your LTW aspect compile, you need the classes to be woven on the project's build path so as to be able to reference them. Example:
Java project
public class Application {
public static void main(String[] args) {
System.out.println("main");
}
}
AspectJ project
import org.aspectj.lang.SoftException;
public aspect LTWAspect {
public static String Application.staticField = "value of static field";
public String Application.normalField = "value of normal field";
public void Application.myMethod() {
try {
System.out.println(Application.class.getDeclaredField("normalField").get(this));
} catch (Exception e) {
throw new SoftException(e);
}
}
void around() : execution(void Application.main(..)) {
System.out.println("around before");
proceed();
System.out.println("around after");
try {
System.out.println(Application.class.getDeclaredField("staticField").get(null));
Application.class.getDeclaredMethod("myMethod", null).invoke(new Application());
} catch (Exception e) {
throw new SoftException(e);
}
}
}
So, e.g. in Eclipse you need to put the Java project on the AspectJ project's build path under "Projects" because only then it can see Java class Application on which you want to declare members. After compilation you just start the Java project and do LTW on the aspect project (don't forget an aop-ajc.xml referencing LTWAspect).
In my example above I declare a static member, a non-static ("normal") member and a non-static method. My advice prints the static member and calls the non-static method, both via reflection. The non-static method then prints the non-static member, again via reflection. This is not nice, but it works and proves the ITD in combination with LTW is possible. There might be a more elegant way, but if so I am unaware of it. (Update: There is a more elegant way: Just ignore the errors marked by Eclipse IDE, see above.)
Program output
around before
main
around after
value of static field
value of normal field