Setting up a classpath for Jansi - java

I've been trying to use Jansi for the first time on a Java program, but I can't import org.fusesource.jansi.AnsiConsole and get an error saying that this package does not exist.
I downloaded the latest Jansi version under C:\Users\Leonardo\Jansi where the jansi-1.17.1.jar is (I do not know if you have to do anything with this file after that or not). The text editor I am using for the program is TextPad and I know that to use Jansi I have to manually add it to its environment variables. I tried doing with by going into
Configure > Preferences > Environment Variables
and adding a new variable
named: %CLASSPATH%
with the
value: C:\Users\Leonardo\Jansi\jansi-1.17.1.jar;%CLASSPATH%;
If anyone has any experience with TextPad and Jansi I would appreciate any help you could give me. I am also open to switching to a different text editor or ide if someone has used Jansi with another program. Below is the sample code I've been using to see if I set it up correctly, no luck so far.
import org.fusesource.jansi.AnsiConsole;
public class JansiDemo{
public static final String CLS = "\u001b[2J\u001b[1;1H";
public static final String RED = "\u001b[31;1m";
public static final String GREEN = "\u001b[32;1m";
public static final String YELLOW = "\u001b[33;1m";
public static final String BLUEONWHITE = "\u001b[34;47m";
public static void main(String[] args){
AnsiConsole.systemInstall();
System.out.println(RED + "apple " + YELLOW + "banana " + GREEN + "pepper");
System.out.println(BLUEONWHITE + "this is blue on white" + NORMAL);
}
}
This is what the console outputs when I compile it:
E:\game.java:2: error: package org.fusesource.jansi does not exist import org.fusesource.jansi.AnsiConsole;
E:\game.java:16: error: cannot find symbol AnsiConsole.systemInstall();
symbol: variable AnsiConsole
location: class game
2 errors
Tool completed with exit code 1

To set up a classpath in Textpad (I am using version 8.1.2) you have to download the .jar file first and then go to into Configure > Preferences... > Environment Variables > New. Then set name:CLASSPATH and the value to the directory of the .jar file in my case value: C:\Users<User_Name>\Jansi\jansi-1.17.1.jar;%CLASSPATH%; After this hit Ok and Apply and restart any command prompt you have open to save any changes.

Related

(Java) How do you do full paths in the File class when you are using Textmate in a macbook?

I have a macbook and our school told us to use TextMate if we don't have a pc. I can do this in a pc, but I'm having trouble with the syntax in a mac OS since they are different. This is what I'm using and even though I created a file named Data.txt, the prompt that pops up says it doesn't exist.
import java.io.File;
class FileClassTutorial
{
public static void main(String[]args)
{
File x= new File("MacintoshHD/Users/Alexis/Desktop/Data.txt");
if(x.exists())
System.out.println(x.getName() + " exists!!!");
else
System.out.println("This file doesn't exist");
}
}
new File("something/other") is assumed to be "./something/other" where '.' is the current working directory where jvm is started. It's what you need ?
Otherwise, you can start a path by '/' to have an absolute path from super-root of your file disk

Cannot create Path object from a string

I'm following along the Basic I/O Tutorial on Oracle.com, but I'm having difficulty making a Path object:
Path p1 = Paths.get("/tmp/foo");
Which gives the error:
error: The method get(URI) in the type Paths is not applicable for the arguments (String).
I'm on Linux and I'm working in Eclipse Kepler. I'm trying to access a text file in the current directory. Using Scanner and File I can work with the file, but I'd also like to fiddle around with a path to the file so I can continue with the tutorial.
edit: The entirety of the program is below. The second half is me being a rookie and confirming the file exists/works. When I comment out the Path definitions, I get the output of "Test" which is in the 'save.txt' file.:
package projectSARA;
import java.util.*;
import java.io.*;
import java.net.URI;
import java.nio.file.Path;
import java.nio.file.Paths;
public class Main {
public static void main(String[] args) {
String saveFile = "save.txt";
Path p1 = Paths.get(saveFile);
Path p2 = Paths.get("save.txt");
File file = new File(saveFile);
try{
Scanner in = new Scanner(file);
String test = in.next();
System.out.println(test);
}
catch(FileNotFoundException e){
System.out.println("File not found");
}
}// end main
}
It appears to be a problem of the (default) JRE settings in Eclipse.
To solve it, in the Package Explorer, right-click the "JRE System Library" > properties.
Select "Execution environment", then select "JavaSE-1.7 (java-7-oracle)", press OK.
It happened to me when creating a new project outside the workspace.
Actually I had the same issue with Oracle Java 8 running on Eclipse. But the solution above didn't help. The solution for me was to simply:
right-click on project in Package Explorer
Select Java Compiler
Enable Project Specific Settings
Set Compiler Compliance Level to 1.7

Can't find symbol even though class is in the same folder

