java


ERROR: Cannot find symbol — How do I get my class to compile when it depends on a class in a subfolder?


Inside my package alignmentparser I have a class named Subject where I have a string I want to convert to a JSON object. After researching how I might achieve this, I found the package JSON-java, which I downloaded and placed into my package as a subdirectory named JSON.
My classes' package statements state package alignmentparser;. I updated the package statements in each file in the JSON subdirectory to package alignmentparser.JSON;.
Now I can't get my TestRunner class (which depends on Subject) to compile. I get these error messages:
./alignmentparser/Subject.java:85: error: cannot find symbol
JSONObject json = new JSONObject(sb.toString());
^ symbol: class JSONObject location: class Subject
./alignmentparser/Subject.java:85: error: cannot find symbol
JSONObject json = new JSONObject(sb.toString());
^ symbol: class JSONObject location: class Subject
What can I do to get past these error messages?
EDIT: Here is the whole code, just in case there are errors I don't know about.
Subject.java
package alignmentparser;
import java.net.*;
import java.io.*;
import java.util.Scanner;
import java.util.regex.*;
public class Subject {
private String id;
private String sequence;
// Position on subject where alignment with query begins, inclusive
private int start;
// Position on subject where alignment with query ends, inclusive
private int end;
public Subject(String accessNum, int hitFrom, int hitTo, String seq) {
id = accessNum;
sequence = seq;
start = hitFrom;
end = hitTo;
getEntireSubject();
}
// Getters
public String getSequence() {
return sequence;
}
public int getStart() {
return start;
}
public int getEnd() {
return end;
}
// Fetches accession number from NCBI
// https://www.ncbi.nlm.nih.gov/books/NBK25498/#chapter3.Application_2_Converting_access
private void getEntireSubject() {
try {
// e post to Entrez
String link = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/epost.fcgi?db=nucleotide&id=" + id;
URLConnection connection = new URL(link).openConnection();
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
connection.connect();
BufferedReader r = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
String webEnv = "";
String queryKey = "";
// Put together string to be scanned to find patterns
StringBuilder sb = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
sb.append(line);
}
String result = sb.toString();
// Specify patterns to search for
String queryKeyRegEx = "<QueryKey>(\\d+)<\\/QueryKey>";
String webEnvRegEx = "<WebEnv>(\\S+)<\\/WebEnv>";
// Create Pattern objects
Pattern queryKeyPattern = Pattern.compile(queryKeyRegEx);
Pattern webEnvPattern = Pattern.compile(webEnvRegEx);
// Create Matcher objects
Matcher queryKeyMatcher = queryKeyPattern.matcher(result);
if (queryKeyMatcher.find()) {
queryKey = queryKeyMatcher.group(1);
System.out.println(queryKey);
}
Matcher webEnvMatcher = webEnvPattern.matcher(result);
if (webEnvMatcher.find()) {
webEnv = webEnvMatcher.group(1);
System.out.println(webEnv);
}
// e fetch to Entrez
link = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=nucleotide&query_key="+queryKey+"&WebEnv="+webEnv+"&usehistory=y";
connection = new URL(link).openConnection();
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
connection.connect();
r = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
sb = new StringBuilder();
while ((line = r.readLine()) != null) {
sb.append(line);
}
JSONObject json = new JSONObject(sb.toString());
System.out.println(json);
} catch (Exception e) {
System.out.println(e);
}
}
}
TestRunner.java
package alignmentparser;
import java.util.*;
class TestRunner {
private String query = "ATCCTGTCTGTGTTAGGAG-AGTCTACTTC-TTAACNGAGGGATTCANTNTTTCCTGCANAGGCGGCCGTCNATGAANACCCTGTTTGTGGACAGCTACNGNGAGATGCTTTTNTTTCTGCAGTCACTGTTCATGCTGGCCACCGTGGTGCTGTACTTCANCCACCTCAAGGAGTATGTGGCTTCCATGGTATTCTCCCTGGCCTTGGGCTGGNCCAACATGCTCTACTACNCCCGCGGTTTCCAGCAGANGGGCATCTATGCCGTCATGATANANAANATGATCCTGAGAGACCTGTGCCGTTTCATGTTTGTCTACGTCGTCTTNTTGTTCGGNTTTTCCACAGCGGNGGTGACGCTGATTGAAGACGGNAANAATGACTCCCTGCCGTCTGAGTCCACGTCGCACAGGNGGNGGGGGCCTGCCTGCANGCCCCCCGATAGCTCCTACAACAGCCTGTACTCCACCTGCCTGGAGCTGTTCAAGTTCACCATCGGCATGGGCGACCTGGANTTCACTGANAACTATGACTTCAAGGCTGTCTTCATCATCCTGCTGCTGGCCTATGTAATTCTCACCTACATCCTCNTGCTCAACATGCTNATCGCCCTCNTGGGTGAGACTGTCAACAAGATCGCACAGNNAGAGCAAGAACATCTGGAANCTGCAGAGAGCCATCACCATCCTGNACACGGAGAAGAGCTTCCTTAAGTGCATGAGGAAGGCCTTCCGCTCAGGCAAGCTGCNTGCANGTGGGGTACACACCTGATGGCAAGGACGACTACCGG";
private String midline = "||||||||||||||||||| |||||||||| || | |||||||||| | ||||||||| ||||||||||| ||||| ||||||||||||||||||||| | ||||||||||| |||||||||||||||||||||||||||||||||||||||||||||| |||||||||||||||||||||||||||||||||||||||||||||||||||| ||||||||||||||||| |||||||||||||||||| |||||||||||||||||||||| | || ||||||||||||||||||||||||||||||||||||||| ||||||| |||||||| ||||||||||||| ||||||||||||||||||||| || |||||||||||||||||||||||||||||||||||| || ||||||||||||||| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |||||||| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| ||||||||||||| ||||||||| ||||||||||||||||||||||||||||| ||||||||||||||||||| |||||||||||||||||||||||| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |||| ||||||||||||||||||||||||||||||||||||";
private Subject subj = new Subject("NM_080704", 1710, 2495, "ATCCTGTCTGTGTTAGGAGGAGTCTACTTCTTTTTCCGAGGGATTCAGTATTTCCTGCAGAGGCGGCCGTCGATGAAGACCCTGTTTGTGGACAGCTACAGTGAGATGCTTTTCTTTCTGCAGTCACTGTTCATGCTGGCCACCGTGGTGCTGTACTTCAGCCACCTCAAGGAGTATGTGGCTTCCATGGTATTCTCCCTGGCCTTGGGCTGGACCAACATGCTCTACTACACCCGCGGTTTCCAGCAGATGGGCATCTATGCCGTCATGATAGAGAAGATGATCCTGAGAGACCTGTGCCGTTTCATGTTTGTCTACATCGTCTTCTTGTTCGGGTTTTCCACAGCGGTGGTGACGCTGATTGAAGACGGGAAGAATGACTCCCTGCCGTCTGAGTCCACGTCGCACAGGTGGCGGGGGCCTGCCTGCAGGCCCCCCGATAGCTCCTACAACAGCCTGTACTCCACCTGCCTGGAGCTGTTCAAGTTCACCATCGGCATGGGCGACCTGGAGTTCACTGAGAACTATGACTTCAAGGCTGTCTTCATCATCCTGCTGCTGGCCTATGTAATTCTCACCTACATCCTCCTGCTCAACATGCTCATCGCCCTCATGGGTGAGACTGTCAACAAGATCGCACAGG-AGAGCAAGAACATCTGGAAGCTGCAGAGAGCCATCACCATCCTGGACACGGAGAAGAGCTTCCTTAAGTGCATGAGGAAGGCCTTCCGCTCAGGCAAGCTGC-TGCAGGTGGGGTACACACCTGATGGCAAGGACGACTACCGG");
public static void main(String[] args) {
TestRunner analyzer = new TestRunner();
analyzer.identifyMismatches();
analyzer.identifyResidue(547);
}
/**
* Identifies the residue encoded by the codon at the specified codon count.
*/
private void identifyResidue(int pos) {
HashMap<String, ArrayList<String>> codons = assembleCodonMap();
// Set base position of first codon in the CDS (to be retrieved from GenBank later)
int posOfCDS = 276;
// Adjust given codon count to account for any difference between CDS / query length
// NOTE: Subtract 3 to account for conversion between codon count and base count
int adjustedPos = (pos * 3) - (subj.getStart() - posOfCDS) - 3;
System.out.println(adjustedPos + subj.getStart());
if (adjustedPos < 0) {
System.out.println("Codon " + pos + " is not viewable in this alignment.");
//System.out.println("You can view codons " + (posFirstCodon + 1) + " through " + ((2495 - 276) / 3));
}
// Get codon
String found = "";
for (int i = 0; i < query.length(); i += 3) {
if (i == adjustedPos) {
found = query.substring(i, i + 3);
System.out.println(found + " at " + (i + subj.getStart()));
}
}
// Identify residue that codon translates to
for (String codon : codons.keySet()) {
if (codons.get(codon).contains(found)) {
System.out.println("Residue is " + codon);
}
}
}
private HashMap<String, ArrayList<String>> assembleCodonMap() {
ArrayList<String> ala = new ArrayList<String>();
ala.add("GCT");
ala.add("GCC");
ala.add("GCA");
ala.add("GCG");
ArrayList<String> arg = new ArrayList<String>();
arg.add("CGT");
arg.add("CGC");
arg.add("CGA");
arg.add("CGG");
arg.add("AGA");
arg.add("AGG");
ArrayList<String> asn = new ArrayList<String>();
asn.add("AAT");
asn.add("AAC");
ArrayList<String> asp = new ArrayList<String>();
asp.add("GAT");
asp.add("GAC");
ArrayList<String> cys = new ArrayList<String>();
cys.add("TGT");
cys.add("TGC");
ArrayList<String> gln = new ArrayList<String>();
gln.add("CAA");
gln.add("CAG");
ArrayList<String> glu = new ArrayList<String>();
glu.add("GAA");
glu.add("GAG");
ArrayList<String> gly = new ArrayList<String>();
gly.add("GGT");
gly.add("GGC");
gly.add("GGA");
gly.add("GGG");
ArrayList<String> his = new ArrayList<String>();
his.add("CAT");
his.add("CAC");
ArrayList<String> ile = new ArrayList<String>();
ile.add("ATT");
ile.add("ATC");
ile.add("ATA");
ArrayList<String> leu = new ArrayList<String>();
leu.add("TTA");
leu.add("TTG");
leu.add("CTT");
leu.add("CTC");
leu.add("CTA");
leu.add("CTG");
ArrayList<String> lys = new ArrayList<String>();
lys.add("AAA");
lys.add("AAG");
ArrayList<String> met = new ArrayList<String>();
met.add("ATG");
ArrayList<String> phe = new ArrayList<String>();
phe.add("TTT");
phe.add("TTC");
ArrayList<String> pro = new ArrayList<String>();
pro.add("CCT");
pro.add("CCC");
pro.add("CCA");
pro.add("CCG");
ArrayList<String> ser = new ArrayList<String>();
ser.add("TCT");
ser.add("TCC");
ser.add("TCA");
ser.add("TCG");
ser.add("AGT");
ser.add("AGC");
ArrayList<String> thr = new ArrayList<String>();
thr.add("ACT");
thr.add("ACC");
thr.add("ACA");
thr.add("ACG");
ArrayList<String> trp = new ArrayList<String>();
trp.add("TGG");
ArrayList<String> tyr = new ArrayList<String>();
tyr.add("TAT");
tyr.add("TAC");
ArrayList<String> val = new ArrayList<String>();
val.add("GTT");
val.add("GTC");
val.add("GTA");
val.add("GTG");
HashMap<String, ArrayList<String>> codons = new HashMap<String, ArrayList<String>>();
codons.put("alanine", ala);
codons.put("arginine", arg);
codons.put("asparagine", asn);
codons.put("aspartic acid", asp);
codons.put("cysteine", cys);
codons.put("glutamine", gln);
codons.put("glutamic acid", glu);
codons.put("glycine", gly);
codons.put("histidine", his);
codons.put("isoleucine", ile);
codons.put("leucine", leu);
codons.put("lysine", lys);
codons.put("methionine", met);
codons.put("phenylalanine", phe);
codons.put("proline", pro);
codons.put("serine", ser);
codons.put("threonine", thr);
codons.put("tryptophan", trp);
codons.put("tyrosine", tyr);
codons.put("valine", val);
return codons;
}
/**
* Prints out mismatches in the alignment of subject and query.
* Base positions are referenced from subject.
*/
private void identifyMismatches() {
// Loops through each character in the midline
for (int i = 0; i < midline.length(); i++) {
// If character is a space, denoting a mismatch
if (midline.charAt(i) == ' ' && query.charAt(i) != 'N') {
// Print out the characters at the same position in subject & query
System.out.println(subj.getStart() + i);
System.out.println(subj.getSequence().charAt(i) + ">" + query.charAt(i));
}
}
}
}
EDIT 2: Screenshot of my folder structure.
Firstly, unless you are planning to modify the source code for JSON-java, you don't really need to include the project as source code. Just modify your project dependency to include the JSON-java jar artifact or include it in your classpath so that the necessary classes can be found by the run time.
Secondly, if you really want to include the project as source code and compile it yourself, you still don't need to change the package structure of the project. You can compile it and build the artifact (jar) outside your application.
Thirdly, if you still want to include the project as source code in your own package, you need to modify the package declaration of each and every class within this project. It looks like you have already tried it. But to make sure you have, for example changed the package name of the class JSONObject from org.json to alignmentparser.org.json? Also, when you want to use one or more of these JSON-java classes in your own code, you need to import those classes. E.g. import org.json.JSONObject. I see in your code sample (Subject.java), that you are not doing it. Once you have fixed the package renaming and included the necessary class import statements, you should not get any compiler error.

Related Links

Swing…Adding JTextField blanks out my JPanel
Synchronized in java ensuring memory updates for other threads?
Java-Setting a tiimeout function when receiving a UDP packet java
Pass data to action class using Ajax
How to get last value from the list and store in another
How to create a tar.gz file from a list of files?
What is wrong with this Java code to handle ArrayList? [duplicate]
Why doesn't my JavaFX UI update when I add or remove nodes in runtime?
How to stick a JDialog to a JFrame?
Array in get parameter Rest Assured
Bad characters when replacing text in pdf using pdfbox
Why is Scriptlet syntax shown in the browser when the server is Red hat Linux 5?
How to get Java Drawables from a Plugin in Unity
How do I use maths and a 2d graph using random numbers in Java
How to utilise OSGi-loaded classes in a program after starting their source bundle
Pressing enter key does not select checkbox

Categories

HOME
ionic-framework
blogger
openmp
minimum-spanning-tree
heroku
view
stock
hashmap
google-oauth
jpeg
retrofit
angular-ui-bootstrap
enterprise-library-5
autotools
append
communication
user-input
php-7.1
remote-access
designer
clearcase-ucm
interop
mps
claims-based-identity
emgucv
shopware
dynamic-featured-image
rst2pdf
replaceall
delicious-api
madlib
r-raster
catel
force-layout
unspecified
reportbuilder
tasker
particles.js
optix
g-code
eigenvalue
password-encryption
midl
python-c-api
yii2-extension
elgg
blacklist
background-service
mikroc
websitepanel
jfugue
endeca-workbench
marching-cubes
django-unittest
sailfish-os
mmc
system32
oberon
key-management
csquery
knuth
network-interface
uitouch
wp-query
marmalade
seaside
undefined-reference
c18
cdc
free-variable
nsnetservice
runas
frameset
amazon-appstore
asyncfileupload
gwt-ext
php-parser
infobox
django-tagging
locate
datareader
sortable-tables
revisions
webkit.net
defensive-programming
commodore
3gp

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App