I have looked at all the other stuff, mine is a compatibility issue I think, or PATH maybe. I have a bunch of classes I've been using since about 2008 and now the java command and the javac command can't find the classes even though they are in the same directory/folder. I have C:\Program Files\Java\jdk1.6.0_25\bin in the Path variable, but nothing in the Classpath. I normally have the compiled classes in the same folder with the java I'm compiling. I've been doing the same thing for 5 years! I have recompiled the lowest level class which is called WotifCat01. The compiler comes back with
WotifCat00.java:27:cannot find symbol
Symbol : WotifCat01
import java.io.*;
/** Find/replace program **/
class WotifCat00
{
private static int cnt;
private static String[][] filnamStrg={ {"Data/reftfile.txt","Data/AccumData/allreftfile.txt","","",""},
{"Data/reft1file.txt","Data/AccumData/allreft1file.txt","","",""},
{"Data/reft2file.txt","Data/AccumData/allreft2file.txt","","",""},
{"Data/reft3file.txt","Data/AccumData/allreft3file.txt","","",""},
{"Data/reft4file.txt","Data/AccumData/allreft4file.txt","","",""},
{"Data/work1file.txt","Data/AccumData/alltextsrc.txt","","",""},
{"","","","",""} };
private static String[] args1={"","","","",""};
public static void main(String[] args) {
while (filnamStrg[cnt][0] != "") {
args1[0] = filnamStrg[cnt][0];
args1[1] = filnamStrg[cnt][1];
WotifCat01 wotifCat01 = new WotifCat01();
wotifCat01.main(args1);
cnt++;
}
}
}
I've used this setup for a while and it worked fine on my laptop till now with Windows 7. I suspect something I've installed has overwritten something. This has to be really simple but I can't see it. I've removed jdk1.7.0_25 back to 1.6 but no change.
I backed up the classpath and deleted it. Now works fine. It contained IBM DB2 paths. DB2 I had installed in 2011 so discounted it, but it did have java paths in it so it must have overrode the path. I am not sure how DB2 managed to do so 2 years after install, I may have inadvertently activated something. Thanks for your input.
Neil Mc

"Error: Could not find or load main class My.class"

Im using Java SDK 1.7 on Windows 7 via cmd.exe . Up until a few hours ago everything was working correctly when suddenly I was unable to run my compiled class files, consistently presented with the error in the title.
I seem to be able to compile my My.java file however I am unable to run the resulting class file (My.class). I am constantly given the error "Error: Could not find or load main class My.class". I have tried this with multiple other class files all resulting in the same problem.
My 'Path' environment variable is set to "C:\Program Files (x86)\Java\jdk1.7.0_05\bin" if you were wondering
I have tried reinstalling, creating and setting a classpath variable (no luck), and even directly using the
java -cp . My.class
command.
I have tried these posts all to no avail, hence why I'm posting:
Error: Could not find or load main class
Error: Could not find or load main class- Novice
Could not find or load main class
Java 1.7.0_03 Error: Could not find or load main class
If it makes any difference my code is :
import javax.swing.JOptionPane;
class My {
public static void main(String[] args) {
final double x = 3.2;
int i = (int)x;
double m = 0;
if (x < 4) {
String saySomething = JOptionPane.showInputDialog(i);
System.out.println(saySomething);
}
else {
String saySomething = JOptionPane.showInputDialog(i);
System.out.println("Hello World");
}
while (m < 10) {
System.out.print(" While Loop ");
m++;
};
for (i=1; i < 10; i++) {
System.out.println("For Loop");
};
}
}
You should specify the classname instead of the file of the class to load. The difference is a simple matter of removing the .class extension.
I would use an IDE and you shouldn't get these issues. Compile and run is just a click of the mouse.
BTW to run your program from the command line
java -cp . My
You don't add .class
Position yourself in a directory of your project (you need to have src and bin directories there, assuming you keep sources in src and binaries in bin)
java -cp bin My
I myself was facing the same problem. It was happening because I was being remiss in typing the name of the class properly. In my instance, I was typing
java doubler
instead of
java Doubler

ClassNotFoundException Sandwich.Java... where is it?

Ok so I have a file called 'Sandwich.java' at the root folder and a file called 'SandwichType.java' inside of a folder at [root]/MyFrstPkg. For whatever reason it won't compile claiming that Sandwich.java cannot be found. Here is the directory structure:
root --
|
|- Sandwich.java
|
|-MyFirstPkg
|
|-SandwichType.java
Here is Sandwich.java:
//note I also tried adding package MyFrstPkg; in this file as well and removing the leading MyFrstPkg. from the import statement below, still no luck.
import MyFrstPkg.SandwichType; //the text 'MyFrstPkg' part is underlined as an error
class Sandwich{
SandwichType type; //the text 'SandwichType' is underlined as an error
public static void main(String[] args){
Sandwich sndwch1 = new Sandwich();
sndwch1.type = SandwichType.HAM; //the text 'SandwichType' is underlined as an error
System.out.println("A HAM costs $"+sndwch1.type.getCost());
System.out.println("and has "+ sndwch1.type.getSlices()+" slices.");
}
}
and here is SandwichType.java:
package MyFrstPkg;
enum SandwichType{
HAM(0,0f);
SandwichType(int numSlices, float cost){ // constructor - Ryan changed 'numslices' to 'numSlices'
this.numSlices = numSlices;
this.cost = cost;
} //end constructor
private int numSlices; //These are specific to this
private float cost; // enum class...
public int getSlices(){
return numSlices;
}
public float getCost(){
return cost;
}
}//end of SandwichType enum
I browse in CMD to the root location and run 'java Sandwich.java' and all I get is a ClassNotFoundExeption Sandwich.Java, why is it not found? IT IS ITSELF D:
To compile your class, use
javac Sandwich.java
If this gives no error messages, you should be able to call
java Sandwich
to start your program.
If the first works without error, we are one step further. If the second does not work, try this instead:
java -cp . Sandwich
If it works this way, you have set some wrong classpath. Type echo %CLASSPATH% and post the result. (Normally you should not need the CLASSPATH variable at all for simple projects.)
Netbeans is very project-based, so I'd try creating a basic Java Application project and putting them in there.
The name of your project becomes the base package name if you choose "new project" then "java application". You would want to choose "java project with existing sources" if you already have the package/directory structure set up.
If you choose "java application" you can delete the default package name, then right click on the project name, choose "New" Then "Java Package ..." from the list.
EDIT: - Sorry... didn't notice. Your classes aren't public. That's your real problem.
public class Sandwich { ...
public enum SandwichType { ...
As for trying to run java Sandwich.java ... er, you can't. That's source code. It has to be compiled to a class first.

Categories